Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(npm): use fs test utils in extract/pnpm.spec.ts #33756

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions lib/modules/manager/npm/extract/pnpm.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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/', '..') });
Expand All @@ -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',
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -91,7 +88,19 @@ describe('modules/manager/npm/extract/pnpm', () => {
});

describe('.detectPnpmWorkspaces()', () => {
beforeEach(() => {
const realFs = jest.requireActual<typeof fs>('../../../../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);
});

it('uses pnpm workspaces', async () => {
fs.localPathExists.mockResolvedValue(true);
const packageFiles = partial<PackageFile<NpmManagerData>>([
{
packageFile: 'package.json',
Expand Down Expand Up @@ -197,6 +206,7 @@ describe('modules/manager/npm/extract/pnpm', () => {
});

it('filters none matching packages', async () => {
fs.localPathExists.mockResolvedValue(true);
const packageFiles = [
{
packageFile: 'package.json',
Expand Down Expand Up @@ -242,14 +252,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);
});
Expand All @@ -259,13 +269,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();
});
Expand Down
Loading