-
Notifications
You must be signed in to change notification settings - Fork 132
/
Use.test.ts
67 lines (56 loc) · 2.47 KB
/
Use.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import {describe, beforeEach, it, expect} from '@jest/globals';
import {ppath, xfs, npath} from '@yarnpkg/fslib';
import process from 'node:process';
import {runCli} from './_runCli';
beforeEach(async () => {
// `process.env` is reset after each tests in setupTests.js.
process.env.COREPACK_HOME = npath.fromPortablePath(await xfs.mktempPromise());
process.env.COREPACK_DEFAULT_TO_LATEST = `0`;
});
describe(`UseCommand`, () => {
it(`should set the package manager in the current project`, async () => {
await xfs.mktempPromise(async cwd => {
await xfs.writeJsonPromise(ppath.join(cwd, `package.json`), {
packageManager: `yarn@1.0.0`,
});
await expect(runCli(cwd, [`use`, `yarn@1.22.4`])).resolves.toMatchObject({
exitCode: 0,
});
await expect(xfs.readJsonPromise(ppath.join(cwd, `package.json`))).resolves.toMatchObject({
packageManager: `yarn@1.22.4+sha512.a1833b862fe52169bd6c2a033045a07df5bc6a23595c259e675fed1b2d035ab37abe6ce309720abb6636d68f03615054b6292dc0a70da31c8697fda228b50d18`,
});
await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
exitCode: 0,
stdout: `1.22.4\n`,
});
});
});
it(`should create a package.json if absent`, async () => {
await xfs.mktempPromise(async cwd => {
await expect(runCli(cwd, [`use`, `yarn@1.22.4`])).resolves.toMatchObject({
exitCode: 0,
stderr: `warning package.json: No license field\nwarning No license field\n`,
});
await expect(xfs.readJsonPromise(ppath.join(cwd, `package.json`))).resolves.toMatchObject({
packageManager: `yarn@1.22.4+sha512.a1833b862fe52169bd6c2a033045a07df5bc6a23595c259e675fed1b2d035ab37abe6ce309720abb6636d68f03615054b6292dc0a70da31c8697fda228b50d18`,
});
await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
exitCode: 0,
stdout: `1.22.4\n`,
stderr: ``,
});
// Ensure Corepack is able to detect package.json in parent directory
const subfolder = ppath.join(cwd, `subfolder`);
await xfs.mkdirPromise(subfolder);
await expect(runCli(subfolder, [`use`, `yarn@2.2.2`])).resolves.toMatchObject({
exitCode: 0,
stderr: ``,
});
await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
exitCode: 0,
stdout: `2.2.2\n`,
stderr: ``,
});
});
});
});