/
nx-plugin.test.ts
272 lines (236 loc) · 9.51 KB
/
nx-plugin.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import {
checkFilesExist,
expectTestsPass,
isNotWindows,
killPorts,
newProject,
readJson,
readProjectConfig,
runCLI,
runCLIAsync,
uniq,
updateFile,
createFile,
readFile,
removeFile,
} from '@nrwl/e2e/utils';
describe('Nx Plugin', () => {
let npmScope: string;
beforeEach(() => {
npmScope = newProject();
});
it('should be able to generate a Nx Plugin ', async () => {
const plugin = uniq('plugin');
runCLI(`generate @nrwl/nx-plugin:plugin ${plugin} --linter=eslint`);
const lintResults = runCLI(`lint ${plugin}`);
expect(lintResults).toContain('All files pass linting.');
expectTestsPass(await runCLIAsync(`test ${plugin}`));
const buildResults = runCLI(`build ${plugin}`);
expect(buildResults).toContain('Done compiling TypeScript files');
checkFilesExist(
`dist/libs/${plugin}/package.json`,
`dist/libs/${plugin}/generators.json`,
`dist/libs/${plugin}/executors.json`,
`dist/libs/${plugin}/src/index.js`,
`dist/libs/${plugin}/src/generators/${plugin}/schema.json`,
`dist/libs/${plugin}/src/generators/${plugin}/schema.d.ts`,
`dist/libs/${plugin}/src/generators/${plugin}/generator.js`,
`dist/libs/${plugin}/src/generators/${plugin}/files/src/index.ts__template__`,
`dist/libs/${plugin}/src/executors/build/executor.js`,
`dist/libs/${plugin}/src/executors/build/schema.d.ts`,
`dist/libs/${plugin}/src/executors/build/schema.json`
);
const project = readJson(`libs/${plugin}/project.json`);
expect(project).toMatchObject({
tags: [],
});
const e2eProject = readJson(`apps/${plugin}-e2e/project.json`);
expect(e2eProject).toMatchObject({
tags: [],
implicitDependencies: [`${plugin}`],
});
}, 90000);
// the test invoke ensureNxProject, which points to @nrwl/workspace collection
// which walks up the directory to find it in the next repo itself, so it
// doesn't use the collection we are building
// we should change it to point to the right collection using relative path
it(`should run the plugin's e2e tests`, async () => {
const plugin = uniq('plugin-name');
runCLI(`generate @nrwl/nx-plugin:plugin ${plugin} --linter=eslint`);
if (isNotWindows()) {
const e2eResults = runCLI(`e2e ${plugin}-e2e`);
expect(e2eResults).toContain('Successfully ran target e2e');
expect(await killPorts()).toBeTruthy();
}
}, 250000);
it('should be able to generate a migration', async () => {
const plugin = uniq('plugin');
const version = '1.0.0';
runCLI(`generate @nrwl/nx-plugin:plugin ${plugin} --linter=eslint`);
runCLI(
`generate @nrwl/nx-plugin:migration --project=${plugin} --version=${version} --packageJsonUpdates=false`
);
const lintResults = runCLI(`lint ${plugin}`);
expect(lintResults).toContain('All files pass linting.');
expectTestsPass(await runCLIAsync(`test ${plugin}`));
const buildResults = runCLI(`build ${plugin}`);
expect(buildResults).toContain('Done compiling TypeScript files');
checkFilesExist(
`dist/libs/${plugin}/src/migrations/update-${version}/update-${version}.js`,
`libs/${plugin}/src/migrations/update-${version}/update-${version}.ts`
);
const migrationsJson = readJson(`libs/${plugin}/migrations.json`);
expect(migrationsJson).toMatchObject({
generators: expect.objectContaining({
[`update-${version}`]: {
version,
description: `update-${version}`,
cli: `nx`,
implementation: `./src/migrations/update-${version}/update-${version}`,
},
}),
});
}, 90000);
it('should be able to generate a generator', async () => {
const plugin = uniq('plugin');
const generator = uniq('generator');
runCLI(`generate @nrwl/nx-plugin:plugin ${plugin} --linter=eslint`);
runCLI(
`generate @nrwl/nx-plugin:generator ${generator} --project=${plugin}`
);
const lintResults = runCLI(`lint ${plugin}`);
expect(lintResults).toContain('All files pass linting.');
expectTestsPass(await runCLIAsync(`test ${plugin}`));
const buildResults = runCLI(`build ${plugin}`);
expect(buildResults).toContain('Done compiling TypeScript files');
checkFilesExist(
`libs/${plugin}/src/generators/${generator}/schema.d.ts`,
`libs/${plugin}/src/generators/${generator}/schema.json`,
`libs/${plugin}/src/generators/${generator}/generator.ts`,
`libs/${plugin}/src/generators/${generator}/generator.spec.ts`,
`dist/libs/${plugin}/src/generators/${generator}/schema.d.ts`,
`dist/libs/${plugin}/src/generators/${generator}/schema.json`,
`dist/libs/${plugin}/src/generators/${generator}/generator.js`
);
const generatorJson = readJson(`libs/${plugin}/generators.json`);
expect(generatorJson).toMatchObject({
generators: expect.objectContaining({
[generator]: {
factory: `./src/generators/${generator}/generator`,
schema: `./src/generators/${generator}/schema.json`,
description: `${generator} generator`,
},
}),
});
}, 90000);
it('should be able to generate a executor', async () => {
const plugin = uniq('plugin');
const executor = uniq('executor');
runCLI(`generate @nrwl/nx-plugin:plugin ${plugin} --linter=eslint`);
runCLI(`generate @nrwl/nx-plugin:executor ${executor} --project=${plugin}`);
const lintResults = runCLI(`lint ${plugin}`);
expect(lintResults).toContain('All files pass linting.');
expectTestsPass(await runCLIAsync(`test ${plugin}`));
const buildResults = runCLI(`build ${plugin}`);
expect(buildResults).toContain('Done compiling TypeScript files');
checkFilesExist(
`libs/${plugin}/src/executors/${executor}/schema.d.ts`,
`libs/${plugin}/src/executors/${executor}/schema.json`,
`libs/${plugin}/src/executors/${executor}/executor.ts`,
`libs/${plugin}/src/executors/${executor}/executor.spec.ts`,
`dist/libs/${plugin}/src/executors/${executor}/schema.d.ts`,
`dist/libs/${plugin}/src/executors/${executor}/schema.json`,
`dist/libs/${plugin}/src/executors/${executor}/executor.js`
);
const executorsJson = readJson(`libs/${plugin}/executors.json`);
expect(executorsJson).toMatchObject({
executors: expect.objectContaining({
[executor]: {
implementation: `./src/executors/${executor}/executor`,
schema: `./src/executors/${executor}/schema.json`,
description: `${executor} executor`,
},
}),
});
}, 90000);
describe('local plugins', () => {
const plugin = uniq('plugin');
beforeEach(() => {
runCLI(`generate @nrwl/nx-plugin:plugin ${plugin} --linter=eslint`);
});
it('should be able to infer projects and targets', async () => {
// Cache workspace json, to test inference and restore afterwards
const workspaceJsonContents = readFile('workspace.json');
removeFile('workspace.json');
// Setup project inference + target inference
updateFile(
`libs/${plugin}/src/index.ts`,
`import {basename} from 'path'
export function registerProjectTargets(f) {
if (basename(f) === 'my-project-file') {
return {
build: {
executor: "@nrwl/workspace:run-commands",
options: {
command: "echo 'custom registered target'"
}
}
}
}
}
export const projectFilePatterns = ['my-project-file'];
`
);
// Register plugin in nx.json (required for inference)
updateFile(`nx.json`, (nxJson) => {
const nx = JSON.parse(nxJson);
nx.plugins = [`@${npmScope}/${plugin}`];
return JSON.stringify(nx, null, 2);
});
// Create project that should be inferred by Nx
const inferredProject = uniq('inferred');
createFile(`libs/${inferredProject}/my-project-file`);
// Attempt to use inferred project w/ Nx
expect(runCLI(`build ${inferredProject}`)).toContain(
'custom registered target'
);
// Restore workspace.json
createFile('workspace.json', workspaceJsonContents);
});
it('should be able to use local generators and executors', async () => {
const generator = uniq('generator');
const generatedProject = uniq('project');
runCLI(
`generate @nrwl/nx-plugin:generator ${generator} --project=${plugin}`
);
runCLI(
`generate @${npmScope}/${plugin}:${generator} --name ${generatedProject}`
);
expect(() => checkFilesExist(`libs/${generatedProject}`)).not.toThrow();
expect(() => runCLI(`build ${generatedProject}`)).not.toThrow();
});
});
describe('--directory', () => {
it('should create a plugin in the specified directory', () => {
const plugin = uniq('plugin');
runCLI(
`generate @nrwl/nx-plugin:plugin ${plugin} --linter=eslint --directory subdir `
);
checkFilesExist(`libs/subdir/${plugin}/package.json`);
const pluginProject = readProjectConfig(`subdir-${plugin}`);
const pluginE2EProject = readProjectConfig(`subdir-${plugin}-e2e`);
expect(pluginProject.root).toBe(`libs/subdir/${plugin}`);
expect(pluginE2EProject).toBeTruthy();
}, 90000);
});
describe('--tags', () => {
it('should add tags to workspace.json', async () => {
const plugin = uniq('plugin');
runCLI(
`generate @nrwl/nx-plugin:plugin ${plugin} --linter=eslint --tags=e2etag,e2ePackage `
);
const pluginProject = readProjectConfig(plugin);
expect(pluginProject.tags).toEqual(['e2etag', 'e2ePackage']);
}, 90000);
});
});