Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: run all app e2e tests when configuring new sub apps #1168

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 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',
'/jest-e2e.json',
'/apps/nestjs-schematics/tsconfig.app.json',
'/apps/project/tsconfig.app.json',
'/apps/project/src/main.ts',
Expand All @@ -30,6 +31,13 @@ describe('SubApp Factory', () => {
'/apps/project/test/app.e2e-spec.ts',
'/apps/project/test/jest-e2e.json',
].sort());

expect(JSON.parse(tree.readContent('/jest-e2e.json'))).toMatchObject({
projects: [
'apps/nestjs-schematics/test/jest-e2e.json',
'apps/project/test/jest-e2e.json',
]
});
});
it('should manage name to normalize', async () => {
const options: SubAppOptions = {
Expand All @@ -41,6 +49,7 @@ describe('SubApp Factory', () => {
const files: string[] = tree.files;
expect(files.sort()).toEqual([
'/nest-cli.json',
'/jest-e2e.json',
'/apps/nestjs-schematics/tsconfig.app.json',
'/apps/awesome-project/tsconfig.app.json',
'/apps/awesome-project/src/main.ts',
Expand All @@ -51,6 +60,13 @@ describe('SubApp Factory', () => {
'/apps/awesome-project/test/app.e2e-spec.ts',
'/apps/awesome-project/test/jest-e2e.json',
].sort());

expect(JSON.parse(tree.readContent('/jest-e2e.json'))).toMatchObject({
projects: [
'apps/nestjs-schematics/test/jest-e2e.json',
'apps/awesome-project/test/jest-e2e.json',
]
});
});
it("should keep underscores in sub-app's path and file name", async () => {
const options: SubAppOptions = {
Expand All @@ -62,6 +78,7 @@ describe('SubApp Factory', () => {
const files: string[] = tree.files;
expect(files.sort()).toEqual([
'/nest-cli.json',
'/jest-e2e.json',
'/apps/nestjs-schematics/tsconfig.app.json',
'/apps/_project/tsconfig.app.json',
'/apps/_project/src/main.ts',
Expand All @@ -72,6 +89,13 @@ describe('SubApp Factory', () => {
'/apps/_project/test/app.e2e-spec.ts',
'/apps/_project/test/jest-e2e.json',
].sort());

expect(JSON.parse(tree.readContent('/jest-e2e.json'))).toMatchObject({
projects: [
'apps/nestjs-schematics/test/jest-e2e.json',
'apps/_project/test/jest-e2e.json',
]
});
});
it('should manage javascript files', async () => {
const options: SubAppOptions = {
Expand All @@ -84,6 +108,7 @@ describe('SubApp Factory', () => {
const files: string[] = tree.files;
expect(files.sort()).toEqual([
'/nest-cli.json',
'/jest-e2e.json',
'/apps/nestjs-schematics/.babelrc',
'/apps/nestjs-schematics/index.js',
'/apps/nestjs-schematics/jsconfig.json',
Expand All @@ -98,6 +123,13 @@ describe('SubApp Factory', () => {
'/apps/project/test/app.e2e-spec.js',
'/apps/project/test/jest-e2e.json',
].sort());

expect(JSON.parse(tree.readContent('/jest-e2e.json'))).toMatchObject({
projects: [
'apps/nestjs-schematics/test/jest-e2e.json',
'apps/project/test/jest-e2e.json',
]
});
});
it('should generate spec files with custom suffix', async () => {
const options: SubAppOptions = {
Expand All @@ -110,6 +142,7 @@ describe('SubApp Factory', () => {
const files: string[] = tree.files;
expect(files.sort()).toEqual([
'/nest-cli.json',
'/jest-e2e.json',
'/apps/nestjs-schematics/tsconfig.app.json',
'/apps/project/tsconfig.app.json',
'/apps/project/src/main.ts',
Expand All @@ -120,5 +153,12 @@ describe('SubApp Factory', () => {
'/apps/project/test/jest-e2e.json',
'/apps/project/test/app.e2e-test.ts',
].sort());

expect(JSON.parse(tree.readContent('/jest-e2e.json'))).toMatchObject({
projects: [
'apps/nestjs-schematics/test/jest-e2e.json',
'apps/project/test/jest-e2e.json',
]
});
});
});
70 changes: 57 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,9 @@ 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);
scripts[
defaultTestScriptName
] = `jest --config jest-e2e.json`;
}
if (
scripts[defaultFormatScriptName] &&
Expand Down Expand Up @@ -299,6 +293,41 @@ function addAppsToCliOptions(
};
}

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

return updateJsonFile(
host,
rootE2eFilePath,
(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(`${projectRoot}/${projectName}/test/jest-e2e.json`)) {
throw new SchematicsException(
`Project "${projectName}" exists in the root E2E config already.`,
);
}
}

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

function updateMainAppOptions(
optionsFile: Record<string, any>,
projectRoot: string,
Expand Down Expand Up @@ -337,6 +366,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