-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathinit.unit.test.ts
More file actions
131 lines (114 loc) · 3.71 KB
/
init.unit.test.ts
File metadata and controls
131 lines (114 loc) · 3.71 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { vol } from 'memfs';
import { afterEach, beforeEach, describe, expect } from 'vitest';
import { MEMFS_VOLUME } from '@code-pushup/test-utils';
import type { ProcessResult } from '@code-pushup/utils';
import * as utils from '@code-pushup/utils';
import { initCodePushup, nxPluginGenerator } from './init.js';
import * as createUtils from './utils.js';
describe('nxPluginGenerator', () => {
it('should create valid command', () => {
expect(nxPluginGenerator('init', { skipNxJson: true })).toStrictEqual({
command: 'npx',
args: ['nx', 'g', '@code-pushup/nx-plugin:init', '--skipNxJson'],
});
});
});
describe('initCodePushup', () => {
const spyExecuteProcess = vi.spyOn(utils, 'executeProcess');
const spyParseNxProcessOutput = vi.spyOn(createUtils, 'parseNxProcessOutput');
const spySetupNxContext = vi.spyOn(createUtils, 'setupNxContext');
const spyTeardownNxContext = vi.spyOn(createUtils, 'teardownNxContext');
beforeEach(() => {
// needed to get test folder set up
vol.fromJSON(
{
'random-file': '',
},
MEMFS_VOLUME,
);
vol.rm('random-file', () => void 0);
spyExecuteProcess.mockResolvedValue({
stdout: 'stdout-mock',
stderr: '',
} as ProcessResult);
});
afterEach(() => {
spyExecuteProcess.mockReset();
});
it('should add packages and create config file', async () => {
const projectJson = { name: 'my-lib' };
vol.fromJSON(
{
'nx.json': '{}',
'project.json': JSON.stringify(projectJson),
},
MEMFS_VOLUME,
);
await initCodePushup();
expect(spySetupNxContext).toHaveBeenCalledTimes(1);
expect(spyExecuteProcess).toHaveBeenNthCalledWith(1, {
command: 'npx',
args: ['nx', 'g', '@code-pushup/nx-plugin:init', '--skipNxJson'],
observer: expect.any(Object),
verbose: false,
});
expect(spyParseNxProcessOutput).toHaveBeenNthCalledWith(1, 'stdout-mock');
expect(spyExecuteProcess).toHaveBeenNthCalledWith(2, {
command: 'npx',
args: [
'nx',
'g',
'@code-pushup/nx-plugin:configuration',
'--skipTarget',
`--project="${projectJson.name}"`,
],
verbose: false,
});
expect(spyParseNxProcessOutput).toHaveBeenNthCalledWith(1, 'stdout-mock');
expect(spyParseNxProcessOutput).toHaveBeenCalledTimes(2);
expect(spyExecuteProcess).toHaveBeenCalledTimes(2);
expect(spyTeardownNxContext).toHaveBeenCalledTimes(1);
expect(spyTeardownNxContext).toHaveBeenNthCalledWith(1, {
projectName: projectJson.name,
nxJsonTeardown: false,
projectJsonTeardown: false,
});
});
it('should teardown nx.json if set up', async () => {
const projectJson = { name: 'my-lib' };
vol.fromJSON(
{
'project.json': JSON.stringify(projectJson),
},
MEMFS_VOLUME,
);
await initCodePushup();
expect(spySetupNxContext).toHaveBeenCalledTimes(1);
expect(spyTeardownNxContext).toHaveBeenCalledTimes(1);
expect(spyTeardownNxContext).toHaveBeenNthCalledWith(1, {
projectName: projectJson.name,
nxJsonTeardown: true,
projectJsonTeardown: false,
});
});
it('should teardown project.json if set up', async () => {
vol.fromJSON(
{
'nx.json': '{}',
},
MEMFS_VOLUME,
);
spyExecuteProcess.mockResolvedValue({
stdout: 'stdout-mock',
stderr: '',
} as ProcessResult);
await initCodePushup();
expect(spySetupNxContext).toHaveBeenCalledTimes(1);
expect(spyTeardownNxContext).toHaveBeenCalledTimes(1);
expect(spyTeardownNxContext).toHaveBeenNthCalledWith(1, {
projectName: 'source-root',
nxJsonTeardown: false,
projectJsonTeardown: true,
});
});
});