From 88971468dbbed3fb5a0d86c6d37a0f558ac75573 Mon Sep 17 00:00:00 2001 From: Caleb Ukle Date: Fri, 22 Apr 2022 21:43:59 -0500 Subject: [PATCH] fix(testing): pass --js flag to jest generators (#9965) --- docs/generated/packages/next.json | 5 ++++ docs/generated/packages/node.json | 5 ++++ docs/generated/packages/react-native.json | 5 ++++ docs/generated/packages/react.json | 5 ++++ packages/jest/src/generators/init/init.ts | 10 +++++--- .../__snapshots__/jest-project.spec.ts.snap | 13 ++++++++++ .../jest-project/jest-project.spec.ts | 25 +++++++++++++++++++ .../jest-project/lib/create-files.ts | 3 ++- .../jest-project/lib/update-jestconfig.ts | 9 ++++--- .../jest-project/lib/update-workspace.ts | 2 +- .../update-jest-config-ext.spec.ts.snap | 8 +++--- .../update-jest-config-ext.spec.ts | 18 ++----------- .../src/utils/config/find-root-jest-files.ts | 25 +++++++++++++++++++ ....config.ts__tmpl__ => jest.config.__ext__} | 2 +- .../js/src/generators/library/library.spec.ts | 4 +-- packages/js/src/generators/library/library.ts | 6 ++--- .../generators/application/lib/add-jest.ts | 1 + .../application/lib/update-jest-config.ts | 4 ++- packages/next/src/generators/init/init.ts | 2 +- packages/next/src/generators/init/schema.d.ts | 1 + packages/next/src/generators/init/schema.json | 5 ++++ .../application/application.spec.ts | 4 +-- .../src/generators/application/application.ts | 1 + packages/node/src/generators/init/init.ts | 2 +- packages/node/src/generators/init/schema.d.ts | 1 + packages/node/src/generators/init/schema.json | 5 ++++ .../react-native/src/generators/init/init.ts | 2 +- .../src/generators/init/schema.d.ts | 1 + .../src/generators/init/schema.json | 5 ++++ packages/react-native/src/utils/add-jest.ts | 3 ++- .../generators/application/lib/add-jest.ts | 1 + .../application/lib/update-jest-config.ts | 4 ++- packages/react/src/generators/init/init.ts | 2 +- .../react/src/generators/init/schema.d.ts | 1 + .../react/src/generators/init/schema.json | 5 ++++ .../react/src/generators/library/library.ts | 1 + .../src/generators/library/library.spec.ts | 4 +-- .../src/generators/library/library.ts | 1 + 38 files changed, 154 insertions(+), 47 deletions(-) create mode 100644 packages/jest/src/utils/config/find-root-jest-files.ts rename packages/js/src/generators/library/files/jest-config/{jest.config.ts__tmpl__ => jest.config.__ext__} (89%) diff --git a/docs/generated/packages/next.json b/docs/generated/packages/next.json index 3ce08b61bb4e0..630e502970949 100644 --- a/docs/generated/packages/next.json +++ b/docs/generated/packages/next.json @@ -32,6 +32,11 @@ "description": "Skip formatting files.", "type": "boolean", "default": false + }, + "js": { + "type": "boolean", + "default": false, + "description": "Use JavaScript instead of TypeScript" } }, "required": [], diff --git a/docs/generated/packages/node.json b/docs/generated/packages/node.json index d95a25c213dba..d64cd6c8a5eb7 100644 --- a/docs/generated/packages/node.json +++ b/docs/generated/packages/node.json @@ -26,6 +26,11 @@ "description": "Skip formatting files.", "type": "boolean", "default": false + }, + "js": { + "type": "boolean", + "default": false, + "description": "Use JavaScript instead of TypeScript" } }, "required": [], diff --git a/docs/generated/packages/react-native.json b/docs/generated/packages/react-native.json index aa4af2db23ba2..8e5bab6e061a1 100644 --- a/docs/generated/packages/react-native.json +++ b/docs/generated/packages/react-native.json @@ -32,6 +32,11 @@ "type": "string", "enum": ["detox", "none"], "default": "detox" + }, + "js": { + "type": "boolean", + "default": false, + "description": "Use JavaScript instead of TypeScript" } }, "required": [], diff --git a/docs/generated/packages/react.json b/docs/generated/packages/react.json index 6fae9dc0f9bf6..405fb78394708 100644 --- a/docs/generated/packages/react.json +++ b/docs/generated/packages/react.json @@ -32,6 +32,11 @@ "description": "Skip formatting files.", "type": "boolean", "default": false + }, + "js": { + "type": "boolean", + "default": false, + "description": "Use JavaScript instead of TypeScript" } }, "required": [], diff --git a/packages/jest/src/generators/init/init.ts b/packages/jest/src/generators/init/init.ts index 55232bf693323..bb37ed9fe2fa3 100644 --- a/packages/jest/src/generators/init/init.ts +++ b/packages/jest/src/generators/init/init.ts @@ -27,7 +27,8 @@ const schemaDefaults = { } as const; function createJestConfig(host: Tree, js: boolean = false) { - if (!host.exists(`jest.config.${js ? 'js' : 'ts'}`)) { + // if the root ts config already exists then don't make a js one or vice versa + if (!host.exists('jest.config.ts') && !host.exists('jest.config.js')) { host.write( `jest.config.${js ? 'js' : 'ts'}`, stripIndents` @@ -39,7 +40,7 @@ function createJestConfig(host: Tree, js: boolean = false) { ); } - if (!host.exists(`jest.preset.${js ? 'js' : 'ts'}`)) { + if (!host.exists('jest.preset.ts') && !host.exists('jest.preset.js')) { host.write( `jest.preset.${js ? 'js' : 'ts'}`, ` @@ -57,8 +58,7 @@ function updateDependencies(tree: Tree, options: NormalizedSchema) { const devDeps = { '@nrwl/jest': nxVersion, jest: jestVersion, - '@types/jest': jestTypesVersion, - '@types/node': '16.11.7', + // because the default jest-preset uses ts-jest, // jest will throw an error if it's not installed // even if not using it in overriding transformers @@ -67,6 +67,8 @@ function updateDependencies(tree: Tree, options: NormalizedSchema) { if (!options.js) { devDeps['ts-node'] = tsNodeVersion; + devDeps['@types/jest'] = jestTypesVersion; + devDeps['@types/node'] = '16.11.7'; } if (options.compiler === 'babel' || options.babelJest) { diff --git a/packages/jest/src/generators/jest-project/__snapshots__/jest-project.spec.ts.snap b/packages/jest/src/generators/jest-project/__snapshots__/jest-project.spec.ts.snap index f14c67e9be05b..907fc146f418f 100644 --- a/packages/jest/src/generators/jest-project/__snapshots__/jest-project.spec.ts.snap +++ b/packages/jest/src/generators/jest-project/__snapshots__/jest-project.spec.ts.snap @@ -77,3 +77,16 @@ exports[`jestProject should create a jest.config.ts 1`] = ` }; " `; + +exports[`jestProject should use jest.config.js in project config with --js flag 1`] = ` +Object { + "executor": "@nrwl/jest:jest", + "options": Object { + "jestConfig": "libs/lib1/jest.config.js", + "passWithNoTests": true, + }, + "outputs": Array [ + "coverage/libs/lib1", + ], +} +`; 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 c7687ce542dbc..8954b6e936e92 100644 --- a/packages/jest/src/generators/jest-project/jest-project.spec.ts +++ b/packages/jest/src/generators/jest-project/jest-project.spec.ts @@ -272,6 +272,31 @@ describe('jestProject', () => { ); }); + it('should use jest.config.js in project config with --js flag', async () => { + await jestProjectGenerator(tree, { + ...defaultOptions, + project: 'lib1', + js: true, + } as JestProjectSchema); + expect(tree.exists('libs/lib1/jest.config.js')).toBeTruthy(); + expect( + readProjectConfiguration(tree, 'lib1').targets['test'] + ).toMatchSnapshot(); + }); + + it('should use the jest.preset.ts when preset with --js', async () => { + tree.write('jest.preset.ts', ''); + await jestProjectGenerator(tree, { + ...defaultOptions, + project: 'lib1', + js: true, + } as JestProjectSchema); + expect(tree.exists('libs/lib1/jest.config.js')).toBeTruthy(); + expect(tree.read('libs/lib1/jest.config.js', 'utf-8')).toContain( + "preset: '../../jest.preset.ts'," + ); + }); + describe('--babelJest', () => { it('should have globals.ts-jest configured when babelJest is false', async () => { await jestProjectGenerator(tree, { 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 5e83880077ebd..a7360fa93a55b 100644 --- a/packages/jest/src/generators/jest-project/lib/create-files.ts +++ b/packages/jest/src/generators/jest-project/lib/create-files.ts @@ -4,6 +4,7 @@ import { readProjectConfiguration, Tree, } from '@nrwl/devkit'; +import { findRootJestPreset } from '../../../utils/config/find-root-jest-files'; import { join } from 'path'; import { JestProjectSchema } from '../schema'; @@ -26,7 +27,7 @@ export function createFiles(tree: Tree, options: JestProjectSchema) { tmpl: '', ...options, transformer, - ext: options.js && tree.exists('jest.preset.js') ? '.js' : '.ts', + ext: findRootJestPreset(tree) === 'jest.preset.js' ? '.js' : '.ts', projectRoot: 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 ee2c8f4806ef1..660b244dc6842 100644 --- a/packages/jest/src/generators/jest-project/lib/update-jestconfig.ts +++ b/packages/jest/src/generators/jest-project/lib/update-jestconfig.ts @@ -1,22 +1,23 @@ +import { findRootJestConfig } from '../../../utils/config/find-root-jest-files'; import { JestProjectSchema } from '../schema'; import { addPropertyToJestConfig } from '../../../utils/config/update-config'; import { readProjectConfiguration, Tree } from '@nrwl/devkit'; -function isUsingUtilityFunction(host: Tree, js = false) { +function isUsingUtilityFunction(host: Tree) { return host - .read(`jest.config.${js ? 'js' : 'ts'}`) + .read(findRootJestConfig(host)) .toString() .includes('getJestProjects()'); } export function updateJestConfig(host: Tree, options: JestProjectSchema) { - if (isUsingUtilityFunction(host, options.js)) { + if (isUsingUtilityFunction(host)) { return; } const project = readProjectConfiguration(host, options.project); addPropertyToJestConfig( host, - `jest.config.${options.js ? 'js' : 'ts'}`, + findRootJestConfig(host), 'projects', `/${project.root}` ); diff --git a/packages/jest/src/generators/jest-project/lib/update-workspace.ts b/packages/jest/src/generators/jest-project/lib/update-workspace.ts index 966729da07ba4..26cd6270d088c 100644 --- a/packages/jest/src/generators/jest-project/lib/update-workspace.ts +++ b/packages/jest/src/generators/jest-project/lib/update-workspace.ts @@ -20,7 +20,7 @@ export function updateWorkspace(tree: Tree, options: JestProjectSchema) { options: { jestConfig: joinPathFragments( normalizePath(projectConfig.root), - 'jest.config.ts' + `jest.config.${options.js ? 'js' : 'ts'}` ), passWithNoTests: true, }, diff --git a/packages/jest/src/migrations/update-14-0-0/__snapshots__/update-jest-config-ext.spec.ts.snap b/packages/jest/src/migrations/update-14-0-0/__snapshots__/update-jest-config-ext.spec.ts.snap index 7f6908203e7e3..c058727ed8fef 100644 --- a/packages/jest/src/migrations/update-14-0-0/__snapshots__/update-jest-config-ext.spec.ts.snap +++ b/packages/jest/src/migrations/update-14-0-0/__snapshots__/update-jest-config-ext.spec.ts.snap @@ -3,7 +3,7 @@ exports[`Jest Migration (v14.0.0) should rename project jest.config.js to jest.config.ts 1`] = ` "module.exports = { displayName: 'lib-one', - preset: '../../jest.preset.ts', + globals: { 'ts-jest': { tsconfig: '/tsconfig.spec.json', @@ -13,7 +13,7 @@ exports[`Jest Migration (v14.0.0) should rename project jest.config.js to jest.c '^.+\\\\\\\\.[tj]sx?$': 'ts-jest' }, moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'], - coverageDirectory: '../../coverage/libs/lib-one' + coverageDirectory: '../../coverage/libs/lib-one',\\"preset\\": \\"../../jest.preset.ts\\" }; " `; @@ -21,7 +21,7 @@ exports[`Jest Migration (v14.0.0) should rename project jest.config.js to jest.c exports[`Jest Migration (v14.0.0) should update jest.config.ts preset to use the jest.preset.ts 1`] = ` "module.exports = { displayName: 'lib-one', - preset: '../../jest.preset.ts', + globals: { 'ts-jest': { tsconfig: '/tsconfig.spec.json', @@ -31,7 +31,7 @@ exports[`Jest Migration (v14.0.0) should update jest.config.ts preset to use the '^.+\\\\\\\\.[tj]sx?$': 'ts-jest' }, moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'], - coverageDirectory: '../../coverage/libs/lib-one' + coverageDirectory: '../../coverage/libs/lib-one',\\"preset\\": \\"../../jest.preset.ts\\" }; " `; diff --git a/packages/jest/src/migrations/update-14-0-0/update-jest-config-ext.spec.ts b/packages/jest/src/migrations/update-14-0-0/update-jest-config-ext.spec.ts index 02de60c843cde..1540ed6c7850c 100644 --- a/packages/jest/src/migrations/update-14-0-0/update-jest-config-ext.spec.ts +++ b/packages/jest/src/migrations/update-14-0-0/update-jest-config-ext.spec.ts @@ -6,6 +6,7 @@ import { updateProjectConfiguration, } from '@nrwl/devkit'; import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; +import { jestInitGenerator } from '@nrwl/jest'; import { updateJestConfigExt } from './update-jest-config-ext'; import { libraryGenerator as workspaceLib } from '@nrwl/workspace'; @@ -13,22 +14,7 @@ describe('Jest Migration (v14.0.0)', () => { let tree: Tree; beforeEach(async () => { tree = createTreeWithEmptyWorkspace(2); - tree.write( - 'jest.config.js', - String.raw` -const { getJestProjects } = require('@nrwl/jest'); -module.exports = { - projects: getJestProjects(), -}; -` - ); - tree.write( - 'jest.preset.js', - String.raw` -const nxPreset = require('@nrwl/jest/preset'); -module.exports = { ...nxPreset }; -` - ); + jestInitGenerator(tree, { js: true, skipPackageJson: true }); await workspaceLib(tree, { name: 'lib-one' }); tree.rename('libs/lib-one/jest.config.ts', 'libs/lib-one/jest.config.js'); updateProjectConfiguration(tree, 'lib-one', { diff --git a/packages/jest/src/utils/config/find-root-jest-files.ts b/packages/jest/src/utils/config/find-root-jest-files.ts new file mode 100644 index 0000000000000..ba22b8e26fe30 --- /dev/null +++ b/packages/jest/src/utils/config/find-root-jest-files.ts @@ -0,0 +1,25 @@ +import { Tree } from '@nrwl/devkit'; + +export function findRootJestConfig(tree: Tree): string | null { + if (tree.exists('jest.config.js')) { + return 'jest.config.js'; + } + + if (tree.exists('jest.config.ts')) { + return 'jest.config.ts'; + } + + return null; +} + +export function findRootJestPreset(tree: Tree): string | null { + if (tree.exists('jest.preset.js')) { + return 'jest.preset.js'; + } + + if (tree.exists('jest.preset.ts')) { + return 'jest.preset.ts'; + } + + return null; +} diff --git a/packages/js/src/generators/library/files/jest-config/jest.config.ts__tmpl__ b/packages/js/src/generators/library/files/jest-config/jest.config.__ext__ similarity index 89% rename from packages/js/src/generators/library/files/jest-config/jest.config.ts__tmpl__ rename to packages/js/src/generators/library/files/jest-config/jest.config.__ext__ index 08621f37f95c6..7d3e9511c9466 100644 --- a/packages/js/src/generators/library/files/jest-config/jest.config.ts__tmpl__ +++ b/packages/js/src/generators/library/files/jest-config/jest.config.__ext__ @@ -8,7 +8,7 @@ const { exclude: _, ...swcJestConfig } = JSON.parse( module.exports = { displayName: '<%= project %>', - preset: '<%= offsetFromRoot %>jest.preset.ts', + preset: '<%= offsetFromRoot %>jest.preset.<%= ext %>', transform: { '^.+\\.[tj]s$': ['@swc/jest', swcJestConfig], }, diff --git a/packages/js/src/generators/library/library.spec.ts b/packages/js/src/generators/library/library.spec.ts index 34f8af52e7086..fec36a8dad781 100644 --- a/packages/js/src/generators/library/library.spec.ts +++ b/packages/js/src/generators/library/library.spec.ts @@ -570,7 +570,7 @@ describe('lib', () => { name: 'myLib', js: true, }); - expect(tree.exists(`libs/my-lib/jest.config.ts`)).toBeTruthy(); + expect(tree.exists(`libs/my-lib/jest.config.js`)).toBeTruthy(); expect(tree.exists('libs/my-lib/src/index.js')).toBeTruthy(); expect(tree.exists('libs/my-lib/src/lib/my-lib.js')).toBeTruthy(); expect(tree.exists('libs/my-lib/src/lib/my-lib.spec.js')).toBeTruthy(); @@ -617,7 +617,7 @@ describe('lib', () => { directory: 'myDir', js: true, }); - expect(tree.exists(`libs/my-dir/my-lib/jest.config.ts`)).toBeTruthy(); + expect(tree.exists(`libs/my-dir/my-lib/jest.config.js`)).toBeTruthy(); expect(tree.exists('libs/my-dir/my-lib/src/index.js')).toBeTruthy(); expect( tree.exists('libs/my-dir/my-lib/src/lib/my-dir-my-lib.js') diff --git a/packages/js/src/generators/library/library.ts b/packages/js/src/generators/library/library.ts index 89c0370d01e69..87775282404ee 100644 --- a/packages/js/src/generators/library/library.ts +++ b/packages/js/src/generators/library/library.ts @@ -14,6 +14,7 @@ import { updateJson, } from '@nrwl/devkit'; import { jestProjectGenerator } from '@nrwl/jest'; +import { findRootJestPreset } from '@nrwl/jest/src/utils/config/find-root-jest-files'; import { Linter, lintProjectGenerator } from '@nrwl/linter'; import { runTasksInSerial } from '@nrwl/workspace/src/utilities/run-tasks-in-serial'; import { @@ -235,6 +236,7 @@ async function addJest( options: NormalizedSchema ): Promise { return await jestProjectGenerator(tree, { + ...options, project: options.name, setupFile: 'none', supportTsx: false, @@ -250,12 +252,10 @@ function replaceJestConfig( options: NormalizedSchema, filesDir: string ) { - // remove the generated jest config by Jest generator - tree.delete(join(options.projectRoot, 'jest.config.js')); - // replace with JS:SWC specific jest config generateFiles(tree, filesDir, options.projectRoot, { tmpl: '', + ext: findRootJestPreset(tree) === 'jest.preset.js' ? 'js' : 'ts', project: options.name, offsetFromRoot: offsetFromRoot(options.projectRoot), projectRoot: options.projectRoot, diff --git a/packages/next/src/generators/application/lib/add-jest.ts b/packages/next/src/generators/application/lib/add-jest.ts index 2aaffbe049f59..e17fec046681a 100644 --- a/packages/next/src/generators/application/lib/add-jest.ts +++ b/packages/next/src/generators/application/lib/add-jest.ts @@ -8,6 +8,7 @@ export async function addJest(host: Tree, options: NormalizedSchema) { } const jestTask = await jestProjectGenerator(host, { + ...options, project: options.projectName, supportTsx: true, skipSerializers: true, diff --git a/packages/next/src/generators/application/lib/update-jest-config.ts b/packages/next/src/generators/application/lib/update-jest-config.ts index f437550730c89..e776f44b7b2cb 100644 --- a/packages/next/src/generators/application/lib/update-jest-config.ts +++ b/packages/next/src/generators/application/lib/update-jest-config.ts @@ -6,7 +6,9 @@ export function updateJestConfig(host: Tree, options: NormalizedSchema) { return; } - const configPath = `${options.appProjectRoot}/jest.config.ts`; + const configPath = `${options.appProjectRoot}/jest.config.${ + options.js ? 'js' : 'ts' + }`; const originalContent = host.read(configPath, 'utf-8'); const content = originalContent .replace( diff --git a/packages/next/src/generators/init/init.ts b/packages/next/src/generators/init/init.ts index 48b9074e14b62..e1b676fc7644c 100644 --- a/packages/next/src/generators/init/init.ts +++ b/packages/next/src/generators/init/init.ts @@ -40,7 +40,7 @@ export async function nextInitGenerator(host: Tree, schema: InitSchema) { setDefaultCollection(host, '@nrwl/next'); if (!schema.unitTestRunner || schema.unitTestRunner === 'jest') { - const jestTask = jestInitGenerator(host, {}); + const jestTask = jestInitGenerator(host, schema); tasks.push(jestTask); } if (!schema.e2eTestRunner || schema.e2eTestRunner === 'cypress') { diff --git a/packages/next/src/generators/init/schema.d.ts b/packages/next/src/generators/init/schema.d.ts index 1609ce865be39..a0157cab6ecd6 100644 --- a/packages/next/src/generators/init/schema.d.ts +++ b/packages/next/src/generators/init/schema.d.ts @@ -2,4 +2,5 @@ export interface InitSchema { unitTestRunner?: 'jest' | 'none'; e2eTestRunner?: 'cypress' | 'none'; skipFormat?: boolean; + js?: boolean; } diff --git a/packages/next/src/generators/init/schema.json b/packages/next/src/generators/init/schema.json index 219991f2d3c99..13370bb45af0c 100644 --- a/packages/next/src/generators/init/schema.json +++ b/packages/next/src/generators/init/schema.json @@ -22,6 +22,11 @@ "description": "Skip formatting files.", "type": "boolean", "default": false + }, + "js": { + "type": "boolean", + "default": false, + "description": "Use JavaScript instead of TypeScript" } }, "required": [] diff --git a/packages/node/src/generators/application/application.spec.ts b/packages/node/src/generators/application/application.spec.ts index a0b0fbead3575..b08bf4ae2fd7a 100644 --- a/packages/node/src/generators/application/application.spec.ts +++ b/packages/node/src/generators/application/application.spec.ts @@ -395,7 +395,7 @@ describe('app', () => { js: true, } as Schema); - expect(tree.exists(`apps/my-node-app/jest.config.ts`)).toBeTruthy(); + expect(tree.exists(`apps/my-node-app/jest.config.js`)).toBeTruthy(); expect(tree.exists('apps/my-node-app/src/main.js')).toBeTruthy(); const tsConfig = readJson(tree, 'apps/my-node-app/tsconfig.json'); @@ -439,7 +439,7 @@ describe('app', () => { js: true, } as Schema); expect( - tree.exists(`apps/my-dir/my-node-app/jest.config.ts`) + tree.exists(`apps/my-dir/my-node-app/jest.config.js`) ).toBeTruthy(); expect(tree.exists('apps/my-dir/my-node-app/src/main.js')).toBeTruthy(); }); diff --git a/packages/node/src/generators/application/application.ts b/packages/node/src/generators/application/application.ts index 2ba46d101c419..a5c86443c1be9 100644 --- a/packages/node/src/generators/application/application.ts +++ b/packages/node/src/generators/application/application.ts @@ -212,6 +212,7 @@ export async function applicationGenerator(tree: Tree, schema: Schema) { if (options.unitTestRunner === 'jest') { const jestTask = await jestProjectGenerator(tree, { + ...options, project: options.name, setupFile: 'none', skipSerializers: true, diff --git a/packages/node/src/generators/init/init.ts b/packages/node/src/generators/init/init.ts index 68164423ad403..9e444c0e5d4b9 100644 --- a/packages/node/src/generators/init/init.ts +++ b/packages/node/src/generators/init/init.ts @@ -41,7 +41,7 @@ export async function initGenerator(tree: Tree, schema: Schema) { let jestInstall: GeneratorCallback; if (options.unitTestRunner === 'jest') { - jestInstall = await jestInitGenerator(tree, {}); + jestInstall = await jestInitGenerator(tree, schema); } const installTask = await updateDependencies(tree); if (!options.skipFormat) { diff --git a/packages/node/src/generators/init/schema.d.ts b/packages/node/src/generators/init/schema.d.ts index dde6ab28778e7..2216c6dbc28be 100644 --- a/packages/node/src/generators/init/schema.d.ts +++ b/packages/node/src/generators/init/schema.d.ts @@ -1,4 +1,5 @@ export interface Schema { unitTestRunner?: 'jest' | 'none'; skipFormat?: boolean; + js?: boolean; } diff --git a/packages/node/src/generators/init/schema.json b/packages/node/src/generators/init/schema.json index a83579685baa8..647e7d4248799 100644 --- a/packages/node/src/generators/init/schema.json +++ b/packages/node/src/generators/init/schema.json @@ -16,6 +16,11 @@ "description": "Skip formatting files.", "type": "boolean", "default": false + }, + "js": { + "type": "boolean", + "default": false, + "description": "Use JavaScript instead of TypeScript" } }, "required": [] diff --git a/packages/react-native/src/generators/init/init.ts b/packages/react-native/src/generators/init/init.ts index 09c239b431bda..373324c95a28c 100644 --- a/packages/react-native/src/generators/init/init.ts +++ b/packages/react-native/src/generators/init/init.ts @@ -50,7 +50,7 @@ export async function reactNativeInitGenerator(host: Tree, schema: Schema) { const tasks = [moveDependency(host), updateDependencies(host)]; if (!schema.unitTestRunner || schema.unitTestRunner === 'jest') { - const jestTask = jestInitGenerator(host, {}); + const jestTask = jestInitGenerator(host, schema); tasks.push(jestTask); } diff --git a/packages/react-native/src/generators/init/schema.d.ts b/packages/react-native/src/generators/init/schema.d.ts index 11ea540ee8778..831a2d5d19d8d 100644 --- a/packages/react-native/src/generators/init/schema.d.ts +++ b/packages/react-native/src/generators/init/schema.d.ts @@ -2,4 +2,5 @@ export interface Schema { unitTestRunner?: 'jest' | 'none'; skipFormat?: boolean; e2eTestRunner?: 'detox' | 'none'; + js?: boolean; } diff --git a/packages/react-native/src/generators/init/schema.json b/packages/react-native/src/generators/init/schema.json index 30b8d8056c7b6..315e90c57df82 100644 --- a/packages/react-native/src/generators/init/schema.json +++ b/packages/react-native/src/generators/init/schema.json @@ -22,6 +22,11 @@ "type": "string", "enum": ["detox", "none"], "default": "detox" + }, + "js": { + "type": "boolean", + "default": false, + "description": "Use JavaScript instead of TypeScript" } }, "required": [] diff --git a/packages/react-native/src/utils/add-jest.ts b/packages/react-native/src/utils/add-jest.ts index 7cc9c6d80f03d..a3b295c53bd7d 100644 --- a/packages/react-native/src/utils/add-jest.ts +++ b/packages/react-native/src/utils/add-jest.ts @@ -13,6 +13,7 @@ export async function addJest( } const jestTask = await jestProjectGenerator(host, { + js, project: projectName, supportTsx: true, skipSerializers: true, @@ -21,7 +22,7 @@ export async function addJest( }); // overwrite the jest.config.ts file because react native needs to have special transform property - const configPath = `${appProjectRoot}/jest.config.ts`; + const configPath = `${appProjectRoot}/jest.config.${js ? 'js' : 'ts'}`; const content = `module.exports = { displayName: '${projectName}', preset: 'react-native', diff --git a/packages/react/src/generators/application/lib/add-jest.ts b/packages/react/src/generators/application/lib/add-jest.ts index 2076a39168629..cd18cce87b40d 100644 --- a/packages/react/src/generators/application/lib/add-jest.ts +++ b/packages/react/src/generators/application/lib/add-jest.ts @@ -8,6 +8,7 @@ export async function addJest(host: Tree, options: NormalizedSchema) { } return await jestProjectGenerator(host, { + ...options, project: options.projectName, supportTsx: true, skipSerializers: true, diff --git a/packages/react/src/generators/application/lib/update-jest-config.ts b/packages/react/src/generators/application/lib/update-jest-config.ts index a3ed7b354e9c1..5e4c5b4c109dc 100644 --- a/packages/react/src/generators/application/lib/update-jest-config.ts +++ b/packages/react/src/generators/application/lib/update-jest-config.ts @@ -21,7 +21,9 @@ export function updateJestConfig(host: Tree, options: NormalizedSchema) { return json; }); - const configPath = `${options.appProjectRoot}/jest.config.ts`; + const configPath = `${options.appProjectRoot}/jest.config.${ + options.js ? 'js' : 'ts' + }`; const originalContent = host.read(configPath, 'utf-8'); const content = updateJestConfigContent(originalContent); host.write(configPath, content); diff --git a/packages/react/src/generators/init/init.ts b/packages/react/src/generators/init/init.ts index 319831f9a2da0..1da00b0744d7a 100755 --- a/packages/react/src/generators/init/init.ts +++ b/packages/react/src/generators/init/init.ts @@ -73,7 +73,7 @@ export async function reactInitGenerator(host: Tree, schema: InitSchema) { setDefault(host); if (!schema.unitTestRunner || schema.unitTestRunner === 'jest') { - const jestTask = jestInitGenerator(host, {}); + const jestTask = jestInitGenerator(host, schema); tasks.push(jestTask); } if (!schema.e2eTestRunner || schema.e2eTestRunner === 'cypress') { diff --git a/packages/react/src/generators/init/schema.d.ts b/packages/react/src/generators/init/schema.d.ts index 1609ce865be39..a0157cab6ecd6 100644 --- a/packages/react/src/generators/init/schema.d.ts +++ b/packages/react/src/generators/init/schema.d.ts @@ -2,4 +2,5 @@ export interface InitSchema { unitTestRunner?: 'jest' | 'none'; e2eTestRunner?: 'cypress' | 'none'; skipFormat?: boolean; + js?: boolean; } diff --git a/packages/react/src/generators/init/schema.json b/packages/react/src/generators/init/schema.json index e91cb068ecb1a..9bfadd7e686c3 100644 --- a/packages/react/src/generators/init/schema.json +++ b/packages/react/src/generators/init/schema.json @@ -22,6 +22,11 @@ "description": "Skip formatting files.", "type": "boolean", "default": false + }, + "js": { + "type": "boolean", + "default": false, + "description": "Use JavaScript instead of TypeScript" } }, "required": [] diff --git a/packages/react/src/generators/library/library.ts b/packages/react/src/generators/library/library.ts index 4a468803ec5ed..827ccb978480e 100644 --- a/packages/react/src/generators/library/library.ts +++ b/packages/react/src/generators/library/library.ts @@ -90,6 +90,7 @@ export async function libraryGenerator(host: Tree, schema: Schema) { if (options.unitTestRunner === 'jest') { const jestTask = await jestProjectGenerator(host, { + ...options, project: options.name, setupFile: 'none', supportTsx: true, diff --git a/packages/workspace/src/generators/library/library.spec.ts b/packages/workspace/src/generators/library/library.spec.ts index cb802593217bd..e461a83d3f22a 100644 --- a/packages/workspace/src/generators/library/library.spec.ts +++ b/packages/workspace/src/generators/library/library.spec.ts @@ -704,7 +704,7 @@ describe('lib', () => { name: 'myLib', js: true, }); - expect(tree.exists(`libs/my-lib/jest.config.ts`)).toBeTruthy(); + expect(tree.exists(`libs/my-lib/jest.config.js`)).toBeTruthy(); expect(tree.exists('libs/my-lib/src/index.js')).toBeTruthy(); expect(tree.exists('libs/my-lib/src/lib/my-lib.js')).toBeTruthy(); expect(tree.exists('libs/my-lib/src/lib/my-lib.spec.js')).toBeTruthy(); @@ -758,7 +758,7 @@ describe('lib', () => { directory: 'myDir', js: true, }); - expect(tree.exists(`libs/my-dir/my-lib/jest.config.ts`)).toBeTruthy(); + expect(tree.exists(`libs/my-dir/my-lib/jest.config.js`)).toBeTruthy(); expect(tree.exists('libs/my-dir/my-lib/src/index.js')).toBeTruthy(); expect( tree.exists('libs/my-dir/my-lib/src/lib/my-dir-my-lib.js') diff --git a/packages/workspace/src/generators/library/library.ts b/packages/workspace/src/generators/library/library.ts index 478ec57fef889..a632d4c816d61 100644 --- a/packages/workspace/src/generators/library/library.ts +++ b/packages/workspace/src/generators/library/library.ts @@ -173,6 +173,7 @@ async function addJest( options: NormalizedSchema ): Promise { return await jestProjectGenerator(tree, { + ...options, project: options.name, setupFile: 'none', supportTsx: true,