Skip to content

Commit

Permalink
feat: run all app e2e tests when configuring new sub apps
Browse files Browse the repository at this point in the history
When a new SubApp is added to a project the project is now configured to run all
E2E tests by default.

Closes nestjs#1167
  • Loading branch information
MitchellCash committed Sep 7, 2022
1 parent 3dc496c commit e89fd9e
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 13 deletions.
34 changes: 34 additions & 0 deletions src/lib/sub-app/sub-app.factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('SubApp Factory', () => {
const files: string[] = tree.files;
expect(files.sort()).toEqual([
'/nest-cli.json',
'/apps/jest-e2e.json',
'/apps/nestjs-schematics/tsconfig.app.json',
'/apps/project/tsconfig.app.json',
'/apps/project/src/main.ts',
Expand All @@ -41,6 +42,7 @@ describe('SubApp Factory', () => {
const files: string[] = tree.files;
expect(files.sort()).toEqual([
'/nest-cli.json',
'/apps/jest-e2e.json',
'/apps/nestjs-schematics/tsconfig.app.json',
'/apps/awesome-project/tsconfig.app.json',
'/apps/awesome-project/src/main.ts',
Expand All @@ -62,6 +64,7 @@ describe('SubApp Factory', () => {
const files: string[] = tree.files;
expect(files.sort()).toEqual([
'/nest-cli.json',
'/apps/jest-e2e.json',
'/apps/nestjs-schematics/tsconfig.app.json',
'/apps/_project/tsconfig.app.json',
'/apps/_project/src/main.ts',
Expand All @@ -84,6 +87,7 @@ describe('SubApp Factory', () => {
const files: string[] = tree.files;
expect(files.sort()).toEqual([
'/nest-cli.json',
'/apps/jest-e2e.json',
'/apps/nestjs-schematics/.babelrc',
'/apps/nestjs-schematics/index.js',
'/apps/nestjs-schematics/jsconfig.json',
Expand All @@ -110,6 +114,7 @@ describe('SubApp Factory', () => {
const files: string[] = tree.files;
expect(files.sort()).toEqual([
'/nest-cli.json',
'/apps/jest-e2e.json',
'/apps/nestjs-schematics/tsconfig.app.json',
'/apps/project/tsconfig.app.json',
'/apps/project/src/main.ts',
Expand All @@ -121,4 +126,33 @@ describe('SubApp Factory', () => {
'/apps/project/test/app.e2e-test.ts',
].sort());
});
it('should generate root e2e file', async () => {
const options: SubAppOptions = {
name: 'project',
};
const tree: UnitTestTree = await runner
.runSchematicAsync('sub-app', options)
.toPromise();
const files: string[] = tree.files;
expect(files.sort()).toEqual([
'/nest-cli.json',
'/apps/jest-e2e.json',
'/apps/nestjs-schematics/tsconfig.app.json',
'/apps/project/tsconfig.app.json',
'/apps/project/src/main.ts',
'/apps/project/src/project.controller.spec.ts',
'/apps/project/src/project.controller.ts',
'/apps/project/src/project.module.ts',
'/apps/project/src/project.service.ts',
'/apps/project/test/app.e2e-spec.ts',
'/apps/project/test/jest-e2e.json',
].sort());

expect(JSON.parse(tree.readContent('/apps/jest-e2e.json'))).toMatchObject({
projects: [
'apps/nestjs-schematics/test/jest-e2e.json',
'apps/project/test/jest-e2e.json',
]
});
});
});
71 changes: 58 additions & 13 deletions src/lib/sub-app/sub-app.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function main(options: SubAppOptions): Rule {
options = transform(options);
return chain([
updateTsConfig(),
updatePackageJson(options, appName),
updatePackageJson(options),
(tree, context) =>
isMonorepo(tree)
? noop()(tree, context)
Expand All @@ -52,6 +52,7 @@ export function main(options: SubAppOptions): Rule {
moveDefaultAppToApps(options.path, appName, options.sourceRoot),
])(tree, context),
addAppsToCliOptions(options.path, options.name, appName),
addAppsToE2eOptions(options.path, options.name, appName),
branchAndMerge(mergeWith(generate(options))),
]);
}
Expand Down Expand Up @@ -145,7 +146,7 @@ function updateTsConfig() {
};
}

function updatePackageJson(options: SubAppOptions, defaultAppName: string) {
function updatePackageJson(options: SubAppOptions) {
return (host: Tree) => {
if (!host.exists('package.json')) {
return host;
Expand All @@ -154,7 +155,7 @@ function updatePackageJson(options: SubAppOptions, defaultAppName: string) {
host,
'package.json',
(packageJson: Record<string, any>) => {
updateNpmScripts(packageJson.scripts, options, defaultAppName);
updateNpmScripts(packageJson.scripts, options);
updateJestOptions(packageJson.jest, options);
},
);
Expand All @@ -164,7 +165,6 @@ function updatePackageJson(options: SubAppOptions, defaultAppName: string) {
function updateNpmScripts(
scripts: Record<string, any>,
options: SubAppOptions,
defaultAppName: string,
) {
if (!scripts) {
return;
Expand All @@ -178,15 +178,11 @@ function updateNpmScripts(
scripts[defaultTestScriptName] &&
scripts[defaultTestScriptName].indexOf(options.path as string) < 0
) {
const defaultTestDir = 'test';
const newTestDir = join(
options.path as Path,
defaultAppName,
defaultTestDir,
);
scripts[defaultTestScriptName] = (
scripts[defaultTestScriptName] as string
).replace(defaultTestDir, newTestDir);
const defaultSourceRoot =
options.rootDir !== undefined ? options.rootDir : DEFAULT_APPS_PATH;
scripts[
defaultTestScriptName
] = `jest --config ${defaultSourceRoot}/jest-e2e.json`;
}
if (
scripts[defaultFormatScriptName] &&
Expand Down Expand Up @@ -299,6 +295,40 @@ function addAppsToCliOptions(
};
}

function addAppsToE2eOptions(
projectRoot: string,
projectName: string,
appName: string,
): Rule {
return (host: Tree) => {
const rootE2eFileExists = host.exists('apps/jest-e2e.json');
if (!rootE2eFileExists) {
host.create('apps/jest-e2e.json', '{}');
}

return updateJsonFile(
host,
'apps/jest-e2e.json',
(optionsFile: Record<string, any>) => {
updateMainAppE2eOptions(optionsFile, projectRoot, appName);
if (!optionsFile.projects) {
optionsFile.projects = [] as string[];
}

for (const project of optionsFile.projects as string) {
if (project.includes(`apps/${projectName}/test/jest-e2e.json`)) {
throw new SchematicsException(
`Project "${projectName}" exists in the root E2E config already.`,
);
}
}

optionsFile.projects.push(`apps/${projectName}/test/jest-e2e.json`);
},
);
}
}

function updateMainAppOptions(
optionsFile: Record<string, any>,
projectRoot: string,
Expand Down Expand Up @@ -337,6 +367,21 @@ function updateMainAppOptions(
};
}

function updateMainAppE2eOptions(
optionsFile: Record<string, any>,
projectRoot: string,
appName: string,
) {
if (optionsFile.projects) {
return;
}
const rootFilePath = join(projectRoot as Path, appName);
const e2ePath = join(rootFilePath, 'test/jest-e2e.json');

optionsFile.projects = [] as string[];
optionsFile.projects.push(e2ePath);
}

function generateWorkspace(options: SubAppOptions, appName: string): Source {
const path = join(options.path as Path, appName);
return apply(url(join('./workspace' as Path, options.language)), [
Expand Down

0 comments on commit e89fd9e

Please sign in to comment.