diff --git a/docs/generated/packages/jest.json b/docs/generated/packages/jest.json index cad6856d52..a0f3802cca 100644 --- a/docs/generated/packages/jest.json +++ b/docs/generated/packages/jest.json @@ -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": [], @@ -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": [], diff --git a/e2e/jest/src/jest-root.test.ts b/e2e/jest/src/jest-root.test.ts new file mode 100644 index 0000000000..6cb9cb9fdc --- /dev/null +++ b/e2e/jest/src/jest-root.test.ts @@ -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); + }); +}); diff --git a/e2e/jest/src/jest.test.ts b/e2e/jest/src/jest.test.ts index e9bbd87fdd..be7a96f08a 100644 --- a/e2e/jest/src/jest.test.ts +++ b/e2e/jest/src/jest.test.ts @@ -4,9 +4,12 @@ import { runCLI, runCLIAsync, uniq, + readJson, updateFile, expectJestTestsToPass, cleanupProject, + readFile, + checkFilesExist, } from '@nrwl/e2e/utils'; describe('Jest', () => { diff --git a/e2e/linter/src/linter.test.ts b/e2e/linter/src/linter.test.ts index 163a8d65cd..b1297ccc6d 100644 --- a/e2e/linter/src/linter.test.ts +++ b/e2e/linter/src/linter.test.ts @@ -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(); diff --git a/packages/angular/src/generators/application/lib/add-unit-test-runner.ts b/packages/angular/src/generators/application/lib/add-unit-test-runner.ts index 9fbf2f821d..c22480b088 100644 --- a/packages/angular/src/generators/application/lib/add-unit-test-runner.ts +++ b/packages/angular/src/generators/application/lib/add-unit-test-runner.ts @@ -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, { diff --git a/packages/jest/index.ts b/packages/jest/index.ts index 498b209d7d..7f78752ba1 100644 --- a/packages/jest/index.ts +++ b/packages/jest/index.ts @@ -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'; diff --git a/packages/jest/src/generators/init/init.spec.ts b/packages/jest/src/generators/init/init.spec.ts index a23e8e300f..a3b7b63af7 100644 --- a/packages/jest/src/generators/init/init.spec.ts +++ b/packages/jest/src/generators/init/init.spec.ts @@ -1,6 +1,8 @@ import { + addProjectConfiguration, NxJsonConfiguration, readJson, + readProjectConfiguration, stripIndents, Tree, updateJson, @@ -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 () => { @@ -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: '/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: '/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', { diff --git a/packages/jest/src/generators/init/init.ts b/packages/jest/src/generators/init/init.ts index 87692a383b..9e616c906b 100644 --- a/packages/jest/src/generators/init/init.ts +++ b/packages/jest/src/generators/init/init.ts @@ -8,7 +8,10 @@ import { Tree, updateJson, updateWorkspaceConfiguration, + updateProjectConfiguration, + getProjects, } from '@nrwl/devkit'; +import { findRootJestConfig } from '../../utils/config/find-root-jest-files'; import { babelJestVersion, jestTypesVersion, @@ -18,6 +21,7 @@ import { tsJestVersion, tslibVersion, tsNodeVersion, + typesNodeVersion, } from '../../utils/versions'; import { JestInitSchema } from './schema'; @@ -26,39 +30,81 @@ interface NormalizedSchema extends ReturnType {} const schemaDefaults = { compiler: 'tsc', js: false, + rootProject: false, } as const; -function createJestConfig(tree: Tree, js: boolean = false) { - // if the root ts config already exists then don't make a js one or vice versa - if (!tree.exists('jest.config.ts') && !tree.exists('jest.config.js')) { - const contents = js - ? stripIndents` - const { getJestProjects } = require('@nrwl/jest'); +function generateGlobalConfig(tree: Tree, isJS: boolean) { + const contents = isJS + ? stripIndents` + const { getJestProjects } = require('@nrwl/jest'); - module.exports = { - projects: getJestProjects() - };` - : stripIndents` - import { getJestProjects } from '@nrwl/jest'; + module.exports = { + projects: getJestProjects() + };` + : stripIndents` + import { getJestProjects } from '@nrwl/jest'; - export default { - projects: getJestProjects() - };`; - tree.write(`jest.config.${js ? 'js' : 'ts'}`, contents); - } + export default { + projects: getJestProjects() + };`; + tree.write(`jest.config.${isJS ? 'js' : 'ts'}`, contents); +} +function createJestConfig(tree: Tree, options: NormalizedSchema) { if (!tree.exists('jest.preset.js')) { // preset is always js file. tree.write( `jest.preset.js`, ` const nxPreset = require('@nrwl/jest/preset').default; - + module.exports = { ...nxPreset }` ); addTestInputs(tree); } + if (options.rootProject) { + // we don't want any config to be made because the `jestProjectGenerator` + // will copy the template config file + return; + } + const rootJestPath = findRootJestConfig(tree); + if (!rootJestPath) { + // if there's not root jest config, we will create one and return + // this can happen when: + // - root jest config was renamed => in which case there is migration needed + // - root project didn't have jest setup => again, no migration is needed + generateGlobalConfig(tree, options.js); + return; + } + + if (tree.exists(rootJestPath)) { + // moving from root project config to monorepo-style config + const projects = getProjects(tree); + const projectNames = Array.from(projects.keys()); + const rootProject = projectNames.find( + (projectName) => projects.get(projectName)?.root === '.' + ); + // root project might have been removed, + // if it's missing there's nothing to migrate + if (rootProject) { + const rootProjectConfig = projects.get(rootProject); + const jestTarget = Object.values(rootProjectConfig.targets || {}).find( + (t) => t?.executor === '@nrwl/jest:jest' + ); + const isProjectConfig = jestTarget?.options?.jestConfig === rootJestPath; + // if root project doesn't have jest target, there's nothing to migrate + if (isProjectConfig) { + const jestAppConfig = `jest.config.app.${options.js ? 'js' : 'ts'}`; + + tree.rename(rootJestPath, jestAppConfig); + jestTarget.options.jestConfig = jestAppConfig; + updateProjectConfiguration(tree, rootProject, rootProjectConfig); + } + // generate new global config as it was move to project config or is missing + generateGlobalConfig(tree, options.js); + } + } } function addTestInputs(tree: Tree) { @@ -113,7 +159,7 @@ function updateDependencies(tree: Tree, options: NormalizedSchema) { if (!options.js) { devDeps['ts-node'] = tsNodeVersion; devDeps['@types/jest'] = jestTypesVersion; - devDeps['@types/node'] = '16.11.7'; + devDeps['@types/node'] = typesNodeVersion; } if (options.compiler === 'babel' || options.babelJest) { @@ -144,7 +190,7 @@ function updateExtensions(host: Tree) { export function jestInitGenerator(tree: Tree, schema: JestInitSchema) { const options = normalizeOptions(schema); - createJestConfig(tree, options.js); + createJestConfig(tree, options); let installTask: GeneratorCallback = () => {}; if (!options.skipPackageJson) { diff --git a/packages/jest/src/generators/init/schema.d.ts b/packages/jest/src/generators/init/schema.d.ts index 355b74b1d8..5e897dfd22 100644 --- a/packages/jest/src/generators/init/schema.d.ts +++ b/packages/jest/src/generators/init/schema.d.ts @@ -6,4 +6,5 @@ export interface JestInitSchema { * @deprecated */ babelJest?: boolean; + rootProject?: boolean; } diff --git a/packages/jest/src/generators/init/schema.json b/packages/jest/src/generators/init/schema.json index ba72f8215b..f5270335f7 100644 --- a/packages/jest/src/generators/init/schema.json +++ b/packages/jest/src/generators/init/schema.json @@ -21,6 +21,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": [] diff --git a/packages/jest/src/generators/jest-project/files-angular/jest.config.ts__tmpl__ b/packages/jest/src/generators/jest-project/files-angular/jest.config.ts__tmpl__ index 9effaf14a6..f70ea1660c 100644 --- a/packages/jest/src/generators/jest-project/files-angular/jest.config.ts__tmpl__ +++ b/packages/jest/src/generators/jest-project/files-angular/jest.config.ts__tmpl__ @@ -19,5 +19,9 @@ 'jest-preset-angular/build/serializers/no-ng-attributes', 'jest-preset-angular/build/serializers/ng-snapshot', 'jest-preset-angular/build/serializers/html-comment', - ]<% } %> + ]<% } %><% if(rootProject){ %>, + testMatch: [ + '/src/**/__tests__/**/*.[jt]s?(x)', + '/src/**/?(*.)+(spec|test).[jt]s?(x)', + ],<% } %> }; diff --git a/packages/jest/src/generators/jest-project/files/jest.config.ts__tmpl__ b/packages/jest/src/generators/jest-project/files/jest.config.ts__tmpl__ index f40d589669..e31e1abb6c 100644 --- a/packages/jest/src/generators/jest-project/files/jest.config.ts__tmpl__ +++ b/packages/jest/src/generators/jest-project/files/jest.config.ts__tmpl__ @@ -13,5 +13,9 @@ <% if (supportTsx){ %>'^.+\\.[tj]sx?$'<% } else { %>'^.+\\.[tj]s$'<% } %>: <% if (supportTsx && transformer === '@swc/jest') { %>['<%= transformer %>', { jsc: { transform: { react: { runtime: 'automatic' } } } }]<% } else { %>'<%= transformer %>'<% } %> }, <% if (supportTsx) { %>moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],<% } else { %>moduleFileExtensions: ['ts', 'js', 'html'],<% } %><% } %> - coverageDirectory: '<%= offsetFromRoot %>coverage/<%= projectRoot %>' + coverageDirectory: '<%= offsetFromRoot %>coverage/<%= projectRoot %>'<% if(rootProject){ %>, + testMatch: [ + '/src/**/__tests__/**/*.[jt]s?(x)', + '/src/**/?(*.)+(spec|test).[jt]s?(x)', + ],<% } %> }; diff --git a/packages/jest/src/generators/jest-project/jest-project.spec.ts b/packages/jest/src/generators/jest-project/jest-project.spec.ts index 8c3e240066..aec19a6ea6 100644 --- a/packages/jest/src/generators/jest-project/jest-project.spec.ts +++ b/packages/jest/src/generators/jest-project/jest-project.spec.ts @@ -362,4 +362,81 @@ describe('jestProject', () => { expect(tree.read('libs/lib1/jest.config.ts', 'utf-8')).toMatchSnapshot(); }); }); + + describe('root project', () => { + it('root jest.config.ts should be project config', async () => { + writeJson(tree, 'tsconfig.json', { + files: [], + include: [], + references: [], + }); + addProjectConfiguration(tree, 'my-project', { + root: '', + sourceRoot: 'src', + name: 'my-project', + targets: {}, + }); + await jestProjectGenerator(tree, { + ...defaultOptions, + project: 'my-project', + rootProject: true, + }); + expect(tree.read('jest.config.ts', 'utf-8')).toMatchInlineSnapshot(` + "/* eslint-disable */ + export default { + displayName: 'my-project', + preset: '../jest.preset.js', + globals: { + 'ts-jest': { + tsconfig: '/tsconfig.spec.json', + } + }, + coverageDirectory: '../coverage/my-project', + testMatch: [ + '/src/**/__tests__/**/*.[jt]s?(x)', + '/src/**/?(*.)+(spec|test).[jt]s?(x)', + ], + }; + " + `); + }); + + it('root jest.config.js should be project config', async () => { + writeJson(tree, 'tsconfig.json', { + files: [], + include: [], + references: [], + }); + addProjectConfiguration(tree, 'my-project', { + root: '', + sourceRoot: 'src', + name: 'my-project', + targets: {}, + }); + await jestProjectGenerator(tree, { + ...defaultOptions, + project: 'my-project', + rootProject: true, + js: true, + }); + expect(tree.read('jest.config.js', 'utf-8')).toMatchInlineSnapshot(` + "/* eslint-disable */ + module.exports = { + displayName: 'my-project', + preset: '../jest.preset.js', + globals: { + 'ts-jest': { + tsconfig: '/tsconfig.spec.json', + } + }, + coverageDirectory: '../coverage/my-project', + testMatch: [ + '/src/**/__tests__/**/*.[jt]s?(x)', + '/src/**/?(*.)+(spec|test).[jt]s?(x)', + ], + }; + " + `); + }); + }); }); diff --git a/packages/jest/src/generators/jest-project/jest-project.ts b/packages/jest/src/generators/jest-project/jest-project.ts index f65c5ced47..8147e89971 100644 --- a/packages/jest/src/generators/jest-project/jest-project.ts +++ b/packages/jest/src/generators/jest-project/jest-project.ts @@ -13,6 +13,7 @@ const schemaDefaults = { supportTsx: false, skipSetupFile: false, skipSerializers: false, + rootProject: false, } as const; function normalizeOptions(options: JestProjectSchema) { @@ -42,7 +43,6 @@ function normalizeOptions(options: JestProjectSchema) { // setupFile is always 'none' options.setupFile = schemaDefaults.setupFile; - return { ...schemaDefaults, ...options, @@ -55,11 +55,12 @@ export async function jestProjectGenerator( ) { const options = normalizeOptions(schema); const installTask = init(tree, options); + checkForTestTarget(tree, options); createFiles(tree, options); updateTsConfig(tree, options); updateWorkspace(tree, options); - updateJestConfig(tree, options); + if (!schema.skipFormat) { await formatFiles(tree); } diff --git a/packages/jest/src/generators/jest-project/lib/check-for-test-target.ts b/packages/jest/src/generators/jest-project/lib/check-for-test-target.ts index e24793c596..a1f4269a6f 100644 --- a/packages/jest/src/generators/jest-project/lib/check-for-test-target.ts +++ b/packages/jest/src/generators/jest-project/lib/check-for-test-target.ts @@ -3,7 +3,7 @@ import { JestProjectSchema } from '../schema'; export function checkForTestTarget(tree: Tree, options: JestProjectSchema) { const projectConfig = readProjectConfiguration(tree, options.project); - if (projectConfig.targets.test) { - throw new Error(`${options.project}: already has a test architect option.`); + if (projectConfig?.targets?.test) { + throw new Error(`${options.project}: already has a test target set.`); } } diff --git a/packages/jest/src/generators/jest-project/lib/create-files.ts b/packages/jest/src/generators/jest-project/lib/create-files.ts index 930aa4f92a..a731c42a20 100644 --- a/packages/jest/src/generators/jest-project/lib/create-files.ts +++ b/packages/jest/src/generators/jest-project/lib/create-files.ts @@ -27,7 +27,8 @@ export function createFiles(tree: Tree, options: JestProjectSchema) { ...options, transformer, js: !!options.js, - projectRoot: projectConfig.root, + rootProject: options.rootProject, + projectRoot: options.rootProject ? options.project : projectConfig.root, offsetFromRoot: offsetFromRoot(projectConfig.root), }); diff --git a/packages/jest/src/generators/jest-project/lib/update-jestconfig.ts b/packages/jest/src/generators/jest-project/lib/update-jestconfig.ts index 660b244dc6..f04769928d 100644 --- a/packages/jest/src/generators/jest-project/lib/update-jestconfig.ts +++ b/packages/jest/src/generators/jest-project/lib/update-jestconfig.ts @@ -4,10 +4,10 @@ import { addPropertyToJestConfig } from '../../../utils/config/update-config'; import { readProjectConfiguration, Tree } from '@nrwl/devkit'; function isUsingUtilityFunction(host: Tree) { - return host - .read(findRootJestConfig(host)) - .toString() - .includes('getJestProjects()'); + const rootConfig = findRootJestConfig(host); + return ( + rootConfig && host.read(rootConfig).toString().includes('getJestProjects()') + ); } export function updateJestConfig(host: Tree, options: JestProjectSchema) { @@ -15,10 +15,13 @@ export function updateJestConfig(host: Tree, options: JestProjectSchema) { return; } const project = readProjectConfiguration(host, options.project); - addPropertyToJestConfig( - host, - findRootJestConfig(host), - 'projects', - `/${project.root}` - ); + const rootConfig = findRootJestConfig(host); + if (rootConfig) { + addPropertyToJestConfig( + host, + findRootJestConfig(host), + 'projects', + `/${project.root}` + ); + } } diff --git a/packages/jest/src/generators/jest-project/schema.d.ts b/packages/jest/src/generators/jest-project/schema.d.ts index b503aa7d13..f82c10e4bf 100644 --- a/packages/jest/src/generators/jest-project/schema.d.ts +++ b/packages/jest/src/generators/jest-project/schema.d.ts @@ -16,4 +16,5 @@ export interface JestProjectSchema { compiler?: 'tsc' | 'babel' | 'swc'; skipPackageJson?: boolean; js?: boolean; + rootProject?: boolean; } diff --git a/packages/jest/src/generators/jest-project/schema.json b/packages/jest/src/generators/jest-project/schema.json index 6fbca26a1a..372df38d6d 100644 --- a/packages/jest/src/generators/jest-project/schema.json +++ b/packages/jest/src/generators/jest-project/schema.json @@ -68,6 +68,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": [] diff --git a/packages/jest/src/utils/config/get-jest-projects.ts b/packages/jest/src/utils/config/get-jest-projects.ts index 99941ef0a8..281d3b5cf1 100644 --- a/packages/jest/src/utils/config/get-jest-projects.ts +++ b/packages/jest/src/utils/config/get-jest-projects.ts @@ -41,3 +41,17 @@ export function getJestProjects() { } return Array.from(jestConfigurationSet); } + +/** + * a list of nested projects that have jest configured + * to be used in the testPathIgnorePatterns property of a given jest config + * https://jestjs.io/docs/configuration#testpathignorepatterns-arraystring + * */ +export function getNestedJestProjects() { + // TODO(caleb): get current project path and list of all projects and their rootDir + // return a list of all projects that are nested in the current projects path + // always include node_modules as that's the default + + const allProjects = getJestProjects(); + return ['/node_modules/']; +} diff --git a/packages/react/src/generators/application/lib/add-jest.ts b/packages/react/src/generators/application/lib/add-jest.ts index cd18cce87b..ff23ea0af4 100644 --- a/packages/react/src/generators/application/lib/add-jest.ts +++ b/packages/react/src/generators/application/lib/add-jest.ts @@ -14,5 +14,6 @@ export async function addJest(host: Tree, options: NormalizedSchema) { skipSerializers: true, setupFile: 'none', compiler: options.compiler, + rootProject: options.rootProject, }); } diff --git a/packages/react/src/generators/init/init.ts b/packages/react/src/generators/init/init.ts index 9b131b4b53..993bc93610 100755 --- a/packages/react/src/generators/init/init.ts +++ b/packages/react/src/generators/init/init.ts @@ -8,7 +8,6 @@ import { Tree, updateWorkspaceConfiguration, } from '@nrwl/devkit'; -import { jestInitGenerator } from '@nrwl/jest'; import { webInitGenerator } from '@nrwl/web'; import { runTasksInSerial } from '@nrwl/workspace/src/utilities/run-tasks-in-serial'; import { @@ -71,10 +70,6 @@ export async function reactInitGenerator(host: Tree, schema: InitSchema) { setDefault(host); - if (!schema.unitTestRunner || schema.unitTestRunner === 'jest') { - const jestTask = jestInitGenerator(host, schema); - tasks.push(jestTask); - } if (!schema.e2eTestRunner || schema.e2eTestRunner === 'cypress') { const cypressTask = cypressInitGenerator(host, {}); tasks.push(cypressTask); diff --git a/packages/workspace/src/generators/preset/preset.ts b/packages/workspace/src/generators/preset/preset.ts index 9afae31112..8c2918d49a 100644 --- a/packages/workspace/src/generators/preset/preset.ts +++ b/packages/workspace/src/generators/preset/preset.ts @@ -44,7 +44,6 @@ async function createPreset(tree: Tree, options: Schema) { name: options.name, style: options.style, linter: options.linter, - unitTestRunner: 'none', standaloneConfig: options.standaloneConfig, rootProject: true, }); @@ -68,7 +67,6 @@ async function createPreset(tree: Tree, options: Schema) { name: options.name, style: options.style, linter: options.linter, - unitTestRunner: 'none', standaloneConfig: options.standaloneConfig, rootProject: true, bundler: 'vite',