Skip to content

Commit df3314d

Browse files
committed
feat: run all app e2e tests when configuring new sub apps
When a new SubApp is added to a project the project is now configured to run all E2E tests by default. Closes #1167
1 parent 3dc496c commit df3314d

File tree

2 files changed

+98
-13
lines changed

2 files changed

+98
-13
lines changed

src/lib/sub-app/sub-app.factory.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ describe('SubApp Factory', () => {
2020
const files: string[] = tree.files;
2121
expect(files.sort()).toEqual([
2222
'/nest-cli.json',
23+
'/apps/jest-e2e.json',
2324
'/apps/nestjs-schematics/tsconfig.app.json',
2425
'/apps/project/tsconfig.app.json',
2526
'/apps/project/src/main.ts',
@@ -30,6 +31,13 @@ describe('SubApp Factory', () => {
3031
'/apps/project/test/app.e2e-spec.ts',
3132
'/apps/project/test/jest-e2e.json',
3233
].sort());
34+
35+
expect(JSON.parse(tree.readContent('/apps/jest-e2e.json'))).toMatchObject({
36+
projects: [
37+
'apps/nestjs-schematics/test/jest-e2e.json',
38+
'apps/project/test/jest-e2e.json',
39+
]
40+
});
3341
});
3442
it('should manage name to normalize', async () => {
3543
const options: SubAppOptions = {
@@ -41,6 +49,7 @@ describe('SubApp Factory', () => {
4149
const files: string[] = tree.files;
4250
expect(files.sort()).toEqual([
4351
'/nest-cli.json',
52+
'/apps/jest-e2e.json',
4453
'/apps/nestjs-schematics/tsconfig.app.json',
4554
'/apps/awesome-project/tsconfig.app.json',
4655
'/apps/awesome-project/src/main.ts',
@@ -51,6 +60,13 @@ describe('SubApp Factory', () => {
5160
'/apps/awesome-project/test/app.e2e-spec.ts',
5261
'/apps/awesome-project/test/jest-e2e.json',
5362
].sort());
63+
64+
expect(JSON.parse(tree.readContent('/apps/jest-e2e.json'))).toMatchObject({
65+
projects: [
66+
'apps/nestjs-schematics/test/jest-e2e.json',
67+
'apps/awesome-project/test/jest-e2e.json',
68+
]
69+
});
5470
});
5571
it("should keep underscores in sub-app's path and file name", async () => {
5672
const options: SubAppOptions = {
@@ -62,6 +78,7 @@ describe('SubApp Factory', () => {
6278
const files: string[] = tree.files;
6379
expect(files.sort()).toEqual([
6480
'/nest-cli.json',
81+
'/apps/jest-e2e.json',
6582
'/apps/nestjs-schematics/tsconfig.app.json',
6683
'/apps/_project/tsconfig.app.json',
6784
'/apps/_project/src/main.ts',
@@ -72,6 +89,13 @@ describe('SubApp Factory', () => {
7289
'/apps/_project/test/app.e2e-spec.ts',
7390
'/apps/_project/test/jest-e2e.json',
7491
].sort());
92+
93+
expect(JSON.parse(tree.readContent('/apps/jest-e2e.json'))).toMatchObject({
94+
projects: [
95+
'apps/nestjs-schematics/test/jest-e2e.json',
96+
'apps/_project/test/jest-e2e.json',
97+
]
98+
});
7599
});
76100
it('should manage javascript files', async () => {
77101
const options: SubAppOptions = {
@@ -84,6 +108,7 @@ describe('SubApp Factory', () => {
84108
const files: string[] = tree.files;
85109
expect(files.sort()).toEqual([
86110
'/nest-cli.json',
111+
'/apps/jest-e2e.json',
87112
'/apps/nestjs-schematics/.babelrc',
88113
'/apps/nestjs-schematics/index.js',
89114
'/apps/nestjs-schematics/jsconfig.json',
@@ -98,6 +123,13 @@ describe('SubApp Factory', () => {
98123
'/apps/project/test/app.e2e-spec.js',
99124
'/apps/project/test/jest-e2e.json',
100125
].sort());
126+
127+
expect(JSON.parse(tree.readContent('/apps/jest-e2e.json'))).toMatchObject({
128+
projects: [
129+
'apps/nestjs-schematics/test/jest-e2e.json',
130+
'apps/project/test/jest-e2e.json',
131+
]
132+
});
101133
});
102134
it('should generate spec files with custom suffix', async () => {
103135
const options: SubAppOptions = {
@@ -110,6 +142,7 @@ describe('SubApp Factory', () => {
110142
const files: string[] = tree.files;
111143
expect(files.sort()).toEqual([
112144
'/nest-cli.json',
145+
'/apps/jest-e2e.json',
113146
'/apps/nestjs-schematics/tsconfig.app.json',
114147
'/apps/project/tsconfig.app.json',
115148
'/apps/project/src/main.ts',
@@ -120,5 +153,12 @@ describe('SubApp Factory', () => {
120153
'/apps/project/test/jest-e2e.json',
121154
'/apps/project/test/app.e2e-test.ts',
122155
].sort());
156+
157+
expect(JSON.parse(tree.readContent('/apps/jest-e2e.json'))).toMatchObject({
158+
projects: [
159+
'apps/nestjs-schematics/test/jest-e2e.json',
160+
'apps/project/test/jest-e2e.json',
161+
]
162+
});
123163
});
124164
});

src/lib/sub-app/sub-app.factory.ts

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export function main(options: SubAppOptions): Rule {
4343
options = transform(options);
4444
return chain([
4545
updateTsConfig(),
46-
updatePackageJson(options, appName),
46+
updatePackageJson(options),
4747
(tree, context) =>
4848
isMonorepo(tree)
4949
? noop()(tree, context)
@@ -52,6 +52,7 @@ export function main(options: SubAppOptions): Rule {
5252
moveDefaultAppToApps(options.path, appName, options.sourceRoot),
5353
])(tree, context),
5454
addAppsToCliOptions(options.path, options.name, appName),
55+
addAppsToE2eOptions(options.path, options.name, appName),
5556
branchAndMerge(mergeWith(generate(options))),
5657
]);
5758
}
@@ -145,7 +146,7 @@ function updateTsConfig() {
145146
};
146147
}
147148

148-
function updatePackageJson(options: SubAppOptions, defaultAppName: string) {
149+
function updatePackageJson(options: SubAppOptions) {
149150
return (host: Tree) => {
150151
if (!host.exists('package.json')) {
151152
return host;
@@ -154,7 +155,7 @@ function updatePackageJson(options: SubAppOptions, defaultAppName: string) {
154155
host,
155156
'package.json',
156157
(packageJson: Record<string, any>) => {
157-
updateNpmScripts(packageJson.scripts, options, defaultAppName);
158+
updateNpmScripts(packageJson.scripts, options);
158159
updateJestOptions(packageJson.jest, options);
159160
},
160161
);
@@ -164,7 +165,6 @@ function updatePackageJson(options: SubAppOptions, defaultAppName: string) {
164165
function updateNpmScripts(
165166
scripts: Record<string, any>,
166167
options: SubAppOptions,
167-
defaultAppName: string,
168168
) {
169169
if (!scripts) {
170170
return;
@@ -178,15 +178,11 @@ function updateNpmScripts(
178178
scripts[defaultTestScriptName] &&
179179
scripts[defaultTestScriptName].indexOf(options.path as string) < 0
180180
) {
181-
const defaultTestDir = 'test';
182-
const newTestDir = join(
183-
options.path as Path,
184-
defaultAppName,
185-
defaultTestDir,
186-
);
187-
scripts[defaultTestScriptName] = (
188-
scripts[defaultTestScriptName] as string
189-
).replace(defaultTestDir, newTestDir);
181+
const defaultSourceRoot =
182+
options.rootDir !== undefined ? options.rootDir : DEFAULT_APPS_PATH;
183+
scripts[
184+
defaultTestScriptName
185+
] = `jest --config ${defaultSourceRoot}/jest-e2e.json`;
190186
}
191187
if (
192188
scripts[defaultFormatScriptName] &&
@@ -299,6 +295,40 @@ function addAppsToCliOptions(
299295
};
300296
}
301297

298+
function addAppsToE2eOptions(
299+
projectRoot: string,
300+
projectName: string,
301+
appName: string,
302+
): Rule {
303+
return (host: Tree) => {
304+
const rootE2eFileExists = host.exists('apps/jest-e2e.json');
305+
if (!rootE2eFileExists) {
306+
host.create('apps/jest-e2e.json', '{}');
307+
}
308+
309+
return updateJsonFile(
310+
host,
311+
'apps/jest-e2e.json',
312+
(optionsFile: Record<string, any>) => {
313+
updateMainAppE2eOptions(optionsFile, projectRoot, appName);
314+
if (!optionsFile.projects) {
315+
optionsFile.projects = [] as string[];
316+
}
317+
318+
for (const project of optionsFile.projects as string) {
319+
if (project.includes(`apps/${projectName}/test/jest-e2e.json`)) {
320+
throw new SchematicsException(
321+
`Project "${projectName}" exists in the root E2E config already.`,
322+
);
323+
}
324+
}
325+
326+
optionsFile.projects.push(`apps/${projectName}/test/jest-e2e.json`);
327+
},
328+
);
329+
}
330+
}
331+
302332
function updateMainAppOptions(
303333
optionsFile: Record<string, any>,
304334
projectRoot: string,
@@ -337,6 +367,21 @@ function updateMainAppOptions(
337367
};
338368
}
339369

370+
function updateMainAppE2eOptions(
371+
optionsFile: Record<string, any>,
372+
projectRoot: string,
373+
appName: string,
374+
) {
375+
if (optionsFile.projects) {
376+
return;
377+
}
378+
const rootFilePath = join(projectRoot as Path, appName);
379+
const e2ePath = join(rootFilePath, 'test/jest-e2e.json');
380+
381+
optionsFile.projects = [] as string[];
382+
optionsFile.projects.push(e2ePath);
383+
}
384+
340385
function generateWorkspace(options: SubAppOptions, appName: string): Source {
341386
const path = join(options.path as Path, appName);
342387
return apply(url(join('./workspace' as Path, options.language)), [

0 commit comments

Comments
 (0)