Skip to content

Commit

Permalink
feat(testing): support root project generation for jest (#13353)
Browse files Browse the repository at this point in the history
Co-authored-by: Miroslav Jonas <[email protected]>
  • Loading branch information
barbados-clemens and meeroslav authored Nov 29, 2022
1 parent 8f2fb24 commit 74bd0bb
Show file tree
Hide file tree
Showing 23 changed files with 416 additions and 51 deletions.
12 changes: 12 additions & 0 deletions docs/generated/packages/jest.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
"type": "boolean",
"default": false,
"description": "Use JavaScript instead of TypeScript for config files"
},
"rootProject": {
"description": "initialize Jest for an application at the root of the workspace",
"type": "boolean",
"default": false,
"hidden": true
}
},
"required": [],
Expand Down Expand Up @@ -123,6 +129,12 @@
"type": "boolean",
"default": false,
"description": "Use JavaScript instead of TypeScript for config files"
},
"rootProject": {
"description": "Add Jest to an application at the root of the workspace",
"type": "boolean",
"default": false,
"hidden": true
}
},
"required": [],
Expand Down
73 changes: 73 additions & 0 deletions e2e/jest/src/jest-root.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { newProject, runCLI, uniq, runCLIAsync } from '@nrwl/e2e/utils';

describe('Jest root projects', () => {
const myapp = uniq('myapp');
const mylib = uniq('mylib');

describe('angular', () => {
beforeAll(() => {
newProject();
});

it('should test root level app projects', async () => {
runCLI(`generate @nrwl/angular:app ${myapp} --rootProject=true`);

const rootProjectTestResults = await runCLIAsync(`test ${myapp}`);

expect(rootProjectTestResults.combinedOutput).toContain(
'Test Suites: 1 passed, 1 total'
);
}, 300_000);

it('should add lib project and tests should still work', async () => {
runCLI(`generate @nrwl/angular:lib ${mylib}`);
runCLI(
`generate @nrwl/angular:component ${mylib} --export --standalone --project=${mylib} --no-interactive`
);

const libProjectTestResults = await runCLIAsync(`test ${mylib}`);

expect(libProjectTestResults.combinedOutput).toContain(
'Test Suites: 1 passed, 1 total'
);

const rootProjectTestResults = await runCLIAsync(`test ${myapp}`);

expect(rootProjectTestResults.combinedOutput).toContain(
'Test Suites: 1 passed, 1 total'
);
}, 300_000);
});

describe('react', () => {
beforeAll(() => {
newProject();
});

it('should test root level app projects', async () => {
runCLI(`generate @nrwl/react:app ${myapp} --rootProject=true`);

const rootProjectTestResults = await runCLIAsync(`test ${myapp}`);

expect(rootProjectTestResults.combinedOutput).toContain(
'Test Suites: 1 passed, 1 total'
);
}, 300_000);

it('should add lib project and tests should still work', async () => {
runCLI(`generate @nrwl/react:lib ${mylib}`);

const libProjectTestResults = await runCLIAsync(`test ${mylib}`);

expect(libProjectTestResults.combinedOutput).toContain(
'Test Suites: 1 passed, 1 total'
);

const rootProjectTestResults = await runCLIAsync(`test ${myapp}`);

expect(rootProjectTestResults.combinedOutput).toContain(
'Test Suites: 1 passed, 1 total'
);
}, 300_000);
});
});
3 changes: 3 additions & 0 deletions e2e/jest/src/jest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import {
runCLI,
runCLIAsync,
uniq,
readJson,
updateFile,
expectJestTestsToPass,
cleanupProject,
readFile,
checkFilesExist,
} from '@nrwl/e2e/utils';

