-
-
Notifications
You must be signed in to change notification settings - Fork 222
/
expand-npm-wildcard.spec.js
87 lines (70 loc) · 2.6 KB
/
expand-npm-wildcard.spec.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const ExpandNpmWildcard = require('./expand-npm-wildcard');
const fs = require('fs');
let parser, readPkg;
beforeEach(() => {
readPkg = jest.fn();
parser = new ExpandNpmWildcard(readPkg);
});
describe('ExpandNpmWildcard#readPackage', () => {
it('can read package', () => {
const expectedPackage = {
'name': 'concurrently',
'version': '6.4.0',
};
jest.spyOn(fs, 'readFileSync').mockImplementation((path, options) => {
if (path === 'package.json') {
return JSON.stringify(expectedPackage);
}
return null;
});
const actualReadPackage = ExpandNpmWildcard.readPackage();
expect(actualReadPackage).toEqual(expectedPackage);
});
it('can handle errors reading package', () => {
jest.spyOn(fs, 'readFileSync').mockImplementation(() => {
throw new Error('Error reading package');
});
expect(() => ExpandNpmWildcard.readPackage()).not.toThrow();
expect(ExpandNpmWildcard.readPackage()).toEqual({});
});
});
it('returns same command if not an npm run command', () => {
const commandInfo = {
command: 'npm test'
};
expect(readPkg).not.toHaveBeenCalled();
expect(parser.parse(commandInfo)).toBe(commandInfo);
});
it('returns same command if no wildcard present', () => {
const commandInfo = {
command: 'npm run foo bar'
};
expect(readPkg).not.toHaveBeenCalled();
expect(parser.parse(commandInfo)).toBe(commandInfo);
});
it('expands to nothing if no scripts exist in package.json', () => {
readPkg.mockReturnValue({});
expect(parser.parse({ command: 'npm run foo-*-baz qux' })).toEqual([]);
});
for (const npmCmd of ['npm', 'yarn', 'pnpm']) {
describe(`with an ${npmCmd}: prefix`, () => {
it('expands to all scripts matching pattern', () => {
readPkg.mockReturnValue({
scripts: {
'foo-bar-baz': '',
'foo--baz': '',
}
});
expect(parser.parse({ command: `${npmCmd} run foo-*-baz qux` })).toEqual([
{ name: 'foo-bar-baz', command: `${npmCmd} run foo-bar-baz qux` },
{ name: 'foo--baz', command: `${npmCmd} run foo--baz qux` },
]);
});
it('caches scripts upon calls', () => {
readPkg.mockReturnValue({});
parser.parse({ command: `${npmCmd} run foo-*-baz qux` });
parser.parse({ command: `${npmCmd} run foo-*-baz qux` });
expect(readPkg).toHaveBeenCalledTimes(1);
});
});
}