From ff99265bb35191b07791c7ceaab95b69157e462c Mon Sep 17 00:00:00 2001 From: Fotis Papadogeorgopoulos Date: Wed, 22 Jan 2025 16:15:25 +0200 Subject: [PATCH] test(npm): use fs test utils in extract/pnpm.spec.ts (#33756) --- lib/modules/manager/npm/extract/pnpm.spec.ts | 42 +++++++++++++------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/lib/modules/manager/npm/extract/pnpm.spec.ts b/lib/modules/manager/npm/extract/pnpm.spec.ts index 5a18700e4a1f6a..73ad5be0f51330 100644 --- a/lib/modules/manager/npm/extract/pnpm.spec.ts +++ b/lib/modules/manager/npm/extract/pnpm.spec.ts @@ -1,7 +1,6 @@ import { Fixtures } from '../../../../../test/fixtures'; -import { getFixturePath, logger, partial } from '../../../../../test/util'; +import { fs, getFixturePath, logger, partial } from '../../../../../test/util'; import { GlobalConfig } from '../../../../config/global'; -import * as fs from '../../../../util/fs'; import * as yaml from '../../../../util/yaml'; import type { PackageFile } from '../../types'; import type { NpmManagerData } from '../types'; @@ -12,6 +11,8 @@ import { getPnpmLock, } from './pnpm'; +jest.mock('../../../../util/fs'); + describe('modules/manager/npm/extract/pnpm', () => { beforeAll(() => { GlobalConfig.set({ localDir: getFixturePath('pnpm-monorepo/', '..') }); @@ -23,9 +24,7 @@ describe('modules/manager/npm/extract/pnpm', () => { describe('.extractPnpmFilters()', () => { it('detects errors in pnpm-workspace.yml file structure', async () => { - jest - .spyOn(fs, 'readLocalFile') - .mockResolvedValueOnce('p!!!ckages:\n - "packages/*"'); + fs.readLocalFile.mockResolvedValueOnce('p!!!ckages:\n - "packages/*"'); const workSpaceFilePath = getFixturePath( 'pnpm-monorepo/pnpm-workspace.yml', @@ -60,7 +59,7 @@ describe('modules/manager/npm/extract/pnpm', () => { describe('.findPnpmWorkspace()', () => { it('detects missing pnpm-workspace.yaml', async () => { - jest.spyOn(fs, 'findLocalSiblingOrParent').mockResolvedValueOnce(null); + fs.findLocalSiblingOrParent.mockResolvedValueOnce(null); const packageFile = 'package.json'; const res = await findPnpmWorkspace(packageFile); @@ -72,10 +71,8 @@ describe('modules/manager/npm/extract/pnpm', () => { }); it('detects missing pnpm-lock.yaml when pnpm-workspace.yaml was already found', async () => { - jest - .spyOn(fs, 'findLocalSiblingOrParent') - .mockResolvedValueOnce('pnpm-workspace.yaml'); - jest.spyOn(fs, 'localPathExists').mockResolvedValueOnce(false); + fs.findLocalSiblingOrParent.mockResolvedValueOnce('pnpm-workspace.yaml'); + fs.localPathExists.mockResolvedValueOnce(false); const packageFile = 'package.json'; const res = await findPnpmWorkspace(packageFile); @@ -91,7 +88,23 @@ describe('modules/manager/npm/extract/pnpm', () => { }); describe('.detectPnpmWorkspaces()', () => { + beforeEach(() => { + const realFs = jest.requireActual('../../../../util/fs'); + + // The real implementations of these functions are used for this block; + // they do static path manipulation. + fs.findLocalSiblingOrParent.mockImplementation( + realFs.findLocalSiblingOrParent, + ); + fs.getSiblingFileName.mockImplementation(realFs.getSiblingFileName); + + // Falls through to reading from the fixture path defined in GlobalConfig + // at the top of this file + fs.readLocalFile.mockImplementation(realFs.readLocalFile); + }); + it('uses pnpm workspaces', async () => { + fs.localPathExists.mockResolvedValue(true); const packageFiles = partial>([ { packageFile: 'package.json', @@ -197,6 +210,7 @@ describe('modules/manager/npm/extract/pnpm', () => { }); it('filters none matching packages', async () => { + fs.localPathExists.mockResolvedValue(true); const packageFiles = [ { packageFile: 'package.json', @@ -242,14 +256,14 @@ describe('modules/manager/npm/extract/pnpm', () => { describe('.getPnpmLock()', () => { it('returns empty if failed to parse', async () => { - jest.spyOn(fs, 'readLocalFile').mockResolvedValueOnce(undefined as never); + fs.readLocalFile.mockResolvedValueOnce(undefined as never); const res = await getPnpmLock('package.json'); expect(res.lockedVersionsWithPath).toBeUndefined(); }); it('extracts version from monorepo', async () => { const plocktest1Lock = Fixtures.get('pnpm-monorepo/pnpm-lock.yaml', '..'); - jest.spyOn(fs, 'readLocalFile').mockResolvedValueOnce(plocktest1Lock); + fs.readLocalFile.mockResolvedValueOnce(plocktest1Lock); const res = await getPnpmLock('package.json'); expect(Object.keys(res.lockedVersionsWithPath!)).toHaveLength(11); }); @@ -259,13 +273,13 @@ describe('modules/manager/npm/extract/pnpm', () => { 'lockfile-parsing/pnpm-lock.yaml', '..', ); - jest.spyOn(fs, 'readLocalFile').mockResolvedValueOnce(plocktest1Lock); + fs.readLocalFile.mockResolvedValueOnce(plocktest1Lock); const res = await getPnpmLock('package.json'); expect(Object.keys(res.lockedVersionsWithPath!)).toHaveLength(1); }); it('returns empty if no deps', async () => { - jest.spyOn(fs, 'readLocalFile').mockResolvedValueOnce('{}'); + fs.readLocalFile.mockResolvedValueOnce('{}'); const res = await getPnpmLock('package.json'); expect(res.lockedVersionsWithPath).toBeUndefined(); });