describe('Jest', () => {
Expand Down
3 changes: 0 additions & 3 deletions e2e/linter/src/linter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,9 +473,6 @@ export function tslibC(): string {
'plugin:@nrwl/nx/javascript',
]);

console.log(JSON.stringify(rootEslint, null, 2));
console.log(JSON.stringify(e2eEslint, null, 2));

runCLI(`generate @nrwl/react:lib ${mylib}`);
// should add new tslint
expect(() => checkFilesExist(`.eslintrc.base.json`)).not.toThrow();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export async function addUnitTestRunner(host: Tree, options: NormalizedSchema) {
supportTsx: false,
skipSerializers: false,
skipPackageJson: options.skipPackageJson,
rootProject: options.rootProject,
});
} else if (options.unitTestRunner === UnitTestRunner.Karma) {
await karmaProjectGenerator(host, {
Expand Down
5 changes: 4 additions & 1 deletion packages/jest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ export {
export { jestConfigObjectAst } from './src/utils/config/functions';
export { jestProjectGenerator } from './src/generators/jest-project/jest-project';
export { jestInitGenerator } from './src/generators/init/init';
export { getJestProjects } from './src/utils/config/get-jest-projects';
export {
getJestProjects,
getNestedJestProjects,
} from './src/utils/config/get-jest-projects';
122 changes: 120 additions & 2 deletions packages/jest/src/generators/init/init.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {
addProjectConfiguration,
NxJsonConfiguration,
readJson,
readProjectConfiguration,
stripIndents,
Tree,
updateJson,
Expand Down Expand Up @@ -41,9 +43,29 @@ describe('jest', () => {
});

it('should not override existing files', async () => {
tree.write('jest.config.ts', `test`);
addProjectConfiguration(tree, 'my-project', {
root: 'apps/my-app',
name: 'my-app',
sourceRoot: 'apps/my-app/src',
targets: {
test: {
executor: '@nrwl/jest:jest',
options: {
jestConfig: 'apps/my-app/jest.config.ts',
},
},
},
});
const expected = stripIndents`
import { getJestProjects } from '@nrwl/jest';
export default {
projects: getJestProjects(),
extraThing: "Goes Here"
}
`;
tree.write('jest.config.ts', expected);
jestInitGenerator(tree, {});
expect(tree.read('jest.config.ts', 'utf-8')).toEqual('test');
expect(tree.read('jest.config.ts', 'utf-8')).toEqual(expected);
});

it('should add target defaults for test', async () => {
Expand Down Expand Up @@ -144,6 +166,102 @@ describe('jest', () => {
});
});

describe('root project', () => {
it('should not add a monorepo jest.config.ts to the project', () => {
jestInitGenerator(tree, { rootProject: true });
expect(tree.exists('jest.config.ts')).toBeFalsy();
});

it('should rename the project jest.config.ts to project jest config', () => {
addProjectConfiguration(tree, 'my-project', {
root: '.',
name: 'my-project',
sourceRoot: 'src',
targets: {
test: {
executor: '@nrwl/jest:jest',
options: {
jestConfig: 'jest.config.ts',
},
},
},
});
tree.write(
'jest.config.ts',
`
/* eslint-disable */
export default {
transform: {
'^.+\\.[tj]sx?$': 'ts-jest',
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'],
globals: { 'ts-jest': { tsconfig: '<rootDir>/tsconfig.spec.json' } },
displayName: 'my-project',
testEnvironment: 'node',
preset: './jest.preset.js',
};
`
);
jestInitGenerator(tree, { rootProject: false });
expect(tree.exists('jest.config.app.ts')).toBeTruthy();
expect(tree.read('jest.config.ts', 'utf-8'))
.toEqual(`import { getJestProjects } from '@nrwl/jest';
export default {
projects: getJestProjects()
};`);
expect(readProjectConfiguration(tree, 'my-project').targets.test)
.toMatchInlineSnapshot(`
Object {
"executor": "@nrwl/jest:jest",
"options": Object {
"jestConfig": "jest.config.app.ts",
},
}
`);
});

it('should work with --js', () => {
addProjectConfiguration(tree, 'my-project', {
root: '.',
name: 'my-project',
sourceRoot: 'src',
targets: {
test: {
executor: '@nrwl/jest:jest',
options: {
jestConfig: 'jest.config.js',
},
},
},
});
tree.write(
'jest.config.js',
`
/* eslint-disable */
module.exports = {
transform: {
'^.+\\.[tj]sx?$': 'ts-jest',
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'],
globals: { 'ts-jest': { tsconfig: '<rootDir>/tsconfig.spec.json' } },
displayName: 'my-project',
testEnvironment: 'node',
preset: './jest.preset.js',
};
`
);
jestInitGenerator(tree, { js: true, rootProject: false });
expect(tree.exists('jest.config.app.js')).toBeTruthy();
expect(tree.read('jest.config.js', 'utf-8'))
.toEqual(`const { getJestProjects } = require('@nrwl/jest');
module.exports = {
projects: getJestProjects()
};`);
});
});

describe('adds jest extension', () => {
beforeEach(async () => {
writeJson(tree, '.vscode/extensions.json', {
Expand Down
Loading

1 comment on commit 74bd0bb

@vercel
Copy link

@vercel vercel bot commented on 74bd0bb Nov 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx.dev
nx-dev-nrwl.vercel.app
nx-five.vercel.app
nx-dev-git-master-nrwl.vercel.app

Please sign in to comment.