Skip to content

Commit b86ffa0

Browse files
test/add test on get user pkg manager (#349)
* chore: make possible to write tests aside with implementations * test: write getUserPkgManager test TO-DOs * test: add test to building the path * test: add tests to manual file checking * test: add tests to npm_config_user_agent * Update packages/cli/src/utils/getUserPkgManager.spec.ts
1 parent 3ee7cd6 commit b86ffa0

File tree

2 files changed

+101
-1
lines changed

2 files changed

+101
-1
lines changed

packages/cli/jest.config.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@
22
export default {
33
preset: 'ts-jest',
44
testEnvironment: 'node',
5-
testMatch: ["<rootDir>/test/**/*.ts?(x)", "<rootDir>/test/**/?(*.)+(spec|test).ts?(x)"],
5+
testMatch: [
6+
"<rootDir>/test/**/*.ts?(x)",
7+
"<rootDir>/test/**/?(*.)+(spec|test).ts?(x)",
8+
"<rootDir>/src/**/?(*.)+(spec|test).ts?(x)"
9+
],
610
};
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import { randomUUID } from "crypto";
2+
import { pathExists } from "./fileSystem";
3+
import { getUserPackageManager } from "./getUserPkgManager";
4+
import * as pathModule from "path";
5+
6+
jest.mock('path', () => ({
7+
join: jest.fn().mockImplementation((...paths: string[]) => paths.join('/')),
8+
}))
9+
10+
jest.mock('./fileSystem', () => ({
11+
pathExists: jest.fn().mockResolvedValue(false),
12+
}));
13+
14+
describe(getUserPackageManager.name, () => {
15+
let path: string;
16+
17+
beforeEach(() => {
18+
path = randomUUID();
19+
});
20+
21+
afterEach(jest.clearAllMocks);
22+
23+
describe(`should use ${pathExists.name} to check for package manager artifacts`, () => {
24+
it('should join the path with the artifact name', async () => {
25+
await getUserPackageManager(path);
26+
27+
expect(pathModule.join).toBeCalledWith(path, 'yarn.lock');
28+
expect(pathModule.join).toBeCalledWith(path, 'pnpm-lock.yaml');
29+
expect(pathModule.join).toBeCalledWith(path, 'package-lock.json');
30+
});
31+
32+
it(`should call ${pathExists.name} with the path.join result`, async () => {
33+
const expected = randomUUID();
34+
35+
(pathModule.join as jest.Mock).mockReturnValueOnce(expected);
36+
37+
await getUserPackageManager(path);
38+
39+
expect(pathExists).toBeCalledWith(expected);
40+
});
41+
42+
it('should return "yarn" if yarn.lock exists', async () => {
43+
(pathExists as jest.Mock).mockImplementation((path: string) => path.endsWith('yarn.lock'));
44+
45+
expect(await getUserPackageManager(path)).toBe('yarn');
46+
});
47+
48+
it('should return "pnpm" if pnpm-lock.yaml exists', async () => {
49+
(pathExists as jest.Mock).mockImplementation(async (path: string) => path.endsWith('pnpm-lock.yaml'));
50+
51+
expect(await getUserPackageManager(path)).toBe('pnpm');
52+
});
53+
54+
it('should return "npm" if package-lock.json exists', async () => {
55+
(pathExists as jest.Mock).mockImplementation((path: string) => path.endsWith('package-lock.json'));
56+
57+
expect(await getUserPackageManager(path)).toBe('npm');
58+
});
59+
60+
it('should return "npm" if npm-shrinkwrap.json exists', async () => {
61+
(pathExists as jest.Mock).mockImplementation((path: string) => path.endsWith('npm-shrinkwrap.json'));
62+
63+
expect(await getUserPackageManager(path)).toBe('npm');
64+
});
65+
});
66+
67+
describe(`if doesn't found artifacts, should use process.env.npm_config_user_agent to detect package manager`, () => {
68+
beforeEach(() => {
69+
(pathExists as jest.Mock).mockResolvedValue(false);
70+
})
71+
72+
it('should return "yarn" if process.env.npm_config_user_agent starts with "yarn"', async () => {
73+
process.env.npm_config_user_agent = 'yarn';
74+
75+
expect(await getUserPackageManager(path)).toBe('yarn');
76+
});
77+
78+
it('should return "pnpm" if process.env.npm_config_user_agent starts with "pnpm"', async () => {
79+
process.env.npm_config_user_agent = 'pnpm';
80+
81+
expect(await getUserPackageManager(path)).toBe('pnpm');
82+
});
83+
84+
it('if doesn\'t start with "yarn" or "pnpm", should return "npm"', async () => {
85+
process.env.npm_config_user_agent = randomUUID();
86+
87+
expect(await getUserPackageManager(path)).toBe('npm');
88+
});
89+
90+
it('should return "npm" if process.env.npm_config_user_agent is not set', async () => {
91+
delete process.env.npm_config_user_agent;
92+
93+
expect(await getUserPackageManager(path)).toBe('npm');
94+
});
95+
});
96+
})

0 commit comments

Comments
 (0)