From 156505d24985dbe2760490f04d6689e41f73c7cd Mon Sep 17 00:00:00 2001 From: Caleb Ukle Date: Thu, 14 Apr 2022 20:18:08 -0500 Subject: [PATCH 1/2] feat(testing): move jest config to .ts move jest config and preset to ts files ISSUES CLOSED: #8344 --- packages/jest/migrations.json | 6 + packages/jest/src/generators/init/init.ts | 8 +- .../__snapshots__/jest-project.spec.ts.snap | 2 +- ...nfig.js__tmpl__ => jest.config.ts__tmpl__} | 2 +- ...nfig.js__tmpl__ => jest.config.ts__tmpl__} | 2 +- .../jest-project/lib/update-jestconfig.ts | 4 +- .../jest-project/lib/update-workspace.ts | 2 +- .../update-jest-config-ext.spec.ts.snap | 37 ++++ .../update-jest-config-ext.spec.ts | 162 ++++++++++++++++++ .../update-14-0-0/update-jest-config-ext.ts | 128 ++++++++++++++ packages/jest/src/utils/config/functions.ts | 6 +- .../src/utils/config/update-config.spec.ts | 75 ++++++++ .../jest/src/utils/config/update-config.ts | 14 +- packages/js/migrations.json | 6 + ...nfig.js__tmpl__ => jest.config.ts__tmpl__} | 2 +- .../files/lib/tsconfig.lib.json__tmpl__ | 2 +- .../exclude-jest-config-swcrc.spec.ts.snap | 36 ++++ .../exclude-jest-config-swcrc.spec.ts | 89 ++++++++++ .../exclude-jest-config-swcrc.ts | 35 ++++ packages/js/src/utils/swc/add-swc-config.ts | 1 + .../workspace-rules-project.ts | 4 +- .../application/files/tsconfig.json__tmpl__ | 2 +- .../application/lib/update-jest-config.ts | 2 +- .../application/files/app/tsconfig.app.json | 2 +- .../library/files/lib/tsconfig.lib.json | 2 +- .../files/app/tsconfig.app.json.template | 2 +- packages/react-native/src/utils/add-jest.ts | 4 +- .../files/common/tsconfig.app.json__tmpl__ | 2 +- .../application/lib/update-jest-config.ts | 2 +- .../files/lib/tsconfig.lib.json__tmpl__ | 2 +- .../application/files/app/tsconfig.app.json | 2 +- .../library/files/lib/tsconfig.lib.json | 2 +- .../generators/move/lib/update-jest-config.ts | 2 +- .../move/lib/update-project-root-files.ts | 6 +- .../remove/lib/update-jest-config.ts | 16 +- 35 files changed, 626 insertions(+), 45 deletions(-) rename packages/jest/src/generators/jest-project/files-angular/{jest.config.js__tmpl__ => jest.config.ts__tmpl__} (94%) rename packages/jest/src/generators/jest-project/files/{jest.config.js__tmpl__ => jest.config.ts__tmpl__} (93%) create mode 100644 packages/jest/src/migrations/update-14-0-0/__snapshots__/update-jest-config-ext.spec.ts.snap create mode 100644 packages/jest/src/migrations/update-14-0-0/update-jest-config-ext.spec.ts create mode 100644 packages/jest/src/migrations/update-14-0-0/update-jest-config-ext.ts rename packages/js/src/generators/library/files/jest-config/{jest.config.js__tmpl__ => jest.config.ts__tmpl__} (90%) create mode 100644 packages/js/src/migrations/update-14-0-0/__snapshots__/exclude-jest-config-swcrc.spec.ts.snap create mode 100644 packages/js/src/migrations/update-14-0-0/exclude-jest-config-swcrc.spec.ts create mode 100644 packages/js/src/migrations/update-14-0-0/exclude-jest-config-swcrc.ts diff --git a/packages/jest/migrations.json b/packages/jest/migrations.json index ddd9e1a8e55b7..29b5d14064a84 100644 --- a/packages/jest/migrations.json +++ b/packages/jest/migrations.json @@ -41,6 +41,12 @@ "cli": "nx", "description": "Create a root babel config file if it doesn't exist and using babel-jest in jest.config.js and add @nrwl/web as needed", "factory": "./src/migrations/update-13-4-4/add-missing-root-babel-config" + }, + "update-jest-config-extensions": { + "version": "14.0.0-beta.2", + "cli": "nx", + "description": "Update move jest config files to .ts files.", + "factory": "./src/migrations/update-14-0-0/update-jest-config-ext" } }, "packageJsonUpdates": { diff --git a/packages/jest/src/generators/init/init.ts b/packages/jest/src/generators/init/init.ts index 0470c8abf147b..e0b027fb75364 100644 --- a/packages/jest/src/generators/init/init.ts +++ b/packages/jest/src/generators/init/init.ts @@ -25,9 +25,9 @@ const schemaDefaults = { } as const; function createJestConfig(host: Tree) { - if (!host.exists('jest.config.js')) { + if (!host.exists('jest.config.ts')) { host.write( - 'jest.config.js', + 'jest.config.ts', stripIndents` const { getJestProjects } = require('@nrwl/jest'); @@ -37,9 +37,9 @@ function createJestConfig(host: Tree) { ); } - if (!host.exists('jest.preset.js')) { + if (!host.exists('jest.preset.ts')) { host.write( - 'jest.preset.js', + 'jest.preset.ts', ` const nxPreset = require('@nrwl/jest/preset'); 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 c7aad642c29ea..f0c8ff522bbd4 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 @@ -64,7 +64,7 @@ exports[`jestProject --setup-file should have setupFilesAfterEnv and globals.ts- " `; -exports[`jestProject should create a jest.config.js 1`] = ` +exports[`jestProject should create a jest.config.ts 1`] = ` "module.exports = { displayName: 'lib1', preset: '../../jest.preset.js', diff --git a/packages/jest/src/generators/jest-project/files-angular/jest.config.js__tmpl__ b/packages/jest/src/generators/jest-project/files-angular/jest.config.ts__tmpl__ similarity index 94% rename from packages/jest/src/generators/jest-project/files-angular/jest.config.js__tmpl__ rename to packages/jest/src/generators/jest-project/files-angular/jest.config.ts__tmpl__ index 24af80da73a59..d89c298e7ed73 100644 --- a/packages/jest/src/generators/jest-project/files-angular/jest.config.js__tmpl__ +++ b/packages/jest/src/generators/jest-project/files-angular/jest.config.ts__tmpl__ @@ -1,6 +1,6 @@ module.exports = { displayName: '<%= project %>', - preset: '<%= offsetFromRoot %>jest.preset.js', + preset: '<%= offsetFromRoot %>jest.preset.ts', setupFilesAfterEnv: ['/src/test-setup.ts'], globals: { 'ts-jest': { diff --git a/packages/jest/src/generators/jest-project/files/jest.config.js__tmpl__ b/packages/jest/src/generators/jest-project/files/jest.config.ts__tmpl__ similarity index 93% rename from packages/jest/src/generators/jest-project/files/jest.config.js__tmpl__ rename to packages/jest/src/generators/jest-project/files/jest.config.ts__tmpl__ index d9efe8761e858..ecb830a72dfa2 100644 --- a/packages/jest/src/generators/jest-project/files/jest.config.js__tmpl__ +++ b/packages/jest/src/generators/jest-project/files/jest.config.ts__tmpl__ @@ -1,6 +1,6 @@ module.exports = { displayName: '<%= project %>', - preset: '<%= offsetFromRoot %>jest.preset.js',<% if(setupFile !== 'none') { %> + preset: '<%= offsetFromRoot %>jest.preset.ts',<% if(setupFile !== 'none') { %> setupFilesAfterEnv: ['/src/test-setup.ts'],<% } %><% if (transformer === 'ts-jest') { %> globals: { 'ts-jest': { 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 47b1dda5c9005..6975570bd6686 100644 --- a/packages/jest/src/generators/jest-project/lib/update-jestconfig.ts +++ b/packages/jest/src/generators/jest-project/lib/update-jestconfig.ts @@ -3,7 +3,7 @@ import { addPropertyToJestConfig } from '../../../utils/config/update-config'; import { readProjectConfiguration, Tree } from '@nrwl/devkit'; function isUsingUtilityFunction(host: Tree) { - return host.read('jest.config.js').toString().includes('getJestProjects()'); + return host.read('jest.config.ts').toString().includes('getJestProjects()'); } export function updateJestConfig(host: Tree, options: JestProjectSchema) { @@ -13,7 +13,7 @@ export function updateJestConfig(host: Tree, options: JestProjectSchema) { const project = readProjectConfiguration(host, options.project); addPropertyToJestConfig( host, - 'jest.config.js', + 'jest.config.ts', '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 a93402e980d48..966729da07ba4 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.js' + 'jest.config.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 new file mode 100644 index 0000000000000..7f6908203e7e3 --- /dev/null +++ b/packages/jest/src/migrations/update-14-0-0/__snapshots__/update-jest-config-ext.spec.ts.snap @@ -0,0 +1,37 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +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', + } + }, + transform: { + '^.+\\\\\\\\.[tj]sx?$': 'ts-jest' + }, + moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'], + coverageDirectory: '../../coverage/libs/lib-one' +}; +" +`; + +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', + } + }, + transform: { + '^.+\\\\\\\\.[tj]sx?$': 'ts-jest' + }, + moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'], + coverageDirectory: '../../coverage/libs/lib-one' +}; +" +`; 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 new file mode 100644 index 0000000000000..3e5dae39c7898 --- /dev/null +++ b/packages/jest/src/migrations/update-14-0-0/update-jest-config-ext.spec.ts @@ -0,0 +1,162 @@ +import { + readJson, + readProjectConfiguration, + Tree, + updateJson, + updateProjectConfiguration, +} from '@nrwl/devkit'; +import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; +import { updateJestConfigExt } from './update-jest-config-ext'; +import { libraryGenerator as workspaceLib } from '@nrwl/workspace'; + +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 }; +` + ); + await workspaceLib(tree, { name: 'lib-one' }); + tree.rename('libs/lib-one/jest.config.ts', 'libs/lib-one/jest.config.js'); + updateProjectConfiguration(tree, 'lib-one', { + ...readProjectConfiguration(tree, 'lib-one'), + targets: { + test: { + executor: '@nrwl/jest:jest', + options: { + jestConfig: 'libs/lib-one/jest.config.js', + passWithNoTests: true, + }, + }, + }, + }); + }); + + it('should rename project jest.config.js to jest.config.ts', async () => { + await updateJestConfigExt(tree); + expect(tree.exists('libs/lib-one/jest.config.ts')).toBeTruthy(); + expect(tree.read('libs/lib-one/jest.config.ts', 'utf-8')).toMatchSnapshot(); + }); + + it('should rename root jest files', async () => { + await updateJestConfigExt(tree); + expect(tree.exists('jest.config.ts')).toBeTruthy(); + expect(tree.exists('jest.preset.ts')).toBeTruthy(); + }); + + it('should update jest.config.ts preset to use the jest.preset.ts', async () => { + tree.rename('libs/lib-one/jest.config.js', 'libs/lib-one/jest.config.ts'); + const projectConfig = readProjectConfiguration(tree, 'lib-one'); + updateProjectConfiguration(tree, 'lib-one', { + ...projectConfig, + targets: { + test: { + ...projectConfig.targets.test, + options: { + jestConfig: 'libs/lib-one/jest.config.ts', + passWithNoTests: true, + }, + }, + }, + }); + expect(tree.exists('libs/lib-one/jest.config.ts')).toBeTruthy(); + await updateJestConfigExt(tree); + expect(tree.exists('libs/lib-one/jest.config.ts')).toBeTruthy(); + expect(tree.read('libs/lib-one/jest.config.ts', 'utf-8')).toMatchSnapshot(); + }); + + it('should only update js/ts files', async () => { + tree.rename('libs/lib-one/jest.config.js', 'libs/lib-one/jest.config.ts'); + updateProjectConfiguration(tree, 'lib-one', { + ...readProjectConfiguration(tree, 'lib-one'), + targets: { + test: { + executor: '@nrwl/jest:jest', + options: { + jestConfig: 'libs/lib-one/jest.config.ts', + passWithNoTests: true, + }, + }, + }, + }); + + await workspaceLib(tree, { name: 'lib-two' }); + tree.delete('libs/lib-two/jest.config.ts'); // lib generator creates a ts file + tree.write('libs/lib-two/jest.config.json', '{}'); + updateProjectConfiguration(tree, 'lib-two', { + ...readProjectConfiguration(tree, 'lib-two'), + targets: { + test: { + executor: '@nrwl/jest:jest', + options: { + jestConfig: 'libs/lib-two/jest.config.json', + passWithNoTests: true, + }, + }, + }, + }); + await workspaceLib(tree, { name: 'lib-three' }); + + expect(tree.exists('libs/lib-one/jest.config.ts')).toBeTruthy(); + await updateJestConfigExt(tree); + expect(tree.exists('libs/lib-one/jest.config.ts')).toBeTruthy(); + expect(tree.exists('libs/lib-two/jest.config.ts')).toBeFalsy(); + expect(tree.exists('libs/lib-two/jest.config.json')).toBeTruthy(); + expect(tree.exists('libs/lib-three/jest.config.ts')).toBeTruthy(); + }); + + it('should not throw error if file does not exit', async () => { + tree.delete('libs/lib-one/jest.config.js'); + await updateJestConfigExt(tree); + expect(tree.exists('libs/lib-one/jest.config.ts')).toBeFalsy(); + expect(tree.exists('libs/lib-one/jest.config.js')).toBeFalsy(); + }); + + it('should update correct tsconfigs', async () => { + updateJson(tree, 'libs/lib-one/tsconfig.lib.json', (json) => { + json.exclude = ['**/*.spec.ts']; + return json; + }); + + await updateJestConfigExt(tree); + + const tsconfig = readJson(tree, 'libs/lib-one/tsconfig.json'); + const libTsConfig = readJson(tree, 'libs/lib-one/tsconfig.lib.json'); + const specTsConfig = readJson(tree, 'libs/lib-one/tsconfig.spec.json'); + + expect(tsconfig.exclude).toBeFalsy(); + expect(libTsConfig.exclude).toEqual(['**/*.spec.ts', 'jest.config.ts']); + expect(specTsConfig.exclude).toBeFalsy(); + }); + + it('should add exclude to root tsconfig with no references', async () => { + tree.delete('libs/lib-one/tsconfig.spec.json'); + tree.delete('libs/lib-one/tsconfig.lib.json'); + + updateJson(tree, 'libs/lib-one/tsconfig.json', (json) => { + delete json.references; + return json; + }); + + await updateJestConfigExt(tree); + + const tsconfig = readJson(tree, 'libs/lib-one/tsconfig.json'); + + expect(tsconfig.exclude).toEqual(['jest.config.ts']); + expect(tree.exists('libs/lib-one/tsconfig.spec.json')).toBeFalsy(); + expect(tree.exists('libs/lib-one/tsconfig.lib.json')).toBeFalsy(); + }); +}); diff --git a/packages/jest/src/migrations/update-14-0-0/update-jest-config-ext.ts b/packages/jest/src/migrations/update-14-0-0/update-jest-config-ext.ts new file mode 100644 index 0000000000000..a98ae4e266aac --- /dev/null +++ b/packages/jest/src/migrations/update-14-0-0/update-jest-config-ext.ts @@ -0,0 +1,128 @@ +import { + formatFiles, + logger, + offsetFromRoot, + readJson, + readProjectConfiguration, + stripIndents, + Tree, + updateJson, + updateProjectConfiguration, +} from '@nrwl/devkit'; +import { jestConfigObject } from '../../utils/config/functions'; +import { dirname, extname, join } from 'path'; +import { + removePropertyFromJestConfig, + addPropertyToJestConfig, +} from '../../utils/config/update-config'; +import { JestExecutorOptions } from '../../executors/jest/schema'; +import { forEachExecutorOptions } from '@nrwl/workspace/src/utilities/executor-options-utils'; + +const allowedExt = ['.ts', '.js']; +let isRootPresetUpdated = false; + +function updateJestPreset( + tree: Tree, + options: JestExecutorOptions, + projectName: string +) { + const oldConfig = jestConfigObject(tree, options.jestConfig); + if (!oldConfig) { + return; + } + // if using the root preset and the root preset was updated to ts file. + // then update the jest config + if (isRootPresetUpdated && oldConfig?.preset?.endsWith('jest.preset.js')) { + removePropertyFromJestConfig(tree, options.jestConfig, 'preset'); + addPropertyToJestConfig( + tree, + options.jestConfig, + 'preset', + join(offsetFromRoot(dirname(options.jestConfig)), 'jest.preset.ts'), + { valueAsString: false } + ); + } +} + +function updateTsConfig(tree: Tree, tsConfigPath: string) { + try { + updateJson(tree, tsConfigPath, (json) => { + json.exclude = Array.from( + new Set([...(json.exclude || []), 'jest.config.ts']) + ); + return json; + }); + } catch (e) { + logger.warn( + stripIndents`Nx Unable to update ${tsConfigPath}. Please manually ignore the jest.config.ts file.` + ); + } +} + +function isJestConfigValid(tree: Tree, options: JestExecutorOptions) { + const configExt = extname(options.jestConfig); + + if (!tree.exists(options.jestConfig) || !allowedExt.includes(configExt)) { + logger.debug( + `unable to update file because it doesn't exist or is not a js or ts file. Config: ${ + options.jestConfig + }. Exists?: ${tree.exists(options.jestConfig)}` + ); + return false; + } + return true; +} + +export async function updateJestConfigExt(tree: Tree) { + if (tree.exists('jest.config.js')) { + tree.rename('jest.config.js', 'jest.config.ts'); + } + + if (tree.exists('jest.preset.js')) { + isRootPresetUpdated = true; + tree.rename('jest.preset.js', 'jest.preset.ts'); + } + + forEachExecutorOptions( + tree, + '@nrwl/jest:jest', + (options, projectName, target, configuration) => { + const projectConfig = readProjectConfiguration(tree, projectName); + + if (!isJestConfigValid(tree, options)) { + return; + } + + updateJestPreset(tree, options, projectName); + + const newJestConfigPath = options.jestConfig.replace('.js', '.ts'); + tree.rename(options.jestConfig, newJestConfigPath); + + const rootFiles = tree.children(projectConfig.root); + for (const fileName of rootFiles) { + if (fileName === 'tsconfig.json') { + const filePath = join(projectConfig.root, fileName); + const tsConfig = readJson(tree, filePath); + + if (tsConfig.references) { + for (const { path } of tsConfig.references) { + if (path.endsWith('tsconfig.spec.json')) { + continue; + } + + updateTsConfig(tree, join(projectConfig.root, path)); + } + } else { + updateTsConfig(tree, filePath); + } + } + } + + projectConfig.targets[target].options.jestConfig = newJestConfigPath; + updateProjectConfiguration(tree, projectName, projectConfig); + } + ); + await formatFiles(tree); +} + +export default updateJestConfigExt; diff --git a/packages/jest/src/utils/config/functions.ts b/packages/jest/src/utils/config/functions.ts index e167fcacc7e71..cc6282eb05b2e 100644 --- a/packages/jest/src/utils/config/functions.ts +++ b/packages/jest/src/utils/config/functions.ts @@ -4,6 +4,7 @@ import { ExpressionStatement, isBinaryExpression, isExpressionStatement, + isPropertyAssignment, SyntaxKind, } from 'typescript'; import { applyChangesToString, ChangeType, Tree } from '@nrwl/devkit'; @@ -23,6 +24,9 @@ function findPropertyAssignment( propertyName: string ) { return object.properties.find((prop) => { + if (!isPropertyAssignment(prop)) { + return false; + } const propNameText = prop.name.getText(); if (propNameText.match(/^["'].+["']$/g)) { return JSON.parse(propNameText.replace(/'/g, '"')) === propertyName; @@ -177,7 +181,7 @@ export function jestConfigObjectAst( fileContent: string ): ts.ObjectLiteralExpression { const sourceFile = ts.createSourceFile( - 'jest.config.js', + 'jest.config.ts', fileContent, ts.ScriptTarget.Latest, true diff --git a/packages/jest/src/utils/config/update-config.spec.ts b/packages/jest/src/utils/config/update-config.spec.ts index 54b56069a52ef..b788b7331e46f 100644 --- a/packages/jest/src/utils/config/update-config.spec.ts +++ b/packages/jest/src/utils/config/update-config.spec.ts @@ -165,6 +165,46 @@ describe('Update jest.config.js', () => { expect(json['update-me']).toEqual('goodbye'); }); + it('should modify a property with spread object syntax config', () => { + host.write( + 'jest.config.js', + String.raw` + const { nxPreset } = require('@nrwl/jest/preset'); + + module.exports = { + ...nxPreset, + name: 'test', + boolean: false, + preset: 'nrwl-preset', + "update-me": "hello", + alreadyExistingArray: ['something'], + alreadyExistingObject: { + nestedProperty: { + primitive: 'string', + childArray: ['value1', 'value2'] + }, + 'nested-object': { + childArray: ['value1', 'value2'] + } + }, + numeric: 0, + } + ` + ); + addPropertyToJestConfig( + host, + 'jest.config.js', + 'something-here', + 'newPropertyValue' + ); + let json = jestConfigObject(host, 'jest.config.js'); + expect(json['something-here']).toEqual('newPropertyValue'); + + addPropertyToJestConfig(host, 'jest.config.js', 'update-me', 'goodbye'); + json = jestConfigObject(host, 'jest.config.js'); + expect(json['update-me']).toEqual('goodbye'); + }); + describe('warnings', () => { beforeEach(() => { jest.spyOn(console, 'warn'); @@ -234,5 +274,40 @@ describe('Update jest.config.js', () => { const json = jestConfigObject(host, 'jest.config.js'); expect(json['alreadyExistingObject']).toEqual(undefined); }); + + it('should remove property with a spread object syntax in config', () => { + host.write( + 'jest.config.js', + String.raw` + const { nxPreset } = require('@nrwl/jest/preset'); + + module.exports = { + ...nxPreset, + name: 'test', + boolean: false, + preset: 'nrwl-preset', + "update-me": "hello", + alreadyExistingArray: ['something'], + alreadyExistingObject: { + nestedProperty: { + primitive: 'string', + childArray: ['value1', 'value2'] + }, + 'nested-object': { + childArray: ['value1', 'value2'] + } + }, + numeric: 0, + } + ` + ); + removePropertyFromJestConfig( + host, + 'jest.config.js', + 'alreadyExistingObject' + ); + const json = jestConfigObject(host, 'jest.config.js'); + expect(json['alreadyExistingObject']).toEqual(undefined); + }); }); }); diff --git a/packages/jest/src/utils/config/update-config.ts b/packages/jest/src/utils/config/update-config.ts index a016d5706679a..f7cebd71290a3 100644 --- a/packages/jest/src/utils/config/update-config.ts +++ b/packages/jest/src/utils/config/update-config.ts @@ -1,4 +1,4 @@ -import { Tree, applyChangesToString, ChangeType } from '@nrwl/devkit'; +import { Tree, applyChangesToString, ChangeType, logger } from '@nrwl/devkit'; import { addOrUpdateProperty, jestConfigObjectAst, @@ -34,12 +34,12 @@ export function addPropertyToJestConfig( path ); } catch (e) { - console.warn( + logger.info(`NX Please manually update ${path}`); + logger.warn( `Could not automatically add the following property to ${path}:` ); - console.warn(`${propertyName}: ${JSON.stringify(value)}`); - console.log(`Please manually update ${path}`); - console.warn(`Error: ${e.message}`); + logger.warn(`${propertyName}: ${JSON.stringify(value)}`); + logger.warn(`Error: ${e.message}`); } } @@ -79,10 +79,10 @@ export function removePropertyFromJestConfig( return; } } catch (e) { - console.warn( + logger.info(`NX Please manually update ${path}`); + logger.warn( `Could not automatically remove the '${propertyName}' property from ${path}:` ); - console.log(`Please manually update ${path}`); } } diff --git a/packages/js/migrations.json b/packages/js/migrations.json index 23b702a8c1d49..bd0ab190cfbb6 100644 --- a/packages/js/migrations.json +++ b/packages/js/migrations.json @@ -17,6 +17,12 @@ "version": "13.10.1-beta.1", "description": "Update .lib.swcrc to exclude missing test files", "factory": "./src/migrations/update-13-10-1/update-lib-swcrc-exclude" + }, + "exclude-jest-config-swcrc": { + "cli": "nx", + "version": "14.0.0-beta.2", + "description": "Exclude jest config from .lib.swcrc", + "factory": "./src/migrations/update-14-0-0/exclude-jest-config-swcrc" } }, "packageJsonUpdates": {} diff --git a/packages/js/src/generators/library/files/jest-config/jest.config.js__tmpl__ b/packages/js/src/generators/library/files/jest-config/jest.config.ts__tmpl__ similarity index 90% rename from packages/js/src/generators/library/files/jest-config/jest.config.js__tmpl__ rename to packages/js/src/generators/library/files/jest-config/jest.config.ts__tmpl__ index 416e51230ac13..08621f37f95c6 100644 --- a/packages/js/src/generators/library/files/jest-config/jest.config.js__tmpl__ +++ b/packages/js/src/generators/library/files/jest-config/jest.config.ts__tmpl__ @@ -8,7 +8,7 @@ const { exclude: _, ...swcJestConfig } = JSON.parse( module.exports = { displayName: '<%= project %>', - preset: '<%= offsetFromRoot %>jest.preset.js', + preset: '<%= offsetFromRoot %>jest.preset.ts', transform: { '^.+\\.[tj]s$': ['@swc/jest', swcJestConfig], }, diff --git a/packages/js/src/generators/library/files/lib/tsconfig.lib.json__tmpl__ b/packages/js/src/generators/library/files/lib/tsconfig.lib.json__tmpl__ index 6bbdeba123c5e..15a16deae4414 100644 --- a/packages/js/src/generators/library/files/lib/tsconfig.lib.json__tmpl__ +++ b/packages/js/src/generators/library/files/lib/tsconfig.lib.json__tmpl__ @@ -6,5 +6,5 @@ "types": [] }, "include": ["**/*.ts"<% if (js) { %>, "**/*.js"<% } %>], - "exclude": ["**/*.spec.ts", "**/*.test.ts"<% if (js) { %>, "**/*.spec.js", "**/*.test.js"<% } %>] + "exclude": ["jest.config.ts", "**/*.spec.ts", "**/*.test.ts"<% if (js) { %>, "**/*.spec.js", "**/*.test.js"<% } %>] } diff --git a/packages/js/src/migrations/update-14-0-0/__snapshots__/exclude-jest-config-swcrc.spec.ts.snap b/packages/js/src/migrations/update-14-0-0/__snapshots__/exclude-jest-config-swcrc.spec.ts.snap new file mode 100644 index 0000000000000..d9f768e42113d --- /dev/null +++ b/packages/js/src/migrations/update-14-0-0/__snapshots__/exclude-jest-config-swcrc.spec.ts.snap @@ -0,0 +1,36 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`JS Migration (v14.0.0) should update the exclude pattern 1`] = ` +"{ + \\"jsc\\": { + \\"target\\": \\"es2017\\", + \\"parser\\": { + \\"syntax\\": \\"typescript\\", + \\"decorators\\": true, + \\"dynamicImport\\": true + }, + \\"transform\\": { + \\"decoratorMetadata\\": true, + \\"legacyDecorator\\": true + }, + \\"keepClassNames\\": true, + \\"externalHelpers\\": true, + \\"loose\\": true + }, + \\"module\\": { + \\"type\\": \\"commonjs\\", + \\"strict\\": true, + \\"noInterop\\": true + }, + \\"sourceMaps\\": true, + \\"exclude\\": [ + \\"jest.config.js\\", + \\".*.spec.tsx?$\\", + \\".*.test.tsx?$\\", + \\"./src/jest-setup.ts$\\", + \\"./**/jest-setup.ts$\\", + \\".*.js$\\" + ] +} +" +`; diff --git a/packages/js/src/migrations/update-14-0-0/exclude-jest-config-swcrc.spec.ts b/packages/js/src/migrations/update-14-0-0/exclude-jest-config-swcrc.spec.ts new file mode 100644 index 0000000000000..af75f2032e78b --- /dev/null +++ b/packages/js/src/migrations/update-14-0-0/exclude-jest-config-swcrc.spec.ts @@ -0,0 +1,89 @@ +import { + addProjectConfiguration, + readJson, + Tree, + updateJson, +} from '@nrwl/devkit'; +import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; +import { excludeJestConfigSwcrc } from './exclude-jest-config-swcrc'; +import { ProjectConfiguration } from 'nx/src/config/workspace-json-project-json'; + +const projectConfig: ProjectConfiguration = { + root: 'libs/swc-lib', + sourceRoot: 'libs/swc-lib/src', + targets: { + build: { + executor: '@nrwl/js:swc', + outputs: ['{options.outputPath}'], + options: { + outputPath: 'dist/libs/swc-lib', + main: 'libs/swc-lib/src/index.ts', + tsConfig: 'libs/swc-lib/tsconfig.lib.json', + assets: ['libs/swc-lib/*.md'], + }, + }, + }, +}; +const oldSwcRc = { + jsc: { + target: 'es2017', + parser: { + syntax: 'typescript', + decorators: true, + dynamicImport: true, + }, + transform: { + decoratorMetadata: true, + legacyDecorator: true, + }, + keepClassNames: true, + externalHelpers: true, + loose: true, + }, + module: { + type: 'commonjs', + strict: true, + noInterop: true, + }, + sourceMaps: true, + exclude: [ + '.*.spec.tsx?$', + '.*.test.tsx?$', + './src/jest-setup.ts$', + './**/jest-setup.ts$', + '.*.js$', + ], +}; +describe('JS Migration (v14.0.0)', () => { + let tree: Tree; + beforeEach(() => { + tree = createTreeWithEmptyWorkspace(); + addProjectConfiguration(tree, 'swc-lib', projectConfig); + + tree.write('libs/swc-lib/.lib.swcrc', JSON.stringify(oldSwcRc)); + }); + + it('should update the exclude pattern', () => { + excludeJestConfigSwcrc(tree); + expect(tree.read('libs/swc-lib/.lib.swcrc', 'utf-8')).toMatchSnapshot(); + }); + + it('should NOT update the exclude pattern if not present', () => { + updateJson(tree, 'libs/swc-lib/.lib.swcrc', (json) => { + delete json.exclude; + return json; + }); + + const before = readJson(tree, 'libs/swc-lib/.lib.swcrc'); + excludeJestConfigSwcrc(tree); + const after = readJson(tree, 'libs/swc-lib/.lib.swcrc'); + + expect(after.exclude).toBeFalsy(); + expect(after).toEqual(before); + }); + + it('should do nothing if .lib.swcrc doest not exist', () => { + tree.delete('libs/swc-lib/.lib-swcrc'); + expect(() => excludeJestConfigSwcrc(tree)).not.toThrowError(); + }); +}); diff --git a/packages/js/src/migrations/update-14-0-0/exclude-jest-config-swcrc.ts b/packages/js/src/migrations/update-14-0-0/exclude-jest-config-swcrc.ts new file mode 100644 index 0000000000000..5bcf4d3601b26 --- /dev/null +++ b/packages/js/src/migrations/update-14-0-0/exclude-jest-config-swcrc.ts @@ -0,0 +1,35 @@ +import { readProjectConfiguration, Tree, updateJson } from '@nrwl/devkit'; +import { SwcExecutorOptions } from '../../utils/schema'; +import { forEachExecutorOptions } from '@nrwl/workspace/src/utilities/executor-options-utils'; +import { join } from 'path'; + +export function excludeJestConfigSwcrc(tree: Tree) { + forEachExecutorOptions( + tree, + '@nrwl/js:swc', + (config: SwcExecutorOptions, projectName) => { + const projectConfig = readProjectConfiguration(tree, projectName); + const libSwcPath = join(projectConfig.root, '.lib.swcrc'); + + if (!tree.exists(libSwcPath)) return; + + updateJson( + tree, + libSwcPath, + (json) => { + if (json.exclude) { + const excludePatterns = new Set([ + 'jest.config.js', + ...json.exclude, + ]); + json.exclude = [...excludePatterns]; + } + return json; + }, + { expectComments: true } + ); + } + ); +} + +export default excludeJestConfigSwcrc; diff --git a/packages/js/src/utils/swc/add-swc-config.ts b/packages/js/src/utils/swc/add-swc-config.ts index 892088026cfaf..6ac7c2bd04247 100644 --- a/packages/js/src/utils/swc/add-swc-config.ts +++ b/packages/js/src/utils/swc/add-swc-config.ts @@ -4,6 +4,7 @@ import { Tree } from '@nrwl/devkit'; import { join } from 'path'; export const defaultExclude = [ + 'jest.config.ts', '.*.spec.tsx?$', '.*.test.tsx?$', './src/jest-setup.ts$', diff --git a/packages/linter/src/generators/workspace-rules-project/workspace-rules-project.ts b/packages/linter/src/generators/workspace-rules-project/workspace-rules-project.ts index 023a93857559b..364e8b227cc63 100644 --- a/packages/linter/src/generators/workspace-rules-project/workspace-rules-project.ts +++ b/packages/linter/src/generators/workspace-rules-project/workspace-rules-project.ts @@ -62,10 +62,10 @@ export async function lintWorkspaceRulesProjectGenerator(tree: Tree) { compiler: 'tsc', }); - // Add extra config to the jest.config.js file to allow ESLint 8 exports mapping to work with jest + // Add extra config to the jest.config.ts file to allow ESLint 8 exports mapping to work with jest addPropertyToJestConfig( tree, - joinPathFragments(WORKSPACE_PLUGIN_DIR, 'jest.config.js'), + joinPathFragments(WORKSPACE_PLUGIN_DIR, 'jest.config.ts'), 'moduleNameMapper', { '@eslint/eslintrc': '@eslint/eslintrc/dist/eslintrc-universal.cjs', diff --git a/packages/next/src/generators/application/files/tsconfig.json__tmpl__ b/packages/next/src/generators/application/files/tsconfig.json__tmpl__ index 12ce44c31d07f..6bfedf649eed0 100644 --- a/packages/next/src/generators/application/files/tsconfig.json__tmpl__ +++ b/packages/next/src/generators/application/files/tsconfig.json__tmpl__ @@ -14,5 +14,5 @@ "incremental": true }, "include": ["**/*.ts", "**/*.tsx", "**/*.js", "**/*.jsx", "next-env.d.ts"], - "exclude": ["node_modules"] + "exclude": ["node_modules", "jest.config.ts"] } 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 df8631310bdaf..f437550730c89 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,7 @@ export function updateJestConfig(host: Tree, options: NormalizedSchema) { return; } - const configPath = `${options.appProjectRoot}/jest.config.js`; + const configPath = `${options.appProjectRoot}/jest.config.ts`; const originalContent = host.read(configPath, 'utf-8'); const content = originalContent .replace( diff --git a/packages/node/src/generators/application/files/app/tsconfig.app.json b/packages/node/src/generators/application/files/app/tsconfig.app.json index c251a15d9984c..3a0dc40bcc37d 100644 --- a/packages/node/src/generators/application/files/app/tsconfig.app.json +++ b/packages/node/src/generators/application/files/app/tsconfig.app.json @@ -5,6 +5,6 @@ "module": "commonjs", "types": ["node"] }, - "exclude": ["**/*.spec.ts", "**/*.test.ts"], + "exclude": ["jest.config.ts", "**/*.spec.ts", "**/*.test.ts"], "include": ["**/*.ts"] } diff --git a/packages/node/src/generators/library/files/lib/tsconfig.lib.json b/packages/node/src/generators/library/files/lib/tsconfig.lib.json index dd63f3372b543..fc6648159ee84 100644 --- a/packages/node/src/generators/library/files/lib/tsconfig.lib.json +++ b/packages/node/src/generators/library/files/lib/tsconfig.lib.json @@ -6,6 +6,6 @@ "declaration": true, "types": ["node"] }, - "exclude": ["**/*.spec.ts", "**/*.test.ts"], + "exclude": ["jest.config.ts", "**/*.spec.ts", "**/*.test.ts"], "include": ["**/*.ts"] } diff --git a/packages/react-native/src/generators/application/files/app/tsconfig.app.json.template b/packages/react-native/src/generators/application/files/app/tsconfig.app.json.template index d4f14ad64a156..7799be4ddbfe8 100644 --- a/packages/react-native/src/generators/application/files/app/tsconfig.app.json.template +++ b/packages/react-native/src/generators/application/files/app/tsconfig.app.json.template @@ -4,6 +4,6 @@ "outDir": "<%= offsetFromRoot %>dist/out-tsc", "types": ["node"] }, - "exclude": ["**/*.spec.ts", "**/*.spec.tsx", "test-setup.ts"], + "exclude": ["jest.config.ts", "**/*.spec.ts", "**/*.spec.tsx", "test-setup.ts"], "include": ["**/*.ts", "**/*.tsx", "**/*.js", "**/*.jsx"] } diff --git a/packages/react-native/src/utils/add-jest.ts b/packages/react-native/src/utils/add-jest.ts index 25b0dfcfd0911..7cc9c6d80f03d 100644 --- a/packages/react-native/src/utils/add-jest.ts +++ b/packages/react-native/src/utils/add-jest.ts @@ -20,8 +20,8 @@ export async function addJest( compiler: 'babel', }); - // overwrite the jest.config.js file because react native needs to have special transform property - const configPath = `${appProjectRoot}/jest.config.js`; + // overwrite the jest.config.ts file because react native needs to have special transform property + const configPath = `${appProjectRoot}/jest.config.ts`; const content = `module.exports = { displayName: '${projectName}', preset: 'react-native', diff --git a/packages/react/src/generators/application/files/common/tsconfig.app.json__tmpl__ b/packages/react/src/generators/application/files/common/tsconfig.app.json__tmpl__ index 1e4a14aded7d2..c8ea6c6486abf 100644 --- a/packages/react/src/generators/application/files/common/tsconfig.app.json__tmpl__ +++ b/packages/react/src/generators/application/files/common/tsconfig.app.json__tmpl__ @@ -9,6 +9,6 @@ "<%= offsetFromRoot %>node_modules/@nrwl/react/typings/cssmodule.d.ts", "<%= offsetFromRoot %>node_modules/@nrwl/react/typings/image.d.ts" ], - "exclude": ["**/*.spec.ts", "**/*.test.ts", "**/*.spec.tsx", "**/*.test.tsx", "**/*.spec.js", "**/*.test.js", "**/*.spec.jsx", "**/*.test.jsx"], + "exclude": ["jest.config.ts","**/*.spec.ts", "**/*.test.ts", "**/*.spec.tsx", "**/*.test.tsx", "**/*.spec.js", "**/*.test.js", "**/*.spec.jsx", "**/*.test.jsx"], "include": ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx"] } 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 c1edb4f641db3..a3ed7b354e9c1 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,7 @@ export function updateJestConfig(host: Tree, options: NormalizedSchema) { return json; }); - const configPath = `${options.appProjectRoot}/jest.config.js`; + const configPath = `${options.appProjectRoot}/jest.config.ts`; const originalContent = host.read(configPath, 'utf-8'); const content = updateJestConfigContent(originalContent); host.write(configPath, content); diff --git a/packages/react/src/generators/library/files/lib/tsconfig.lib.json__tmpl__ b/packages/react/src/generators/library/files/lib/tsconfig.lib.json__tmpl__ index eddbbe340d88b..fa30133616e56 100644 --- a/packages/react/src/generators/library/files/lib/tsconfig.lib.json__tmpl__ +++ b/packages/react/src/generators/library/files/lib/tsconfig.lib.json__tmpl__ @@ -9,6 +9,6 @@ "<%= offsetFromRoot %>node_modules/@nrwl/react/typings/cssmodule.d.ts", "<%= offsetFromRoot %>node_modules/@nrwl/react/typings/image.d.ts" ], - "exclude": ["**/*.spec.ts", "**/*.test.ts", "**/*.spec.tsx", "**/*.test.tsx", "**/*.spec.js", "**/*.test.js", "**/*.spec.jsx", "**/*.test.jsx"], + "exclude": ["jest.config.ts", "**/*.spec.ts", "**/*.test.ts", "**/*.spec.tsx", "**/*.test.tsx", "**/*.spec.js", "**/*.test.js", "**/*.spec.jsx", "**/*.test.jsx"], "include": ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx"] } diff --git a/packages/web/src/generators/application/files/app/tsconfig.app.json b/packages/web/src/generators/application/files/app/tsconfig.app.json index 29c1be079aadf..0ee582ddabaac 100644 --- a/packages/web/src/generators/application/files/app/tsconfig.app.json +++ b/packages/web/src/generators/application/files/app/tsconfig.app.json @@ -4,6 +4,6 @@ "outDir": "<%= offsetFromRoot %>dist/out-tsc", "types": ["node"] }, - "exclude": ["**/*.spec.ts", "**/*.test.ts"], + "exclude": ["jest.config.ts","**/*.spec.ts", "**/*.test.ts"], "include": ["**/*.ts"] } diff --git a/packages/workspace/src/generators/library/files/lib/tsconfig.lib.json b/packages/workspace/src/generators/library/files/lib/tsconfig.lib.json index 2efb9fca0e5af..0c95a2973ab2d 100644 --- a/packages/workspace/src/generators/library/files/lib/tsconfig.lib.json +++ b/packages/workspace/src/generators/library/files/lib/tsconfig.lib.json @@ -6,5 +6,5 @@ "types": [] }, "include": ["**/*.ts"<% if (js) { %>, "**/*.js"<% } %>], - "exclude": ["**/*.spec.ts"<% if (js) { %>, "**/*.spec.js"<% } %>] + "exclude": ["jest.config.ts","**/*.spec.ts"<% if (js) { %>, "**/*.spec.js"<% } %>] } diff --git a/packages/workspace/src/generators/move/lib/update-jest-config.ts b/packages/workspace/src/generators/move/lib/update-jest-config.ts index 8a517e3d91b61..ae2360fcf4279 100644 --- a/packages/workspace/src/generators/move/lib/update-jest-config.ts +++ b/packages/workspace/src/generators/move/lib/update-jest-config.ts @@ -16,7 +16,7 @@ export function updateJestConfig( ) { const jestConfigPath = path.join( schema.relativeToRootDestination, - 'jest.config.js' + 'jest.config.ts' ); if (tree.exists(jestConfigPath)) { diff --git a/packages/workspace/src/generators/move/lib/update-project-root-files.ts b/packages/workspace/src/generators/move/lib/update-project-root-files.ts index 366ce6cc737bb..b345e488f4f59 100644 --- a/packages/workspace/src/generators/move/lib/update-project-root-files.ts +++ b/packages/workspace/src/generators/move/lib/update-project-root-files.ts @@ -3,7 +3,7 @@ import { workspaceRoot } from '@nrwl/devkit'; import * as path from 'path'; import { extname, join } from 'path'; import { NormalizedSchema } from '../schema'; - +const allowedExt = ['.ts', '.js', '.json']; /** * Updates the files in the root of the project * @@ -37,7 +37,9 @@ export function updateProjectRootFiles( const regex = new RegExp(oldRelativeRoot.replace(dots, '\\.'), 'g'); for (const file of tree.children(schema.relativeToRootDestination)) { - if (!extname(file).startsWith('.js')) { + const ext = extname(file); + console.log({file}) + if (!allowedExt.includes(ext)) { continue; } if (file === '.eslintrc.json') { diff --git a/packages/workspace/src/generators/remove/lib/update-jest-config.ts b/packages/workspace/src/generators/remove/lib/update-jest-config.ts index d8178652d299a..7a5fa9501615b 100644 --- a/packages/workspace/src/generators/remove/lib/update-jest-config.ts +++ b/packages/workspace/src/generators/remove/lib/update-jest-config.ts @@ -20,7 +20,7 @@ import { import { join } from 'path'; function isUsingUtilityFunction(host: Tree) { - return host.read('jest.config.js').toString().includes('getJestProjects()'); + return host.read('jest.config.ts').toString().includes('getJestProjects()'); } /** @@ -34,16 +34,16 @@ export function updateJestConfig( const projectToRemove = schema.projectName; if ( - !tree.exists('jest.config.js') || - !tree.exists(join(projectConfig.root, 'jest.config.js')) || + !tree.exists('jest.config.ts') || + !tree.exists(join(projectConfig.root, 'jest.config.ts')) || isUsingUtilityFunction(tree) ) { return; } - const contents = tree.read('jest.config.js', 'utf-8'); + const contents = tree.read('jest.config.ts', 'utf-8'); const sourceFile = createSourceFile( - 'jest.config.js', + 'jest.config.ts', contents, ScriptTarget.Latest ); @@ -59,7 +59,7 @@ export function updateJestConfig( if (!projectsAssignment) { throw Error( - `Could not remove ${projectToRemove} from projects in /jest.config.js. Please remove ${projectToRemove} from your projects.` + `Could not remove ${projectToRemove} from projects in /jest.config.ts. Please remove ${projectToRemove} from your projects.` ); } const projectsArray = @@ -73,7 +73,7 @@ export function updateJestConfig( if (!project) { console.warn( - `Could not find ${projectToRemove} in projects in /jest.config.js.` + `Could not find ${projectToRemove} in projects in /jest.config.ts.` ); return; } @@ -86,7 +86,7 @@ export function updateJestConfig( : project.getStart(sourceFile); tree.write( - 'jest.config.js', + 'jest.config.ts', applyChangesToString(contents, [ { type: ChangeType.Delete, From fb40831b949276253009cd389cbfc0b0db146593 Mon Sep 17 00:00:00 2001 From: Caleb Ukle Date: Tue, 19 Apr 2022 10:46:07 -0500 Subject: [PATCH 2/2] fix(testing): update tests for jest.config.ts changes update tests broken by renaming jest fils to ts files --- e2e/angular-core/src/config.test.ts | 2 +- e2e/jest/src/jest.test.ts | 2 +- e2e/node/src/node.test.ts | 4 +- e2e/utils/index.ts | 2 +- e2e/workspace-core/src/aux-commands.test.ts | 14 +++--- .../src/workspace-aux-commands.test.ts | 2 +- .../__snapshots__/application.spec.ts.snap | 4 +- .../application/application.spec.ts | 18 +++---- .../angular/src/generators/init/init.spec.ts | 2 +- .../src/generators/library/library.spec.ts | 12 ++--- .../storybook-configuration.spec.ts.snap | 12 ++--- .../application/application.spec.ts | 2 + .../jest/src/executors/jest/jest.impl.spec.ts | 38 +++++++------- .../jest/src/generators/init/init.spec.ts | 8 +-- .../__snapshots__/jest-project.spec.ts.snap | 10 ++-- .../jest-project/jest-project.spec.ts | 32 ++++++------ .../js/src/generators/library/library.spec.ts | 16 +++--- .../update-lib-swcrc-exclude.spec.ts.snap | 1 + .../workspace-rules-project.spec.ts.snap | 2 +- .../workspace-rules-project.spec.ts | 4 +- .../application/application.spec.ts | 6 ++- .../nest/src/generators/init/init.spec.ts | 4 +- .../__snapshots__/library.spec.ts.snap | 4 +- .../src/generators/library/library.spec.ts | 18 ++++--- .../application/application.spec.ts | 6 +-- .../update-13-1-1/enable-swc.spec.ts | 50 +++++++++++++++++-- .../application/application.spec.ts | 25 ++++++---- .../src/generators/library/library.spec.ts | 21 +++++--- .../src/generators/e2e-project/e2e.spec.ts | 4 +- .../src/generators/plugin/plugin.spec.ts | 2 +- .../src/generators/library/library.spec.ts | 2 +- .../application/application.spec.ts | 12 +++-- .../src/generators/library/library.spec.ts | 7 +-- .../application/application.spec.ts | 14 +++--- .../src/generators/library/library.spec.ts | 22 ++++---- .../move/lib/update-jest-config.spec.ts | 18 +++---- .../generators/move/lib/update-jest-config.ts | 4 +- .../move/lib/update-project-root-files.ts | 2 - .../src/generators/move/move.spec.ts | 47 +++++++++++++++++ .../preset/__snapshots__/preset.spec.ts.snap | 2 +- .../{jest.config.js => jest.config.ts} | 0 .../remove/lib/update-jest-config.spec.ts | 18 +++---- 42 files changed, 294 insertions(+), 181 deletions(-) create mode 100644 packages/workspace/src/generators/move/move.spec.ts rename packages/workspace/src/generators/remove/lib/test-files/{jest.config.js => jest.config.ts} (100%) diff --git a/e2e/angular-core/src/config.test.ts b/e2e/angular-core/src/config.test.ts index fd0524d25ae6c..e329353095cf6 100644 --- a/e2e/angular-core/src/config.test.ts +++ b/e2e/angular-core/src/config.test.ts @@ -152,7 +152,7 @@ const angularV1Json = (appName: string) => `{ "builder": "@nrwl/jest:jest", "outputs": ["coverage/apps/${appName}"], "options": { - "jestConfig": "apps/${appName}/jest.config.js", + "jestConfig": "apps/${appName}/jest.config.ts", "passWithNoTests": true } } diff --git a/e2e/jest/src/jest.test.ts b/e2e/jest/src/jest.test.ts index 34ac3520830ba..3ed5352601554 100644 --- a/e2e/jest/src/jest.test.ts +++ b/e2e/jest/src/jest.test.ts @@ -75,7 +75,7 @@ describe('Jest', () => { ); updateFile( - `libs/${mylib}/jest.config.js`, + `libs/${mylib}/jest.config.ts`, stripIndents` module.exports = { testMatch: ['**/+(*.)+(spec|test).+(ts|js)?(x)'], diff --git a/e2e/node/src/node.test.ts b/e2e/node/src/node.test.ts index 3a7b36c86b545..8d511e1230874 100644 --- a/e2e/node/src/node.test.ts +++ b/e2e/node/src/node.test.ts @@ -356,12 +356,12 @@ describe('nest libraries', function () { const nestlib = uniq('nestlib'); runCLI(`generate @nrwl/nest:lib ${nestlib}`); - const jestConfigContent = readFile(`libs/${nestlib}/jest.config.js`); + const jestConfigContent = readFile(`libs/${nestlib}/jest.config.ts`); expect(stripIndents`${jestConfigContent}`).toEqual( stripIndents`module.exports = { displayName: '${nestlib}', - preset: '../../jest.preset.js', + preset: '../../jest.preset.ts', globals: { 'ts-jest': { tsconfig: '/tsconfig.spec.json', diff --git a/e2e/utils/index.ts b/e2e/utils/index.ts index 02f2c2158ea41..99fd2f3d1bb33 100644 --- a/e2e/utils/index.ts +++ b/e2e/utils/index.ts @@ -803,7 +803,7 @@ export function expectNoAngularDevkit() { export function expectNoTsJestInJestConfig(appName: string) { const jestConfig = readFile( - joinPathFragments('apps', appName, 'jest.config.js') + joinPathFragments('apps', appName, 'jest.config.ts') ); expect(jestConfig).not.toContain('ts-jest'); } diff --git a/e2e/workspace-core/src/aux-commands.test.ts b/e2e/workspace-core/src/aux-commands.test.ts index 3cfcb39eb8c27..e0aa375097e48 100644 --- a/e2e/workspace-core/src/aux-commands.test.ts +++ b/e2e/workspace-core/src/aux-commands.test.ts @@ -186,7 +186,7 @@ describe('workspace-lint', () => { expect(stdout).toContain( 'The following file(s) do not belong to any projects:' ); - expect(stdout).toContain(`- apps/${appAfter}/jest.config.js`); + expect(stdout).toContain(`- apps/${appAfter}/jest.config.ts`); expect(stdout).toContain(`- apps/${appAfter}/src/app/app.component.css`); expect(stdout).toContain(`- apps/${appAfter}/src/app/app.component.html`); expect(stdout).toContain( @@ -257,12 +257,12 @@ describe('move project', () => { expect(moveOutput).toContain(`CREATE ${readmePath}`); checkFilesExist(readmePath); - const jestConfigPath = `${newPath}/jest.config.js`; + const jestConfigPath = `${newPath}/jest.config.ts`; expect(moveOutput).toContain(`CREATE ${jestConfigPath}`); checkFilesExist(jestConfigPath); const jestConfig = readFile(jestConfigPath); expect(jestConfig).toContain(`displayName: 'shared-${lib1}-data-access'`); - expect(jestConfig).toContain(`preset: '../../../../jest.preset.js'`); + expect(jestConfig).toContain(`preset: '../../../../jest.preset.ts'`); expect(jestConfig).toContain(`'../../../../coverage/${newPath}'`); const tsConfigPath = `${newPath}/tsconfig.json`; @@ -395,12 +395,12 @@ describe('move project', () => { expect(moveOutput).toContain(`CREATE ${readmePath}`); checkFilesExist(readmePath); - const jestConfigPath = `${newPath}/jest.config.js`; + const jestConfigPath = `${newPath}/jest.config.ts`; expect(moveOutput).toContain(`CREATE ${jestConfigPath}`); checkFilesExist(jestConfigPath); const jestConfig = readFile(jestConfigPath); expect(jestConfig).toContain(`displayName: 'shared-${lib1}-data-access'`); - expect(jestConfig).toContain(`preset: '../../../../jest.preset.js'`); + expect(jestConfig).toContain(`preset: '../../../../jest.preset.ts'`); expect(jestConfig).toContain(`'../../../../coverage/${newPath}'`); const tsConfigPath = `${newPath}/tsconfig.json`; @@ -528,12 +528,12 @@ describe('move project', () => { expect(moveOutput).toContain(`CREATE ${readmePath}`); checkFilesExist(readmePath); - const jestConfigPath = `${newPath}/jest.config.js`; + const jestConfigPath = `${newPath}/jest.config.ts`; expect(moveOutput).toContain(`CREATE ${jestConfigPath}`); checkFilesExist(jestConfigPath); const jestConfig = readFile(jestConfigPath); expect(jestConfig).toContain(`displayName: 'shared-${lib1}-data-access'`); - expect(jestConfig).toContain(`preset: '../../../../jest.preset.js'`); + expect(jestConfig).toContain(`preset: '../../../../jest.preset.ts'`); expect(jestConfig).toContain(`'../../../../coverage/${newPath}'`); const tsConfigPath = `${newPath}/tsconfig.json`; diff --git a/e2e/workspace-integrations/src/workspace-aux-commands.test.ts b/e2e/workspace-integrations/src/workspace-aux-commands.test.ts index 98e1173de26c0..d887106aa97b9 100644 --- a/e2e/workspace-integrations/src/workspace-aux-commands.test.ts +++ b/e2e/workspace-integrations/src/workspace-aux-commands.test.ts @@ -445,7 +445,7 @@ describe('Move Angular Project', () => { // just check the output expect(moveOutput).toContain(`DELETE apps/${app1}`); expect(moveOutput).toContain(`CREATE apps/${newPath}/.browserslistrc`); - expect(moveOutput).toContain(`CREATE apps/${newPath}/jest.config.js`); + expect(moveOutput).toContain(`CREATE apps/${newPath}/jest.config.ts`); expect(moveOutput).toContain(`CREATE apps/${newPath}/tsconfig.app.json`); expect(moveOutput).toContain(`CREATE apps/${newPath}/tsconfig.json`); expect(moveOutput).toContain(`CREATE apps/${newPath}/tsconfig.spec.json`); diff --git a/packages/angular/src/generators/application/__snapshots__/application.spec.ts.snap b/packages/angular/src/generators/application/__snapshots__/application.spec.ts.snap index 7444ec5af6240..52b48853c7825 100644 --- a/packages/angular/src/generators/application/__snapshots__/application.spec.ts.snap +++ b/packages/angular/src/generators/application/__snapshots__/application.spec.ts.snap @@ -112,7 +112,7 @@ Object { "test": Object { "builder": "@nrwl/jest:jest", "options": Object { - "jestConfig": "apps/my-dir/my-app/jest.config.js", + "jestConfig": "apps/my-dir/my-app/jest.config.ts", "passWithNoTests": true, }, "outputs": Array [ @@ -281,7 +281,7 @@ Object { "test": Object { "builder": "@nrwl/jest:jest", "options": Object { - "jestConfig": "apps/my-app/jest.config.js", + "jestConfig": "apps/my-app/jest.config.ts", "passWithNoTests": true, }, "outputs": Array [ diff --git a/packages/angular/src/generators/application/application.spec.ts b/packages/angular/src/generators/application/application.spec.ts index 94cb4ebcbaf67..d533f462f4562 100644 --- a/packages/angular/src/generators/application/application.spec.ts +++ b/packages/angular/src/generators/application/application.spec.ts @@ -70,7 +70,7 @@ describe('app', () => { it('should generate files', async () => { await generateApp(appTree); - expect(appTree.exists(`apps/my-app/jest.config.js`)).toBeTruthy(); + expect(appTree.exists(`apps/my-app/jest.config.ts`)).toBeTruthy(); expect(appTree.exists('apps/my-app/src/main.ts')).toBeTruthy(); expect(appTree.exists('apps/my-app/src/app/app.module.ts')).toBeTruthy(); expect( @@ -113,13 +113,13 @@ describe('app', () => { it('should setup jest with serializers', async () => { await generateApp(appTree); - expect(appTree.read('apps/my-app/jest.config.js', 'utf-8')).toContain( + expect(appTree.read('apps/my-app/jest.config.ts', 'utf-8')).toContain( `'jest-preset-angular/build/serializers/no-ng-attributes'` ); - expect(appTree.read('apps/my-app/jest.config.js', 'utf-8')).toContain( + expect(appTree.read('apps/my-app/jest.config.ts', 'utf-8')).toContain( `'jest-preset-angular/build/serializers/ng-snapshot'` ); - expect(appTree.read('apps/my-app/jest.config.js', 'utf-8')).toContain( + expect(appTree.read('apps/my-app/jest.config.ts', 'utf-8')).toContain( `'jest-preset-angular/build/serializers/html-comment'` ); }); @@ -249,7 +249,7 @@ describe('app', () => { // Make sure these exist [ - `apps/my-dir/my-app/jest.config.js`, + `apps/my-dir/my-app/jest.config.ts`, 'apps/my-dir/my-app/src/main.ts', 'apps/my-dir/my-app/src/app/app.module.ts', 'apps/my-dir/my-app/src/app/app.component.ts', @@ -332,7 +332,7 @@ describe('app', () => { // Make sure these exist [ - 'my-dir/my-app/jest.config.js', + 'my-dir/my-app/jest.config.ts', 'my-dir/my-app/src/main.ts', 'my-dir/my-app/src/app/app.module.ts', 'my-dir/my-app/src/app/app.component.ts', @@ -631,10 +631,10 @@ describe('app', () => { describe('--unit-test-runner', () => { describe('default (jest)', () => { - it('should generate jest.config.js with serializers', async () => { + it('should generate jest.config.ts with serializers', async () => { await generateApp(appTree); - const jestConfig = appTree.read('apps/my-app/jest.config.js', 'utf-8'); + const jestConfig = appTree.read('apps/my-app/jest.config.ts', 'utf-8'); expect(jestConfig).toContain( `'jest-preset-angular/build/serializers/no-ng-attributes'` @@ -678,7 +678,7 @@ describe('app', () => { expect(appTree.exists('apps/my-app/src/test-setup.ts')).toBeFalsy(); expect(appTree.exists('apps/my-app/src/test.ts')).toBeFalsy(); expect(appTree.exists('apps/my-app/tsconfig.spec.json')).toBeFalsy(); - expect(appTree.exists('apps/my-app/jest.config.js')).toBeFalsy(); + expect(appTree.exists('apps/my-app/jest.config.ts')).toBeFalsy(); expect(appTree.exists('apps/my-app/karma.config.js')).toBeFalsy(); expect( appTree.exists('apps/my-app/src/app/app.component.spec.ts') diff --git a/packages/angular/src/generators/init/init.spec.ts b/packages/angular/src/generators/init/init.spec.ts index 2c48ba965851e..cdaefe34ee29d 100644 --- a/packages/angular/src/generators/init/init.spec.ts +++ b/packages/angular/src/generators/init/init.spec.ts @@ -140,7 +140,7 @@ describe('init', () => { skipFormat: false, }); - const hasJestConfigFile = host.exists('jest.config.js'); + const hasJestConfigFile = host.exists('jest.config.ts'); // ASSERT expect(hasJestConfigFile).toBeTruthy(); diff --git a/packages/angular/src/generators/library/library.spec.ts b/packages/angular/src/generators/library/library.spec.ts index 02fadb3c12029..881a588b734a4 100644 --- a/packages/angular/src/generators/library/library.spec.ts +++ b/packages/angular/src/generators/library/library.spec.ts @@ -429,7 +429,7 @@ describe('lib', () => { await runLibraryGeneratorWithOpts({ name: 'my-lib2' }); // ASSERT - expect(tree.exists(`libs/my-lib/jest.config.js`)).toBeTruthy(); + expect(tree.exists(`libs/my-lib/jest.config.ts`)).toBeTruthy(); expect(tree.exists('libs/my-lib/src/index.ts')).toBeTruthy(); expect(tree.exists('libs/my-lib/src/lib/my-lib.module.ts')).toBeTruthy(); @@ -444,7 +444,7 @@ describe('lib', () => { tree.exists('libs/my-lib/src/lib/my-lib.service.spec.ts') ).toBeFalsy(); - expect(tree.exists(`libs/my-lib2/jest.config.js`)).toBeTruthy(); + expect(tree.exists(`libs/my-lib2/jest.config.ts`)).toBeTruthy(); expect(tree.exists('libs/my-lib2/src/index.ts')).toBeTruthy(); expect( tree.exists('libs/my-lib2/src/lib/my-lib2.module.ts') @@ -544,7 +544,7 @@ describe('lib', () => { }); // ASSERT - expect(tree.exists(`libs/my-dir/my-lib/jest.config.js`)).toBeTruthy(); + expect(tree.exists(`libs/my-dir/my-lib/jest.config.ts`)).toBeTruthy(); expect(tree.exists('libs/my-dir/my-lib/src/index.ts')).toBeTruthy(); expect( tree.exists('libs/my-dir/my-lib/src/lib/my-dir-my-lib.module.ts') @@ -563,7 +563,7 @@ describe('lib', () => { tree.exists('libs/my-dir/my-lib/src/lib/my-lib.service.spec.ts') ).toBeFalsy(); - expect(tree.exists(`libs/my-dir/my-lib2/jest.config.js`)).toBeTruthy(); + expect(tree.exists(`libs/my-dir/my-lib2/jest.config.ts`)).toBeTruthy(); expect(tree.exists('libs/my-dir/my-lib2/src/index.ts')).toBeTruthy(); expect( tree.exists('libs/my-dir/my-lib2/src/lib/my-lib2.module.ts') @@ -740,7 +740,7 @@ describe('lib', () => { // Make sure these exist [ 'my-dir/my-lib/.browserslistrc', - 'my-dir/my-lib/jest.config.js', + 'my-dir/my-lib/jest.config.ts', 'my-dir/my-lib/ng-package.json', 'my-dir/my-lib/project.json', 'my-dir/my-lib/tsconfig.lib.prod.json', @@ -1192,7 +1192,7 @@ describe('lib', () => { expect(tree.exists('libs/my-lib/src/test.ts')).toBeFalsy(); expect(tree.exists('libs/my-lib/src/test.ts')).toBeFalsy(); expect(tree.exists('libs/my-lib/tsconfig.spec.json')).toBeFalsy(); - expect(tree.exists('libs/my-lib/jest.config.js')).toBeFalsy(); + expect(tree.exists('libs/my-lib/jest.config.ts')).toBeFalsy(); expect(tree.exists('libs/my-lib/karma.conf.js')).toBeFalsy(); expect(workspaceJson.projects['my-lib'].architect.test).toBeUndefined(); }); diff --git a/packages/angular/src/generators/storybook-configuration/__snapshots__/storybook-configuration.spec.ts.snap b/packages/angular/src/generators/storybook-configuration/__snapshots__/storybook-configuration.spec.ts.snap index 882b4394d9a35..e17bfa9471c28 100644 --- a/packages/angular/src/generators/storybook-configuration/__snapshots__/storybook-configuration.spec.ts.snap +++ b/packages/angular/src/generators/storybook-configuration/__snapshots__/storybook-configuration.spec.ts.snap @@ -54,13 +54,13 @@ Array [ "apps/one/two/test-ui-lib-e2e/src/support/commands.ts", "apps/one/two/test-ui-lib-e2e/src/support/index.ts", "apps/one/two/test-ui-lib-e2e/tsconfig.json", - "jest.config.js", - "jest.preset.js", + "jest.config.ts", + "jest.preset.ts", "libs/test-ui-lib/.eslintrc.json", "libs/test-ui-lib/.storybook/main.js", "libs/test-ui-lib/.storybook/preview.js", "libs/test-ui-lib/.storybook/tsconfig.json", - "libs/test-ui-lib/jest.config.js", + "libs/test-ui-lib/jest.config.ts", "libs/test-ui-lib/README.md", "libs/test-ui-lib/src/index.ts", "libs/test-ui-lib/src/lib/barrel/barrel-button/barrel-button.component.css", @@ -159,13 +159,13 @@ Array [ "apps/test-ui-lib-e2e/src/support/commands.ts", "apps/test-ui-lib-e2e/src/support/index.ts", "apps/test-ui-lib-e2e/tsconfig.json", - "jest.config.js", - "jest.preset.js", + "jest.config.ts", + "jest.preset.ts", "libs/test-ui-lib/.eslintrc.json", "libs/test-ui-lib/.storybook/main.js", "libs/test-ui-lib/.storybook/preview.js", "libs/test-ui-lib/.storybook/tsconfig.json", - "libs/test-ui-lib/jest.config.js", + "libs/test-ui-lib/jest.config.ts", "libs/test-ui-lib/README.md", "libs/test-ui-lib/src/index.ts", "libs/test-ui-lib/src/lib/barrel/barrel-button/barrel-button.component.css", diff --git a/packages/express/src/generators/application/application.spec.ts b/packages/express/src/generators/application/application.spec.ts index 620a4e62051f0..253363078ec0a 100644 --- a/packages/express/src/generators/application/application.spec.ts +++ b/packages/express/src/generators/application/application.spec.ts @@ -90,6 +90,7 @@ Object { ], }, "exclude": Array [ + "jest.config.ts", "**/*.spec.ts", "**/*.test.ts", ], @@ -124,6 +125,7 @@ Object { ); expect(tsConfigApp.include).toEqual(['**/*.ts', '**/*.js']); expect(tsConfigApp.exclude).toEqual([ + 'jest.config.ts', '**/*.spec.ts', '**/*.test.ts', '**/*.spec.js', diff --git a/packages/jest/src/executors/jest/jest.impl.spec.ts b/packages/jest/src/executors/jest/jest.impl.spec.ts index 241903746fed0..7af56a71f2834 100644 --- a/packages/jest/src/executors/jest/jest.impl.spec.ts +++ b/packages/jest/src/executors/jest/jest.impl.spec.ts @@ -70,7 +70,7 @@ describe('Jest Executor', () => { describe('when the jest config file is untouched', () => { beforeEach(() => { jest.mock( - '/root/jest.config.js', + '/root/jest.config.ts', () => ({ transform: {}, }), @@ -82,7 +82,7 @@ describe('Jest Executor', () => { await jestExecutor( { ...defaultOptions, - jestConfig: './jest.config.js', + jestConfig: './jest.config.ts', watch: false, }, mockContext @@ -93,7 +93,7 @@ describe('Jest Executor', () => { testPathPattern: [], watch: false, }), - ['/root/jest.config.js'] + ['/root/jest.config.ts'] ); }); @@ -123,7 +123,7 @@ describe('Jest Executor', () => { await jestExecutor( { testFile: 'lib.spec.ts', - jestConfig: './jest.config.js', + jestConfig: './jest.config.ts', codeCoverage: false, runInBand: true, testNamePattern: 'should load', @@ -152,7 +152,7 @@ describe('Jest Executor', () => { coverageDirectory: '/root/test/coverage', watch: false, }), - ['/root/jest.config.js'] + ['/root/jest.config.ts'] ); }); @@ -161,7 +161,7 @@ describe('Jest Executor', () => { { ...defaultOptions, findRelatedTests: 'file1.ts,file2.ts', - jestConfig: './jest.config.js', + jestConfig: './jest.config.ts', codeCoverage: false, runInBand: true, testNamePattern: 'should load', @@ -180,14 +180,14 @@ describe('Jest Executor', () => { testPathPattern: [], watch: false, }), - ['/root/jest.config.js'] + ['/root/jest.config.ts'] ); }); it('should send other options to jestCLI', async () => { await jestExecutor( { - jestConfig: './jest.config.js', + jestConfig: './jest.config.ts', codeCoverage: true, bail: 1, color: false, @@ -251,7 +251,7 @@ describe('Jest Executor', () => { watchAll: false, testLocationInResults: true, }, - ['/root/jest.config.js'] + ['/root/jest.config.ts'] ); }); @@ -259,7 +259,7 @@ describe('Jest Executor', () => { await jestExecutor( { ...defaultOptions, - jestConfig: './jest.config.js', + jestConfig: './jest.config.ts', maxWorkers: '50%', }, mockContext @@ -270,7 +270,7 @@ describe('Jest Executor', () => { maxWorkers: '50%', testPathPattern: [], }, - ['/root/jest.config.js'] + ['/root/jest.config.ts'] ); }); @@ -278,7 +278,7 @@ describe('Jest Executor', () => { await jestExecutor( { ...defaultOptions, - jestConfig: './jest.config.js', + jestConfig: './jest.config.ts', setupFile: './test-setup.ts', watch: false, }, @@ -291,14 +291,14 @@ describe('Jest Executor', () => { testPathPattern: [], watch: false, }), - ['/root/jest.config.js'] + ['/root/jest.config.ts'] ); }); describe('when the jest config file has been modified', () => { beforeAll(() => { jest.doMock( - '/root/jest.config.js', + '/root/jest.config.ts', () => ({ transform: { '^.+\\.[tj]sx?$': 'ts-jest', @@ -313,7 +313,7 @@ describe('Jest Executor', () => { await jestExecutor( { ...defaultOptions, - jestConfig: './jest.config.js', + jestConfig: './jest.config.ts', setupFile: './test-setup.ts', watch: false, }, @@ -327,7 +327,7 @@ describe('Jest Executor', () => { testPathPattern: [], watch: false, }), - ['/root/jest.config.js'] + ['/root/jest.config.ts'] ); }); }); @@ -335,7 +335,7 @@ describe('Jest Executor', () => { describe('when we use babel-jest', () => { beforeEach(() => { jest.doMock( - '/root/jest.config.js', + '/root/jest.config.ts', () => ({ transform: { '^.+\\.[tj]sx?$': 'babel-jest', @@ -348,7 +348,7 @@ describe('Jest Executor', () => { it('should send appropriate options to jestCLI', async () => { const options: JestExecutorOptions = { ...defaultOptions, - jestConfig: './jest.config.js', + jestConfig: './jest.config.ts', watch: false, }; @@ -359,7 +359,7 @@ describe('Jest Executor', () => { testPathPattern: [], watch: false, }), - ['/root/jest.config.js'] + ['/root/jest.config.ts'] ); }); }); diff --git a/packages/jest/src/generators/init/init.spec.ts b/packages/jest/src/generators/init/init.spec.ts index 93f43b0be174d..1a7c760301c76 100644 --- a/packages/jest/src/generators/init/init.spec.ts +++ b/packages/jest/src/generators/init/init.spec.ts @@ -12,8 +12,8 @@ describe('jest', () => { it('should generate files', async () => { jestInitGenerator(tree, {}); - expect(tree.exists('jest.config.js')).toBeTruthy(); - expect(tree.read('jest.config.js', 'utf-8')).toMatchInlineSnapshot(` + expect(tree.exists('jest.config.ts')).toBeTruthy(); + expect(tree.read('jest.config.ts', 'utf-8')).toMatchInlineSnapshot(` "const { getJestProjects } = require('@nrwl/jest'); module.exports = { @@ -23,9 +23,9 @@ describe('jest', () => { }); it('should not override existing files', async () => { - tree.write('jest.config.js', `test`); + tree.write('jest.config.ts', `test`); jestInitGenerator(tree, {}); - expect(tree.read('jest.config.js', 'utf-8')).toEqual('test'); + expect(tree.read('jest.config.ts', 'utf-8')).toEqual('test'); }); it('should add dependencies', async () => { 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 f0c8ff522bbd4..f14c67e9be05b 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 @@ -3,7 +3,7 @@ exports[`jestProject --babelJest should generate proper jest.transform when --compiler=swc and supportTsx is true 1`] = ` "module.exports = { displayName: 'lib1', - preset: '../../jest.preset.js', + preset: '../../jest.preset.ts', transform: { '^.+\\\\\\\\.[tj]sx?$': ['@swc/jest', { jsc: { transform: { react: { runtime: 'automatic' } } } }] }, @@ -16,7 +16,7 @@ exports[`jestProject --babelJest should generate proper jest.transform when --co exports[`jestProject --babelJest should generate proper jest.transform when babelJest and supportTsx is true 1`] = ` "module.exports = { displayName: 'lib1', - preset: '../../jest.preset.js', + preset: '../../jest.preset.ts', transform: { '^.+\\\\\\\\.[tj]sx?$': 'babel-jest' }, @@ -29,7 +29,7 @@ exports[`jestProject --babelJest should generate proper jest.transform when babe exports[`jestProject --babelJest should generate proper jest.transform when babelJest is true 1`] = ` "module.exports = { displayName: 'lib1', - preset: '../../jest.preset.js', + preset: '../../jest.preset.ts', transform: { '^.+\\\\\\\\.[tj]s$': 'babel-jest' }, @@ -42,7 +42,7 @@ exports[`jestProject --babelJest should generate proper jest.transform when babe exports[`jestProject --setup-file should have setupFilesAfterEnv and globals.ts-jest in the jest.config when generated for angular 1`] = ` "module.exports = { displayName: 'lib1', - preset: '../../jest.preset.js', + preset: '../../jest.preset.ts', setupFilesAfterEnv: ['/src/test-setup.ts'], globals: { 'ts-jest': { @@ -67,7 +67,7 @@ exports[`jestProject --setup-file should have setupFilesAfterEnv and globals.ts- exports[`jestProject should create a jest.config.ts 1`] = ` "module.exports = { displayName: 'lib1', - preset: '../../jest.preset.js', + preset: '../../jest.preset.ts', globals: { 'ts-jest': { tsconfig: '/tsconfig.spec.json', 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 84a96499dbb2b..bc79b33f98627 100644 --- a/packages/jest/src/generators/jest-project/jest-project.spec.ts +++ b/packages/jest/src/generators/jest-project/jest-project.spec.ts @@ -51,7 +51,7 @@ describe('jestProject', () => { setupFile: 'angular', } as JestProjectSchema); expect(tree.exists('libs/lib1/src/test-setup.ts')).toBeTruthy(); - expect(tree.exists('libs/lib1/jest.config.js')).toBeTruthy(); + expect(tree.exists('libs/lib1/jest.config.ts')).toBeTruthy(); expect(tree.exists('libs/lib1/tsconfig.spec.json')).toBeTruthy(); }); @@ -75,7 +75,7 @@ describe('jestProject', () => { executor: '@nrwl/jest:jest', outputs: ['coverage/libs/lib1'], options: { - jestConfig: 'libs/lib1/jest.config.js', + jestConfig: 'libs/lib1/jest.config.ts', passWithNoTests: true, }, }); @@ -84,12 +84,12 @@ describe('jestProject', () => { ); }); - it('should create a jest.config.js', async () => { + it('should create a jest.config.ts', async () => { await jestProjectGenerator(tree, { ...defaultOptions, project: 'lib1', } as JestProjectSchema); - expect(tree.read('libs/lib1/jest.config.js', 'utf-8')).toMatchSnapshot(); + expect(tree.read('libs/lib1/jest.config.ts', 'utf-8')).toMatchSnapshot(); }); it('should add a reference to solution tsconfig.json', async () => { @@ -129,7 +129,7 @@ describe('jestProject', () => { project: 'lib1', } as JestProjectSchema); expect(tree.exists('src/test-setup.ts')).toBeFalsy(); - expect(tree.read('libs/lib1/jest.config.js', 'utf-8')).not.toContain( + expect(tree.read('libs/lib1/jest.config.ts', 'utf-8')).not.toContain( `setupFilesAfterEnv: ['/src/test-setup.ts'],` ); }); @@ -140,7 +140,7 @@ describe('jestProject', () => { project: 'lib1', setupFile: 'web-components', } as JestProjectSchema); - expect(tree.read('libs/lib1/jest.config.js', 'utf-8')).toContain( + expect(tree.read('libs/lib1/jest.config.ts', 'utf-8')).toContain( `setupFilesAfterEnv: ['/src/test-setup.ts'],` ); }); @@ -152,7 +152,7 @@ describe('jestProject', () => { setupFile: 'angular', } as JestProjectSchema); - const jestConfig = tree.read('libs/lib1/jest.config.js', 'utf-8'); + const jestConfig = tree.read('libs/lib1/jest.config.ts', 'utf-8'); expect(jestConfig).toContain( `setupFilesAfterEnv: ['/src/test-setup.ts'],` ); @@ -212,13 +212,13 @@ describe('jestProject', () => { }); describe('--skip-serializers', () => { - it('should not list the serializers in jest.config.js', async () => { + it('should not list the serializers in jest.config.ts', async () => { await jestProjectGenerator(tree, { ...defaultOptions, project: 'lib1', skipSerializers: true, } as JestProjectSchema); - const jestConfig = tree.read('libs/lib1/jest.config.js', 'utf-8'); + const jestConfig = tree.read('libs/lib1/jest.config.ts', 'utf-8'); expect(jestConfig).not.toContain(` snapshotSerializers: [ 'jest-preset-angular/build/AngularNoNgAttributesSnapshotSerializer.js, @@ -236,7 +236,7 @@ describe('jestProject', () => { project: 'lib1', supportTsx: true, } as JestProjectSchema); - const jestConfig = jestConfigObject(tree, 'libs/lib1/jest.config.js'); + const jestConfig = jestConfigObject(tree, 'libs/lib1/jest.config.ts'); expect(jestConfig.transform).toEqual({ '^.+\\.[tj]sx?$': 'ts-jest', }); @@ -248,7 +248,7 @@ describe('jestProject', () => { project: 'lib1', supportTsx: true, } as JestProjectSchema); - const jestConfig = jestConfigObject(tree, 'libs/lib1/jest.config.js'); + const jestConfig = jestConfigObject(tree, 'libs/lib1/jest.config.ts'); expect(jestConfig.moduleFileExtensions).toEqual([ 'ts', 'tsx', @@ -265,7 +265,7 @@ describe('jestProject', () => { project: 'lib1', babelJest: false, } as JestProjectSchema); - const jestConfig = jestConfigObject(tree, 'libs/lib1/jest.config.js'); + const jestConfig = jestConfigObject(tree, 'libs/lib1/jest.config.ts'); expect(jestConfig.globals).toEqual({ 'ts-jest': { @@ -280,14 +280,14 @@ describe('jestProject', () => { project: 'lib1', babelJest: true, } as JestProjectSchema); - const jestConfig = jestConfigObject(tree, 'libs/lib1/jest.config.js'); + const jestConfig = jestConfigObject(tree, 'libs/lib1/jest.config.ts'); expect(jestConfig.globals).not.toEqual({ 'ts-jest': { tsConfig: '/tsconfig.spec.json', }, }); - expect(tree.read('libs/lib1/jest.config.js', 'utf-8')).toMatchSnapshot(); + expect(tree.read('libs/lib1/jest.config.ts', 'utf-8')).toMatchSnapshot(); }); it('should generate proper jest.transform when babelJest and supportTsx is true', async () => { @@ -297,7 +297,7 @@ describe('jestProject', () => { babelJest: true, supportTsx: true, } as JestProjectSchema); - expect(tree.read('libs/lib1/jest.config.js', 'utf-8')).toMatchSnapshot(); + expect(tree.read('libs/lib1/jest.config.ts', 'utf-8')).toMatchSnapshot(); }); it('should generate proper jest.transform when --compiler=swc and supportTsx is true', async () => { @@ -307,7 +307,7 @@ describe('jestProject', () => { compiler: 'swc', supportTsx: true, } as JestProjectSchema); - expect(tree.read('libs/lib1/jest.config.js', 'utf-8')).toMatchSnapshot(); + expect(tree.read('libs/lib1/jest.config.ts', 'utf-8')).toMatchSnapshot(); }); }); }); diff --git a/packages/js/src/generators/library/library.spec.ts b/packages/js/src/generators/library/library.spec.ts index 1c2d4337b31ea..34f8af52e7086 100644 --- a/packages/js/src/generators/library/library.spec.ts +++ b/packages/js/src/generators/library/library.spec.ts @@ -211,7 +211,7 @@ describe('lib', () => { name: 'myLib', directory: 'myDir', }); - expect(tree.exists(`libs/my-dir/my-lib/jest.config.js`)).toBeTruthy(); + expect(tree.exists(`libs/my-dir/my-lib/jest.config.ts`)).toBeTruthy(); expect(tree.exists('libs/my-dir/my-lib/src/index.ts')).toBeTruthy(); expect( tree.exists('libs/my-dir/my-lib/src/lib/my-dir-my-lib.ts') @@ -570,7 +570,7 @@ describe('lib', () => { name: 'myLib', js: true, }); - expect(tree.exists(`libs/my-lib/jest.config.js`)).toBeTruthy(); + expect(tree.exists(`libs/my-lib/jest.config.ts`)).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.js`)).toBeTruthy(); + expect(tree.exists(`libs/my-dir/my-lib/jest.config.ts`)).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') @@ -688,18 +688,18 @@ describe('lib', () => { }); expect(tree.exists('libs/my-lib/tsconfig.spec.json')).toBeTruthy(); - expect(tree.exists('libs/my-lib/jest.config.js')).toBeTruthy(); + expect(tree.exists('libs/my-lib/jest.config.ts')).toBeTruthy(); expect(tree.exists('libs/my-lib/src/lib/my-lib.spec.ts')).toBeTruthy(); const projectConfig = readProjectConfiguration(tree, 'my-lib'); expect(projectConfig.targets.test).toBeDefined(); - expect(tree.exists(`libs/my-lib/jest.config.js`)).toBeTruthy(); - expect(tree.read(`libs/my-lib/jest.config.js`, 'utf-8')) + expect(tree.exists(`libs/my-lib/jest.config.ts`)).toBeTruthy(); + expect(tree.read(`libs/my-lib/jest.config.ts`, 'utf-8')) .toMatchInlineSnapshot(` "module.exports = { displayName: 'my-lib', - preset: '../../jest.preset.js', + preset: '../../jest.preset.ts', globals: { 'ts-jest': { tsconfig: '/tsconfig.spec.json', @@ -779,7 +779,7 @@ describe('lib', () => { compiler: 'swc', }); - const jestConfig = tree.read('libs/my-lib/jest.config.js').toString(); + const jestConfig = tree.read('libs/my-lib/jest.config.ts').toString(); expect(jestConfig).toContain('@swc/jest'); }); diff --git a/packages/js/src/migrations/update-13-10-1/__snapshots__/update-lib-swcrc-exclude.spec.ts.snap b/packages/js/src/migrations/update-13-10-1/__snapshots__/update-lib-swcrc-exclude.spec.ts.snap index c32b52734d3fc..dbd56e68bf316 100644 --- a/packages/js/src/migrations/update-13-10-1/__snapshots__/update-lib-swcrc-exclude.spec.ts.snap +++ b/packages/js/src/migrations/update-13-10-1/__snapshots__/update-lib-swcrc-exclude.spec.ts.snap @@ -24,6 +24,7 @@ exports[`Update .lib.swcrc exclude should update the exclude pattern 1`] = ` }, \\"sourceMaps\\": true, \\"exclude\\": [ + \\"jest.config.ts\\", \\".*.spec.tsx?$\\", \\".*.test.tsx?$\\", \\"./src/jest-setup.ts$\\", diff --git a/packages/linter/src/generators/workspace-rules-project/__snapshots__/workspace-rules-project.spec.ts.snap b/packages/linter/src/generators/workspace-rules-project/__snapshots__/workspace-rules-project.spec.ts.snap index 0c29a557489a6..ba1df99284bf7 100644 --- a/packages/linter/src/generators/workspace-rules-project/__snapshots__/workspace-rules-project.spec.ts.snap +++ b/packages/linter/src/generators/workspace-rules-project/__snapshots__/workspace-rules-project.spec.ts.snap @@ -84,7 +84,7 @@ exports[`@nrwl/linter:workspace-rules-project should generate the required files exports[`@nrwl/linter:workspace-rules-project should generate the required files 5`] = ` "module.exports = { displayName: 'eslint-rules', - preset: '../../jest.preset.js', + preset: '../../jest.preset.ts', globals: { 'ts-jest': { tsconfig: '/tsconfig.spec.json', diff --git a/packages/linter/src/generators/workspace-rules-project/workspace-rules-project.spec.ts b/packages/linter/src/generators/workspace-rules-project/workspace-rules-project.spec.ts index 164206b967674..37cd84f6d371e 100644 --- a/packages/linter/src/generators/workspace-rules-project/workspace-rules-project.spec.ts +++ b/packages/linter/src/generators/workspace-rules-project/workspace-rules-project.spec.ts @@ -46,7 +46,7 @@ describe('@nrwl/linter:workspace-rules-project', () => { tree.read('tools/eslint-rules/tsconfig.spec.json', 'utf-8') ).toMatchSnapshot(); expect( - tree.read('tools/eslint-rules/jest.config.js', 'utf-8') + tree.read('tools/eslint-rules/jest.config.ts', 'utf-8') ).toMatchSnapshot(); }); @@ -78,7 +78,7 @@ describe('@nrwl/linter:workspace-rules-project', () => { "test": Object { "executor": "@nrwl/jest:jest", "options": Object { - "jestConfig": "tools/eslint-rules/jest.config.js", + "jestConfig": "tools/eslint-rules/jest.config.ts", "passWithNoTests": true, }, "outputs": Array [ diff --git a/packages/nest/src/generators/application/application.spec.ts b/packages/nest/src/generators/application/application.spec.ts index c31ef0181a643..434e27688af53 100644 --- a/packages/nest/src/generators/application/application.spec.ts +++ b/packages/nest/src/generators/application/application.spec.ts @@ -52,7 +52,11 @@ describe('application generator', () => { ); expect(tsConfig.compilerOptions.emitDecoratorMetadata).toBe(true); expect(tsConfig.compilerOptions.target).toBe('es2015'); - expect(tsConfig.exclude).toEqual(['**/*.spec.ts', '**/*.test.ts']); + expect(tsConfig.exclude).toEqual([ + 'jest.config.ts', + '**/*.spec.ts', + '**/*.test.ts', + ]); }); describe('--skipFormat', () => { diff --git a/packages/nest/src/generators/init/init.spec.ts b/packages/nest/src/generators/init/init.spec.ts index d14416cf141ef..3e77f80a0f580 100644 --- a/packages/nest/src/generators/init/init.spec.ts +++ b/packages/nest/src/generators/init/init.spec.ts @@ -61,13 +61,13 @@ describe('init generator', () => { it('should add jest config when unitTestRunner is jest', async () => { await initGenerator(tree, { unitTestRunner: 'jest' }); - expect(tree.exists('jest.config.js')).toBe(true); + expect(tree.exists('jest.config.ts')).toBe(true); }); it('should not add jest config when unitTestRunner is none', async () => { await initGenerator(tree, { unitTestRunner: 'none' }); - expect(tree.exists('jest.config.js')).toBe(false); + expect(tree.exists('jest.config.ts')).toBe(false); }); describe('--skipFormat', () => { diff --git a/packages/nest/src/generators/library/__snapshots__/library.spec.ts.snap b/packages/nest/src/generators/library/__snapshots__/library.spec.ts.snap index 4adc174f94238..130e8779108fb 100644 --- a/packages/nest/src/generators/library/__snapshots__/library.spec.ts.snap +++ b/packages/nest/src/generators/library/__snapshots__/library.spec.ts.snap @@ -3,7 +3,7 @@ exports[`lib --testEnvironment should set target jest testEnvironment to jsdom 1`] = ` "module.exports = { displayName: 'my-lib', - preset: '../../jest.preset.js', + preset: '../../jest.preset.ts', globals: { 'ts-jest': { tsconfig: '/tsconfig.spec.json', @@ -21,7 +21,7 @@ exports[`lib --testEnvironment should set target jest testEnvironment to jsdom 1 exports[`lib --testEnvironment should set target jest testEnvironment to node by default 1`] = ` "module.exports = { displayName: 'my-lib', - preset: '../../jest.preset.js', + preset: '../../jest.preset.ts', globals: { 'ts-jest': { tsconfig: '/tsconfig.spec.json', diff --git a/packages/nest/src/generators/library/library.spec.ts b/packages/nest/src/generators/library/library.spec.ts index f18e2ec175833..79cc0abbac7a1 100644 --- a/packages/nest/src/generators/library/library.spec.ts +++ b/packages/nest/src/generators/library/library.spec.ts @@ -36,7 +36,7 @@ describe('lib', () => { builder: '@nrwl/jest:jest', outputs: [`coverage/libs/${libFileName}`], options: { - jestConfig: `libs/${libFileName}/jest.config.js`, + jestConfig: `libs/${libFileName}/jest.config.ts`, passWithNoTests: true, }, }); @@ -156,13 +156,17 @@ describe('lib', () => { `libs/${libFileName}/tsconfig.lib.json` ); expect(tsconfigJson.extends).toEqual('./tsconfig.json'); - expect(tsconfigJson.exclude).toEqual(['**/*.spec.ts', '**/*.test.ts']); + expect(tsconfigJson.exclude).toEqual([ + 'jest.config.ts', + '**/*.spec.ts', + '**/*.test.ts', + ]); }); it('should generate files', async () => { await libraryGenerator(tree, { name: libName }); - expect(tree.exists(`libs/${libFileName}/jest.config.js`)).toBeTruthy(); + expect(tree.exists(`libs/${libFileName}/jest.config.ts`)).toBeTruthy(); expect(tree.exists(`libs/${libFileName}/src/index.ts`)).toBeTruthy(); expect( tree.exists(`libs/${libFileName}/src/lib/${libFileName}.spec.ts`) @@ -195,7 +199,7 @@ describe('lib', () => { await libraryGenerator(tree, { name: libName, directory: dirName }); expect( - tree.exists(`libs/${dirFileName}/${libFileName}/jest.config.js`) + tree.exists(`libs/${dirFileName}/${libFileName}/jest.config.ts`) ).toBeTruthy(); expect( tree.exists(`libs/${dirFileName}/${libFileName}/src/index.ts`) @@ -285,7 +289,7 @@ describe('lib', () => { await libraryGenerator(tree, { name: libName, unitTestRunner: 'none' }); expect(tree.exists(`libs/${libFileName}/tsconfig.spec.json`)).toBeFalsy(); - expect(tree.exists(`libs/${libFileName}/jest.config.js`)).toBeFalsy(); + expect(tree.exists(`libs/${libFileName}/jest.config.ts`)).toBeFalsy(); expect( tree.exists(`libs/${libFileName}/lib/${libFileName}.spec.ts`) ).toBeFalsy(); @@ -358,7 +362,7 @@ describe('lib', () => { await libraryGenerator(tree, { name: libName }); expect( - tree.read(`libs/${libFileName}/jest.config.js`, 'utf-8') + tree.read(`libs/${libFileName}/jest.config.ts`, 'utf-8') ).toMatchSnapshot(); }); @@ -366,7 +370,7 @@ describe('lib', () => { await libraryGenerator(tree, { name: libName, testEnvironment: 'jsdom' }); expect( - tree.read(`libs/${libFileName}/jest.config.js`, 'utf-8') + tree.read(`libs/${libFileName}/jest.config.ts`, 'utf-8') ).toMatchSnapshot(); }); }); diff --git a/packages/next/src/generators/application/application.spec.ts b/packages/next/src/generators/application/application.spec.ts index 4fa74ffbf734d..30e5a94e52a73 100644 --- a/packages/next/src/generators/application/application.spec.ts +++ b/packages/next/src/generators/application/application.spec.ts @@ -223,7 +223,7 @@ describe('app', () => { standaloneConfig: false, }); - expect(tree.read('apps/my-app/jest.config.js', 'utf-8')).toContain( + expect(tree.read('apps/my-app/jest.config.ts', 'utf-8')).toContain( `moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],` ); }); @@ -235,7 +235,7 @@ describe('app', () => { standaloneConfig: false, }); - expect(tree.read('apps/my-app/jest.config.js', 'utf-8')).toContain( + expect(tree.read('apps/my-app/jest.config.ts', 'utf-8')).toContain( `'^(?!.*\\\\.(js|jsx|ts|tsx|css|json)$)': '@nrwl/react/plugins/jest'` ); }); @@ -302,7 +302,7 @@ describe('app', () => { unitTestRunner: 'none', standaloneConfig: false, }); - expect(tree.exists('jest.config.js')).toBeFalsy(); + expect(tree.exists('jest.config.ts')).toBeFalsy(); expect(tree.exists('apps/my-app/specs/index.spec.tsx')).toBeFalsy(); }); }); diff --git a/packages/next/src/migrations/update-13-1-1/enable-swc.spec.ts b/packages/next/src/migrations/update-13-1-1/enable-swc.spec.ts index 99b0dae4055e6..a673b33dc08e1 100644 --- a/packages/next/src/migrations/update-13-1-1/enable-swc.spec.ts +++ b/packages/next/src/migrations/update-13-1-1/enable-swc.spec.ts @@ -1,4 +1,8 @@ -import { Tree } from '@nrwl/devkit'; +import { + readProjectConfiguration, + Tree, + updateProjectConfiguration, +} from '@nrwl/devkit'; import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; import { applicationGenerator } from '../../generators/application/application'; @@ -18,6 +22,20 @@ describe('Migration: enable SWC', () => { skipFormat: false, swc: false, }); + // rename jest config to js as that was standard at this version of nx + tree.delete('apps/demo/jest.config.ts'); + updateProjectConfiguration(tree, 'demo', { + ...readProjectConfiguration(tree, 'demo'), + targets: { + test: { + executor: '@nrwl/jest:jest', + options: { + jestConfig: 'apps/demo/jest.config.js', + passWithNoTests: true, + }, + }, + }, + }); // Config that isn't configured properly tree.write( @@ -53,7 +71,20 @@ module.exports = { skipFormat: false, swc: true, }); - + // rename jest config to js as that was standard at this version of nx + tree.delete('apps/demo/jest.config.ts'); + updateProjectConfiguration(tree, 'demo', { + ...readProjectConfiguration(tree, 'demo'), + targets: { + test: { + executor: '@nrwl/jest:jest', + options: { + jestConfig: 'apps/demo/jest.config.js', + passWithNoTests: true, + }, + }, + }, + }); // Config that isn't configured properly tree.write( 'apps/demo/jest.config.js', @@ -85,7 +116,20 @@ module.exports = { skipFormat: false, swc: false, }); - + // rename jest config to js as that was standard at this version of nx + tree.rename('apps/demo/jest.config.ts', 'apps/demo/jest.config.js'); + updateProjectConfiguration(tree, 'demo', { + ...readProjectConfiguration(tree, 'demo'), + targets: { + test: { + executor: '@nrwl/jest:jest', + options: { + jestConfig: 'apps/demo/jest.config.js', + passWithNoTests: true, + }, + }, + }, + }); tree.write( 'apps/demo/.babelrc', `{ diff --git a/packages/node/src/generators/application/application.spec.ts b/packages/node/src/generators/application/application.spec.ts index f64cfdb486f70..a0b0fbead3575 100644 --- a/packages/node/src/generators/application/application.spec.ts +++ b/packages/node/src/generators/application/application.spec.ts @@ -104,7 +104,7 @@ describe('app', () => { name: 'myNodeApp', standaloneConfig: false, }); - expect(tree.exists(`apps/my-node-app/jest.config.js`)).toBeTruthy(); + expect(tree.exists(`apps/my-node-app/jest.config.ts`)).toBeTruthy(); expect(tree.exists('apps/my-node-app/src/main.ts')).toBeTruthy(); const tsconfig = readJson(tree, 'apps/my-node-app/tsconfig.json'); @@ -127,7 +127,11 @@ describe('app', () => { const tsconfigApp = readJson(tree, 'apps/my-node-app/tsconfig.app.json'); expect(tsconfigApp.compilerOptions.outDir).toEqual('../../dist/out-tsc'); expect(tsconfigApp.extends).toEqual('./tsconfig.json'); - expect(tsconfigApp.exclude).toEqual(['**/*.spec.ts', '**/*.test.ts']); + expect(tsconfigApp.exclude).toEqual([ + 'jest.config.ts', + '**/*.spec.ts', + '**/*.test.ts', + ]); const eslintrc = readJson(tree, 'apps/my-node-app/.eslintrc.json'); expect(eslintrc).toMatchInlineSnapshot(` Object { @@ -236,7 +240,7 @@ describe('app', () => { // Make sure these exist [ - `apps/my-dir/my-node-app/jest.config.js`, + `apps/my-dir/my-node-app/jest.config.ts`, 'apps/my-dir/my-node-app/src/main.ts', ].forEach((path) => { expect(tree.exists(path)).toBeTruthy(); @@ -257,7 +261,7 @@ describe('app', () => { { path: 'apps/my-dir/my-node-app/tsconfig.app.json', lookupFn: (json) => json.exclude, - expectedValue: ['**/*.spec.ts', '**/*.test.ts'], + expectedValue: ['jest.config.ts', '**/*.spec.ts', '**/*.test.ts'], }, { path: 'apps/my-dir/my-node-app/.eslintrc.json', @@ -275,11 +279,11 @@ describe('app', () => { unitTestRunner: 'none', standaloneConfig: false, }); - expect(tree.exists('jest.config.js')).toBeFalsy(); + expect(tree.exists('jest.config.ts')).toBeFalsy(); expect(tree.exists('apps/my-node-app/src/test-setup.ts')).toBeFalsy(); expect(tree.exists('apps/my-node-app/src/test.ts')).toBeFalsy(); expect(tree.exists('apps/my-node-app/tsconfig.spec.json')).toBeFalsy(); - expect(tree.exists('apps/my-node-app/jest.config.js')).toBeFalsy(); + expect(tree.exists('apps/my-node-app/jest.config.ts')).toBeFalsy(); const workspaceJson = readJson(tree, 'workspace.json'); expect( workspaceJson.projects['my-node-app'].architect.test @@ -368,11 +372,11 @@ describe('app', () => { babelJest: true, } as Schema); - expect(tree.read(`apps/my-node-app/jest.config.js`, 'utf-8')) + expect(tree.read(`apps/my-node-app/jest.config.ts`, 'utf-8')) .toMatchInlineSnapshot(` "module.exports = { displayName: 'my-node-app', - preset: '../../jest.preset.js', + preset: '../../jest.preset.ts', testEnvironment: 'node', transform: { '^.+\\\\\\\\.[tj]s$': 'babel-jest' @@ -391,7 +395,7 @@ describe('app', () => { js: true, } as Schema); - expect(tree.exists(`apps/my-node-app/jest.config.js`)).toBeTruthy(); + expect(tree.exists(`apps/my-node-app/jest.config.ts`)).toBeTruthy(); expect(tree.exists('apps/my-node-app/src/main.js')).toBeTruthy(); const tsConfig = readJson(tree, 'apps/my-node-app/tsconfig.json'); @@ -402,6 +406,7 @@ describe('app', () => { const tsConfigApp = readJson(tree, 'apps/my-node-app/tsconfig.app.json'); expect(tsConfigApp.include).toEqual(['**/*.ts', '**/*.js']); expect(tsConfigApp.exclude).toEqual([ + 'jest.config.ts', '**/*.spec.ts', '**/*.test.ts', '**/*.spec.js', @@ -434,7 +439,7 @@ describe('app', () => { js: true, } as Schema); expect( - tree.exists(`apps/my-dir/my-node-app/jest.config.js`) + tree.exists(`apps/my-dir/my-node-app/jest.config.ts`) ).toBeTruthy(); expect(tree.exists('apps/my-dir/my-node-app/src/main.js')).toBeTruthy(); }); diff --git a/packages/node/src/generators/library/library.spec.ts b/packages/node/src/generators/library/library.spec.ts index 68d921400e830..b7091a8adbb9d 100644 --- a/packages/node/src/generators/library/library.spec.ts +++ b/packages/node/src/generators/library/library.spec.ts @@ -40,7 +40,7 @@ describe('lib', () => { builder: '@nrwl/jest:jest', outputs: ['coverage/libs/my-lib'], options: { - jestConfig: 'libs/my-lib/jest.config.js', + jestConfig: 'libs/my-lib/jest.config.ts', passWithNoTests: true, }, }); @@ -116,12 +116,16 @@ describe('lib', () => { it('should exclude test files from tsconfig.lib.json', async () => { await libraryGenerator(tree, baseLibraryConfig); const tsconfigJson = readJson(tree, 'libs/my-lib/tsconfig.lib.json'); - expect(tsconfigJson.exclude).toEqual(['**/*.spec.ts', '**/*.test.ts']); + expect(tsconfigJson.exclude).toEqual([ + 'jest.config.ts', + '**/*.spec.ts', + '**/*.test.ts', + ]); }); it('should generate files', async () => { await libraryGenerator(tree, baseLibraryConfig); - expect(tree.exists(`libs/my-lib/jest.config.js`)).toBeTruthy(); + expect(tree.exists(`libs/my-lib/jest.config.ts`)).toBeTruthy(); expect(tree.exists('libs/my-lib/src/index.ts')).toBeTruthy(); const eslintrc = readJson(tree, 'libs/my-lib/.eslintrc.json'); @@ -199,7 +203,7 @@ describe('lib', () => { ...baseLibraryConfig, directory: 'myDir', }); - expect(tree.exists(`libs/my-dir/my-lib/jest.config.js`)).toBeTruthy(); + expect(tree.exists(`libs/my-dir/my-lib/jest.config.ts`)).toBeTruthy(); expect(tree.exists('libs/my-dir/my-lib/src/index.ts')).toBeTruthy(); }); @@ -276,7 +280,7 @@ describe('lib', () => { directory: 'myDir', simpleModuleName: true, }); - expect(tree.exists(`libs/my-dir/my-lib/jest.config.js`)).toBeTruthy(); + expect(tree.exists(`libs/my-dir/my-lib/jest.config.ts`)).toBeTruthy(); expect(tree.exists('libs/my-dir/my-lib/src/index.ts')).toBeTruthy(); expect(tree.exists('libs/my-dir/my-lib/src/lib/my-lib.ts')).toBeTruthy(); expect( @@ -320,7 +324,7 @@ describe('lib', () => { unitTestRunner: 'none', }); expect(tree.exists('libs/my-lib/tsconfig.spec.json')).toBeFalsy(); - expect(tree.exists('libs/my-lib/jest.config.js')).toBeFalsy(); + expect(tree.exists('libs/my-lib/jest.config.ts')).toBeFalsy(); expect(tree.exists('libs/my-lib/lib/my-lib.spec.ts')).toBeFalsy(); const workspaceJson = readJson(tree, 'workspace.json'); expect(workspaceJson.projects['my-lib'].architect.test).toBeUndefined(); @@ -456,11 +460,11 @@ describe('lib', () => { babelJest: true, } as Schema); - expect(tree.read(`libs/my-lib/jest.config.js`, 'utf-8')) + expect(tree.read(`libs/my-lib/jest.config.ts`, 'utf-8')) .toMatchInlineSnapshot(` "module.exports = { displayName: 'my-lib', - preset: '../../jest.preset.js', + preset: '../../jest.preset.ts', testEnvironment: 'node', transform: { '^.+\\\\\\\\.[tj]sx?$': 'babel-jest' @@ -494,6 +498,7 @@ describe('lib', () => { '**/*.js', ]); expect(readJson(tree, 'libs/my-lib/tsconfig.lib.json').exclude).toEqual([ + 'jest.config.ts', '**/*.spec.ts', '**/*.test.ts', '**/*.spec.js', diff --git a/packages/nx-plugin/src/generators/e2e-project/e2e.spec.ts b/packages/nx-plugin/src/generators/e2e-project/e2e.spec.ts index ffd2484d273d6..a765284adc36c 100644 --- a/packages/nx-plugin/src/generators/e2e-project/e2e.spec.ts +++ b/packages/nx-plugin/src/generators/e2e-project/e2e.spec.ts @@ -140,11 +140,11 @@ describe('NxPlugin e2e-project Generator', () => { expect(project.targets.e2e).toMatchObject({ options: expect.objectContaining({ - jestConfig: 'apps/my-plugin-e2e/jest.config.js', + jestConfig: 'apps/my-plugin-e2e/jest.config.ts', }), }); expect(tree.exists('apps/my-plugin-e2e/tsconfig.spec.json')).toBeTruthy(); - expect(tree.exists('apps/my-plugin-e2e/jest.config.js')).toBeTruthy(); + expect(tree.exists('apps/my-plugin-e2e/jest.config.ts')).toBeTruthy(); }); }); diff --git a/packages/nx-plugin/src/generators/plugin/plugin.spec.ts b/packages/nx-plugin/src/generators/plugin/plugin.spec.ts index 1deec71cbcf51..cbb6f21bec38b 100644 --- a/packages/nx-plugin/src/generators/plugin/plugin.spec.ts +++ b/packages/nx-plugin/src/generators/plugin/plugin.spec.ts @@ -75,7 +75,7 @@ describe('NxPlugin Plugin Generator', () => { executor: '@nrwl/jest:jest', outputs: ['coverage/libs/my-plugin'], options: { - jestConfig: 'libs/my-plugin/jest.config.js', + jestConfig: 'libs/my-plugin/jest.config.ts', passWithNoTests: true, }, }); diff --git a/packages/react-native/src/generators/library/library.spec.ts b/packages/react-native/src/generators/library/library.spec.ts index 985673fe9d681..01c5e6612efee 100644 --- a/packages/react-native/src/generators/library/library.spec.ts +++ b/packages/react-native/src/generators/library/library.spec.ts @@ -226,7 +226,7 @@ describe('lib', () => { }); expect(appTree.exists('libs/my-lib/tsconfig.spec.json')).toBeFalsy(); - expect(appTree.exists('libs/my-lib/jest.config.js')).toBeFalsy(); + expect(appTree.exists('libs/my-lib/jest.config.ts')).toBeFalsy(); const workspaceJson = readJson(appTree, 'workspace.json'); expect(workspaceJson.projects['my-lib'].architect.test).toBeUndefined(); expect(workspaceJson.projects['my-lib'].architect.lint) diff --git a/packages/react/src/generators/application/application.spec.ts b/packages/react/src/generators/application/application.spec.ts index beab116c056a7..217eeec4f6460 100644 --- a/packages/react/src/generators/application/application.spec.ts +++ b/packages/react/src/generators/application/application.spec.ts @@ -64,7 +64,7 @@ describe('app', () => { expect(appTree.exists('apps/my-app/src/app/app.spec.tsx')).toBeTruthy(); expect(appTree.exists('apps/my-app/src/app/app.module.css')).toBeTruthy(); - const jestConfig = appTree.read('apps/my-app/jest.config.js').toString(); + const jestConfig = appTree.read('apps/my-app/jest.config.ts').toString(); expect(jestConfig).toContain('@nrwl/react/plugins/jest'); const tsconfig = readJson(appTree, 'apps/my-app/tsconfig.json'); @@ -91,6 +91,7 @@ describe('app', () => { expect(tsconfigApp.compilerOptions.outDir).toEqual('../../dist/out-tsc'); expect(tsconfigApp.extends).toEqual('./tsconfig.json'); expect(tsconfigApp.exclude).toEqual([ + 'jest.config.ts', '**/*.spec.ts', '**/*.test.ts', '**/*.spec.tsx', @@ -208,6 +209,7 @@ Object { path: 'apps/my-dir/my-app/tsconfig.app.json', lookupFn: (json) => json.exclude, expectedValue: [ + 'jest.config.ts', '**/*.spec.ts', '**/*.test.ts', '**/*.spec.tsx', @@ -274,7 +276,7 @@ Object { it('should setup jest with tsx support', async () => { await applicationGenerator(appTree, { ...schema, name: 'my-app' }); - expect(appTree.read('apps/my-app/jest.config.js').toString()).toContain( + expect(appTree.read('apps/my-app/jest.config.ts').toString()).toContain( `moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],` ); }); @@ -282,7 +284,7 @@ Object { it('should setup jest without serializers', async () => { await applicationGenerator(appTree, { ...schema, name: 'my-app' }); - expect(appTree.read('apps/my-app/jest.config.js').toString()).not.toContain( + expect(appTree.read('apps/my-app/jest.config.ts').toString()).not.toContain( `'jest-preset-angular/build/AngularSnapshotSerializer.js',` ); }); @@ -359,10 +361,10 @@ Object { unitTestRunner: 'none', }); - expect(appTree.exists('jest.config.js')).toBeFalsy(); + expect(appTree.exists('jest.config.ts')).toBeFalsy(); expect(appTree.exists('apps/my-app/src/app/app.spec.tsx')).toBeFalsy(); expect(appTree.exists('apps/my-app/tsconfig.spec.json')).toBeFalsy(); - expect(appTree.exists('apps/my-app/jest.config.js')).toBeFalsy(); + expect(appTree.exists('apps/my-app/jest.config.ts')).toBeFalsy(); const workspaceJson = getProjects(appTree); expect(workspaceJson.get('my-app').targets.test).toBeUndefined(); expect(workspaceJson.get('my-app').targets.lint).toMatchInlineSnapshot(` diff --git a/packages/react/src/generators/library/library.spec.ts b/packages/react/src/generators/library/library.spec.ts index 096f89c6ff0e3..a5b804887ad47 100644 --- a/packages/react/src/generators/library/library.spec.ts +++ b/packages/react/src/generators/library/library.spec.ts @@ -152,6 +152,7 @@ describe('lib', () => { await libraryGenerator(appTree, defaultSchema); const tsconfigJson = readJson(appTree, 'libs/my-lib/tsconfig.lib.json'); expect(tsconfigJson.exclude).toEqual([ + 'jest.config.ts', '**/*.spec.ts', '**/*.test.ts', '**/*.spec.tsx', @@ -166,7 +167,7 @@ describe('lib', () => { it('should generate files', async () => { await libraryGenerator(appTree, defaultSchema); expect(appTree.exists('libs/my-lib/package.json')).toBeFalsy(); - expect(appTree.exists(`libs/my-lib/jest.config.js`)).toBeTruthy(); + expect(appTree.exists(`libs/my-lib/jest.config.ts`)).toBeTruthy(); expect(appTree.exists('libs/my-lib/src/index.ts')).toBeTruthy(); expect(appTree.exists('libs/my-lib/src/lib/my-lib.tsx')).toBeTruthy(); expect( @@ -247,7 +248,7 @@ describe('lib', () => { it('should generate files', async () => { await libraryGenerator(appTree, { ...defaultSchema, directory: 'myDir' }); - expect(appTree.exists(`libs/my-dir/my-lib/jest.config.js`)).toBeTruthy(); + expect(appTree.exists(`libs/my-dir/my-lib/jest.config.ts`)).toBeTruthy(); expect(appTree.exists('libs/my-dir/my-lib/src/index.ts')).toBeTruthy(); expect( appTree.exists('libs/my-dir/my-lib/src/lib/my-dir-my-lib.tsx') @@ -395,7 +396,7 @@ describe('lib', () => { }); expect(appTree.exists('libs/my-lib/tsconfig.spec.json')).toBeFalsy(); - expect(appTree.exists('libs/my-lib/jest.config.js')).toBeFalsy(); + expect(appTree.exists('libs/my-lib/jest.config.ts')).toBeFalsy(); const workspaceJson = readJson(appTree, 'workspace.json'); expect(workspaceJson.projects['my-lib'].architect.test).toBeUndefined(); expect(workspaceJson.projects['my-lib'].architect.lint) diff --git a/packages/web/src/generators/application/application.spec.ts b/packages/web/src/generators/application/application.spec.ts index 9834b4ee13810..1469652436cb5 100644 --- a/packages/web/src/generators/application/application.spec.ts +++ b/packages/web/src/generators/application/application.spec.ts @@ -279,7 +279,7 @@ describe('app', () => { standaloneConfig: false, }); - expect(tree.read('apps/my-app/jest.config.js', 'utf-8')).not.toContain( + expect(tree.read('apps/my-app/jest.config.ts', 'utf-8')).not.toContain( `'jest-preset-angular/build/AngularSnapshotSerializer.js',` ); }); @@ -374,12 +374,12 @@ describe('app', () => { unitTestRunner: 'none', standaloneConfig: false, }); - expect(tree.exists('jest.config.js')).toBeFalsy(); + expect(tree.exists('jest.config.ts')).toBeFalsy(); expect( tree.exists('apps/my-app/src/app/app.element.spec.ts') ).toBeFalsy(); expect(tree.exists('apps/my-app/tsconfig.spec.json')).toBeFalsy(); - expect(tree.exists('apps/my-app/jest.config.js')).toBeFalsy(); + expect(tree.exists('apps/my-app/jest.config.ts')).toBeFalsy(); const workspaceJson = readJson(tree, 'workspace.json'); expect(workspaceJson.projects['my-app'].architect.test).toBeUndefined(); expect(workspaceJson.projects['my-app'].architect.lint) @@ -419,11 +419,11 @@ describe('app', () => { compiler: 'babel', } as Schema); - expect(tree.read(`apps/my-app/jest.config.js`, 'utf-8')) + expect(tree.read(`apps/my-app/jest.config.ts`, 'utf-8')) .toMatchInlineSnapshot(` "module.exports = { displayName: 'my-app', - preset: '../../jest.preset.js', + preset: '../../jest.preset.ts', setupFilesAfterEnv: ['/src/test-setup.ts'], transform: { '^.+\\\\\\\\.[tj]s$': 'babel-jest' @@ -441,11 +441,11 @@ describe('app', () => { compiler: 'swc', } as Schema); - expect(tree.read(`apps/my-app/jest.config.js`, 'utf-8')) + expect(tree.read(`apps/my-app/jest.config.ts`, 'utf-8')) .toMatchInlineSnapshot(` "module.exports = { displayName: 'my-app', - preset: '../../jest.preset.js', + preset: '../../jest.preset.ts', setupFilesAfterEnv: ['/src/test-setup.ts'], transform: { '^.+\\\\\\\\.[tj]s$': '@swc/jest' diff --git a/packages/workspace/src/generators/library/library.spec.ts b/packages/workspace/src/generators/library/library.spec.ts index 5a57f09652469..cb802593217bd 100644 --- a/packages/workspace/src/generators/library/library.spec.ts +++ b/packages/workspace/src/generators/library/library.spec.ts @@ -192,12 +192,12 @@ describe('lib', () => { name: 'myLib', }); - expect(tree.exists(`libs/my-lib/jest.config.js`)).toBeTruthy(); - expect(tree.read(`libs/my-lib/jest.config.js`, 'utf-8')) + expect(tree.exists(`libs/my-lib/jest.config.ts`)).toBeTruthy(); + expect(tree.read(`libs/my-lib/jest.config.ts`, 'utf-8')) .toMatchInlineSnapshot(` "module.exports = { displayName: 'my-lib', - preset: '../../jest.preset.js', + preset: '../../jest.preset.ts', globals: { 'ts-jest': { tsconfig: '/tsconfig.spec.json', @@ -234,7 +234,7 @@ describe('lib', () => { };" `; - expect(tree.read('jest.config.js', 'utf-8')).toMatchInlineSnapshot( + expect(tree.read('jest.config.ts', 'utf-8')).toMatchInlineSnapshot( expectedRootJestConfig ); await libraryGenerator(tree, { @@ -242,7 +242,7 @@ describe('lib', () => { name: 'myLib2', }); - expect(tree.read('jest.config.js', 'utf-8')).toMatchInlineSnapshot( + expect(tree.read('jest.config.ts', 'utf-8')).toMatchInlineSnapshot( expectedRootJestConfig ); }); @@ -287,7 +287,7 @@ describe('lib', () => { name: 'myLib', directory: 'myDir', }); - expect(tree.exists(`libs/my-dir/my-lib/jest.config.js`)).toBeTruthy(); + expect(tree.exists(`libs/my-dir/my-lib/jest.config.ts`)).toBeTruthy(); expect(tree.exists('libs/my-dir/my-lib/src/index.ts')).toBeTruthy(); expect( tree.exists('libs/my-dir/my-lib/src/lib/my-dir-my-lib.ts') @@ -602,7 +602,7 @@ describe('lib', () => { }); expect(tree.exists('libs/my-lib/tsconfig.spec.json')).toBeFalsy(); - expect(tree.exists('libs/my-lib/jest.config.js')).toBeFalsy(); + expect(tree.exists('libs/my-lib/jest.config.ts')).toBeFalsy(); expect(tree.exists('libs/my-lib/src/lib/my-lib.spec.ts')).toBeFalsy(); const workspaceJson = readJson(tree, 'workspace.json'); @@ -704,7 +704,7 @@ describe('lib', () => { name: 'myLib', js: true, }); - expect(tree.exists(`libs/my-lib/jest.config.js`)).toBeTruthy(); + expect(tree.exists(`libs/my-lib/jest.config.ts`)).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.js`)).toBeTruthy(); + expect(tree.exists(`libs/my-dir/my-lib/jest.config.ts`)).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') @@ -827,11 +827,11 @@ describe('lib', () => { babelJest: true, } as Schema); - expect(tree.read(`libs/my-lib/jest.config.js`, 'utf-8')) + expect(tree.read(`libs/my-lib/jest.config.ts`, 'utf-8')) .toMatchInlineSnapshot(` "module.exports = { displayName: 'my-lib', - preset: '../../jest.preset.js', + preset: '../../jest.preset.ts', transform: { '^.+\\\\\\\\.[tj]sx?$': 'babel-jest' }, diff --git a/packages/workspace/src/generators/move/lib/update-jest-config.spec.ts b/packages/workspace/src/generators/move/lib/update-jest-config.spec.ts index fe26d3da45bd9..f3964d1f3e4da 100644 --- a/packages/workspace/src/generators/move/lib/update-jest-config.spec.ts +++ b/packages/workspace/src/generators/move/lib/update-jest-config.spec.ts @@ -32,15 +32,15 @@ describe('updateJestConfig', () => { it('should update the name and coverage directory', async () => { const jestConfig = `module.exports = { name: 'my-source', - preset: '../../jest.config.js', + preset: '../../jest.config.ts', coverageDirectory: '../../coverage/libs/my-source', snapshotSerializers: [ 'jest-preset-angular/AngularSnapshotSerializer.js', 'jest-preset-angular/HTMLCommentSerializer.js' ] };`; - const jestConfigPath = '/libs/my-destination/jest.config.js'; - const rootJestConfigPath = '/jest.config.js'; + const jestConfigPath = '/libs/my-destination/jest.config.ts'; + const rootJestConfigPath = '/jest.config.ts'; await libraryGenerator(tree, { name: 'my-source', standaloneConfig: false, @@ -70,15 +70,15 @@ describe('updateJestConfig', () => { it('should update jest configs properly even if project is in many layers of subfolders', async () => { const jestConfig = `module.exports = { name: 'some-test-dir-my-source', - preset: '../../jest.config.js', + preset: '../../jest.config.ts', coverageDirectory: '../../coverage/libs/some/test/dir/my-source', snapshotSerializers: [ 'jest-preset-angular/AngularSnapshotSerializer.js', 'jest-preset-angular/HTMLCommentSerializer.js' ] };`; - const jestConfigPath = '/libs/other/test/dir/my-destination/jest.config.js'; - const rootJestConfigPath = '/jest.config.js'; + const jestConfigPath = '/libs/other/test/dir/my-destination/jest.config.ts'; + const rootJestConfigPath = '/jest.config.ts'; await libraryGenerator(tree, { name: 'some/test/dir/my-source', standaloneConfig: false, @@ -108,7 +108,7 @@ describe('updateJestConfig', () => { }); it('updates the root config if not using `getJestProjects()`', async () => { - const rootJestConfigPath = '/jest.config.js'; + const rootJestConfigPath = '/jest.config.ts'; await libraryGenerator(tree, { name: 'some/test/dir/my-source', standaloneConfig: false, @@ -145,7 +145,7 @@ describe('updateJestConfig', () => { }); it('updates the root config if `getJestProjects()` is used but old path exists', async () => { - const rootJestConfigPath = '/jest.config.js'; + const rootJestConfigPath = '/jest.config.ts'; await libraryGenerator(tree, { name: 'some/test/dir/my-source', standaloneConfig: false, @@ -185,7 +185,7 @@ module.exports = { }); it('updates the root config if `getJestProjects()` is used with other projects in the array', async () => { - const rootJestConfigPath = '/jest.config.js'; + const rootJestConfigPath = '/jest.config.ts'; await libraryGenerator(tree, { name: 'some/test/dir/my-source', standaloneConfig: false, diff --git a/packages/workspace/src/generators/move/lib/update-jest-config.ts b/packages/workspace/src/generators/move/lib/update-jest-config.ts index ae2360fcf4279..256134ac7e8b3 100644 --- a/packages/workspace/src/generators/move/lib/update-jest-config.ts +++ b/packages/workspace/src/generators/move/lib/update-jest-config.ts @@ -31,8 +31,8 @@ export function updateJestConfig( tree.write(jestConfigPath, newContent); } - // update root jest.config.js - const rootJestConfigPath = '/jest.config.js'; + // update root jest.config.ts + const rootJestConfigPath = '/jest.config.ts'; if (!tree.exists(rootJestConfigPath)) { return; diff --git a/packages/workspace/src/generators/move/lib/update-project-root-files.ts b/packages/workspace/src/generators/move/lib/update-project-root-files.ts index b345e488f4f59..fd83586a68c37 100644 --- a/packages/workspace/src/generators/move/lib/update-project-root-files.ts +++ b/packages/workspace/src/generators/move/lib/update-project-root-files.ts @@ -35,10 +35,8 @@ export function updateProjectRootFiles( const dots = /\./g; const regex = new RegExp(oldRelativeRoot.replace(dots, '\\.'), 'g'); - for (const file of tree.children(schema.relativeToRootDestination)) { const ext = extname(file); - console.log({file}) if (!allowedExt.includes(ext)) { continue; } diff --git a/packages/workspace/src/generators/move/move.spec.ts b/packages/workspace/src/generators/move/move.spec.ts new file mode 100644 index 0000000000000..d13b192dbb3eb --- /dev/null +++ b/packages/workspace/src/generators/move/move.spec.ts @@ -0,0 +1,47 @@ +import { Tree } from '@nrwl/devkit'; +import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; +import { moveGenerator } from './move'; +import { libraryGenerator } from '../library/library'; + +describe('move', () => { + let tree: Tree; + beforeEach(() => { + tree = createTreeWithEmptyWorkspace(); + }); + + it('should update jest config when moving down directories', async () => { + await libraryGenerator(tree, { name: 'my-lib' }); + + await moveGenerator(tree, { + projectName: 'my-lib', + importPath: '@proj/shared-mylib', + updateImportPath: true, + destination: 'shared/my-lib-new', + }); + const jestConfigPath = 'libs/shared/my-lib-new/jest.config.ts'; + const afterJestConfig = tree.read(jestConfigPath, 'utf-8'); + expect(tree.exists(jestConfigPath)).toBeTruthy(); + expect(afterJestConfig).toContain("preset: '../../../jest.preset.ts'"); + expect(afterJestConfig).toContain( + "coverageDirectory: '../../../coverage/libs/shared/my-lib-new'" + ); + }); + + it('should update jest config when moving up directories', async () => { + await libraryGenerator(tree, { name: 'shared/my-lib' }); + + await moveGenerator(tree, { + projectName: 'shared-my-lib', + importPath: '@proj/mylib', + updateImportPath: true, + destination: 'my-lib-new', + }); + const jestConfigPath = 'libs/my-lib-new/jest.config.ts'; + const afterJestConfig = tree.read(jestConfigPath, 'utf-8'); + expect(tree.exists(jestConfigPath)).toBeTruthy(); + expect(afterJestConfig).toContain("preset: '../../jest.preset.ts'"); + expect(afterJestConfig).toContain( + "coverageDirectory: '../../coverage/libs/my-lib-new'" + ); + }); +}); diff --git a/packages/workspace/src/generators/preset/__snapshots__/preset.spec.ts.snap b/packages/workspace/src/generators/preset/__snapshots__/preset.spec.ts.snap index a48a3b470d898..7b6bcd58c8776 100644 --- a/packages/workspace/src/generators/preset/__snapshots__/preset.spec.ts.snap +++ b/packages/workspace/src/generators/preset/__snapshots__/preset.spec.ts.snap @@ -9,7 +9,7 @@ Array [ "tsconfig.editor.json", "tsconfig.json", ".eslintrc.json", - "jest.config.js", + "jest.config.ts", ] `; diff --git a/packages/workspace/src/generators/remove/lib/test-files/jest.config.js b/packages/workspace/src/generators/remove/lib/test-files/jest.config.ts similarity index 100% rename from packages/workspace/src/generators/remove/lib/test-files/jest.config.js rename to packages/workspace/src/generators/remove/lib/test-files/jest.config.ts diff --git a/packages/workspace/src/generators/remove/lib/update-jest-config.spec.ts b/packages/workspace/src/generators/remove/lib/update-jest-config.spec.ts index 18484127b03d0..6e7fb02964129 100644 --- a/packages/workspace/src/generators/remove/lib/update-jest-config.spec.ts +++ b/packages/workspace/src/generators/remove/lib/update-jest-config.spec.ts @@ -31,19 +31,19 @@ describe('updateRootJestConfig', () => { }); tree.write( - 'jest.config.js', - readFileSync(join(__dirname, './test-files/jest.config.js'), 'utf-8') + 'jest.config.ts', + readFileSync(join(__dirname, './test-files/jest.config.ts'), 'utf-8') ); }); it('should delete lib project ref from root jest config', async () => { - const jestConfig = tree.read('jest.config.js', 'utf-8'); + const jestConfig = tree.read('jest.config.ts', 'utf-8'); expect(jestConfig).toMatchSnapshot(); updateJestConfig(tree, schema, readProjectConfiguration(tree, 'my-lib')); - const updatedJestConfig = tree.read('jest.config.js', 'utf-8'); + const updatedJestConfig = tree.read('jest.config.ts', 'utf-8'); expect(updatedJestConfig).toMatchSnapshot(); @@ -53,23 +53,23 @@ describe('updateRootJestConfig', () => { readProjectConfiguration(tree, 'my-other-lib') ); - const updatedJestConfig2 = tree.read('jest.config.js', 'utf-8'); + const updatedJestConfig2 = tree.read('jest.config.ts', 'utf-8'); expect(updatedJestConfig2).toMatchSnapshot(); }); it('should not delete lib project ref from root jest config if there is no project jest config', () => { - tree.delete('libs/my-lib/jest.config.js'); + tree.delete('libs/my-lib/jest.config.ts'); - const originalRootJestConfig = tree.read('jest.config.js', 'utf-8'); + const originalRootJestConfig = tree.read('jest.config.ts', 'utf-8'); tree.write( - 'jest.config.js', + 'jest.config.ts', originalRootJestConfig.replace(`'/libs/my-lib',`, '') ); updateJestConfig(tree, schema, readProjectConfiguration(tree, 'my-lib')); - const rootJestConfig = tree.read('jest.config.js', 'utf-8'); + const rootJestConfig = tree.read('jest.config.ts', 'utf-8'); expect(rootJestConfig).toMatchSnapshot(); });