From 18f2e703dd33fa5cd4ecd69e8c103e77a1fa4bd9 Mon Sep 17 00:00:00 2001 From: Jason Jean Date: Fri, 12 Nov 2021 16:36:59 -0500 Subject: [PATCH] fix(angular): remove unnecessary addition of module file extensions (#7725) --- .../update-angular-jest-config.spec.ts.snap | 3 +- .../update-angular-jest-config.spec.ts | 164 ------------------ .../update-angular-jest-config.ts | 57 +----- .../files-angular/jest.config.js__tmpl__ | 7 +- 4 files changed, 5 insertions(+), 226 deletions(-) diff --git a/packages/angular/src/migrations/update-13-2-0/__snapshots__/update-angular-jest-config.spec.ts.snap b/packages/angular/src/migrations/update-13-2-0/__snapshots__/update-angular-jest-config.spec.ts.snap index 707d6cc0a8..a7e83cd52e 100644 --- a/packages/angular/src/migrations/update-13-2-0/__snapshots__/update-angular-jest-config.spec.ts.snap +++ b/packages/angular/src/migrations/update-13-2-0/__snapshots__/update-angular-jest-config.spec.ts.snap @@ -21,6 +21,5 @@ transformIgnorePatterns: ['node_modules/(?!.*\\\\.mjs$)'], 'jest-preset-angular/build/serializers/ng-snapshot', 'jest-preset-angular/build/serializers/html-comment', ], - -moduleFileExtensions: ['mjs', 'ts', 'js', 'html'], };" + };" `; diff --git a/packages/angular/src/migrations/update-13-2-0/update-angular-jest-config.spec.ts b/packages/angular/src/migrations/update-13-2-0/update-angular-jest-config.spec.ts index 63c412a338..e8bfa3dda2 100644 --- a/packages/angular/src/migrations/update-13-2-0/update-angular-jest-config.spec.ts +++ b/packages/angular/src/migrations/update-13-2-0/update-angular-jest-config.spec.ts @@ -100,7 +100,6 @@ snapshotSerializers: [ 'jest-preset-angular/build/serializers/ng-snapshot', 'jest-preset-angular/build/serializers/html-comment', ], - moduleFileExtensions: ['mjs', 'ts', 'js', 'html'], };" `); }); @@ -155,7 +154,6 @@ snapshotSerializers: [ 'jest-preset-angular/build/serializers/ng-snapshot', 'jest-preset-angular/build/serializers/html-comment', ], - moduleFileExtensions: ['mjs', 'ts', 'js', 'html'], };" `); }); @@ -200,7 +198,6 @@ transform: { '^.+\\\\.(ts|mjs|js|html)$': 'jest-preset-angular', }, transformIgnorePatterns: ['node_modules/(?!.*\\\\.mjs$)'], - moduleFileExtensions: ['mjs', 'ts', 'js', 'html'], };" `); }); @@ -249,165 +246,4 @@ transform: { " `); }); - - it('should add the mjs extension to a config with existing moduleFileExtensions property', () => { - // ARRANGE - const jestConfig = `module.exports = { - preset: '../../jest.preset.js', - coverageDirectory: '../../coverage/libs/common-platform', - moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'], - setupFilesAfterEnv: ['/src/test-setup.ts'], - transform: { - '^.+\\.(ts|js|html)$': 'jest-preset-angular', - }, - globals: { - 'ts-jest': { - stringifyContentPathRegex: '\\\\.(html|svg)$', - astTransformers: [ - 'jest-preset-angular/build/InlineFilesTransformer', - 'jest-preset-angular/build/StripStylesTransformer', - ], - tsconfig: '/tsconfig.spec.json', - }, - }, - displayName: 'common-platform', - }; - `; - - // ACT - const updatedFile = replaceTransformAndAddIgnorePattern(jestConfig); - - // ASSERT - expect(updatedFile).toMatchInlineSnapshot(` - "module.exports = { - preset: '../../jest.preset.js', - coverageDirectory: '../../coverage/libs/common-platform', - moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html', 'mjs'], - setupFilesAfterEnv: ['/src/test-setup.ts'], - transform: { - '^.+\\\\.(ts|mjs|js|html)$': 'jest-preset-angular', - }, - transformIgnorePatterns: ['node_modules/(?!.*\\\\.mjs$)'], - globals: { - 'ts-jest': { - stringifyContentPathRegex: '\\\\\\\\.(html|svg)$', - astTransformers: [ - 'jest-preset-angular/build/InlineFilesTransformer', - 'jest-preset-angular/build/StripStylesTransformer', - ], - tsconfig: '/tsconfig.spec.json', - }, - }, - displayName: 'common-platform', - }; - " - `); - }); - - it('should not add the mjs extension to a config with existing moduleFileExtensions property with mjs extension', () => { - // ARRANGE - const jestConfig = `module.exports = { - preset: '../../jest.preset.js', - coverageDirectory: '../../coverage/libs/common-platform', - moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html', 'mjs'], - setupFilesAfterEnv: ['/src/test-setup.ts'], - transform: { - '^.+\\.(ts|js|html)$': 'jest-preset-angular', - }, - globals: { - 'ts-jest': { - stringifyContentPathRegex: '\\\\.(html|svg)$', - astTransformers: [ - 'jest-preset-angular/build/InlineFilesTransformer', - 'jest-preset-angular/build/StripStylesTransformer', - ], - tsconfig: '/tsconfig.spec.json', - }, - }, - displayName: 'common-platform', - }; - `; - - // ACT - const updatedFile = replaceTransformAndAddIgnorePattern(jestConfig); - - // ASSERT - expect(updatedFile).toMatchInlineSnapshot(` - "module.exports = { - preset: '../../jest.preset.js', - coverageDirectory: '../../coverage/libs/common-platform', - moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html', 'mjs'], - setupFilesAfterEnv: ['/src/test-setup.ts'], - transform: { - '^.+\\\\.(ts|mjs|js|html)$': 'jest-preset-angular', - }, - transformIgnorePatterns: ['node_modules/(?!.*\\\\.mjs$)'], - globals: { - 'ts-jest': { - stringifyContentPathRegex: '\\\\\\\\.(html|svg)$', - astTransformers: [ - 'jest-preset-angular/build/InlineFilesTransformer', - 'jest-preset-angular/build/StripStylesTransformer', - ], - tsconfig: '/tsconfig.spec.json', - }, - }, - displayName: 'common-platform', - }; - " - `); - }); - - it('should add the moduleFileExtensions property with mjs extension', () => { - // ARRANGE - const jestConfig = `module.exports = { - preset: '../../jest.preset.js', - coverageDirectory: '../../coverage/libs/common-platform', - setupFilesAfterEnv: ['/src/test-setup.ts'], - transform: { - '^.+\\.(ts|js|html)$': 'jest-preset-angular', - }, - globals: { - 'ts-jest': { - stringifyContentPathRegex: '\\\\.(html|svg)$', - astTransformers: [ - 'jest-preset-angular/build/InlineFilesTransformer', - 'jest-preset-angular/build/StripStylesTransformer', - ], - tsconfig: '/tsconfig.spec.json', - }, - }, - displayName: 'common-platform', - }; - `; - - // ACT - const updatedFile = replaceTransformAndAddIgnorePattern(jestConfig); - - // ASSERT - expect(updatedFile).toMatchInlineSnapshot(` - "module.exports = { - preset: '../../jest.preset.js', - coverageDirectory: '../../coverage/libs/common-platform', - setupFilesAfterEnv: ['/src/test-setup.ts'], - transform: { - '^.+\\\\.(ts|mjs|js|html)$': 'jest-preset-angular', - }, - transformIgnorePatterns: ['node_modules/(?!.*\\\\.mjs$)'], - globals: { - 'ts-jest': { - stringifyContentPathRegex: '\\\\\\\\.(html|svg)$', - astTransformers: [ - 'jest-preset-angular/build/InlineFilesTransformer', - 'jest-preset-angular/build/StripStylesTransformer', - ], - tsconfig: '/tsconfig.spec.json', - }, - }, - displayName: 'common-platform', - - moduleFileExtensions: ['mjs', 'ts', 'js', 'html'], }; - " - `); - }); }); diff --git a/packages/angular/src/migrations/update-13-2-0/update-angular-jest-config.ts b/packages/angular/src/migrations/update-13-2-0/update-angular-jest-config.ts index 0fa8b71b18..4462ba7c9b 100644 --- a/packages/angular/src/migrations/update-13-2-0/update-angular-jest-config.ts +++ b/packages/angular/src/migrations/update-13-2-0/update-angular-jest-config.ts @@ -1,9 +1,5 @@ import type { Tree } from '@nrwl/devkit'; -import type { - Node, - ObjectLiteralExpression, - PropertyAssignment, -} from 'typescript'; +import type { Node, PropertyAssignment } from 'typescript'; import { formatFiles, getProjects } from '@nrwl/devkit'; import { tsquery } from '@phenomnomnominal/tsquery'; @@ -28,6 +24,8 @@ export default async function (tree: Tree) { angularProjects[projectName] = [jestConfigPath, jestConfig]; } } + + await formatFiles(tree); } for (const [_, [jestConfigPath, jestFileContents]] of Object.entries( @@ -50,7 +48,6 @@ export function replaceTransformAndAddIgnorePattern(fileContents: string) { return updatedFileContents; } - updatedFileContents = updateModuleFileExtenstions(updatedFileContents); return updatedFileContents; } @@ -111,51 +108,3 @@ function updateTransformIgnorePattern(fileContents: string) { transformEndIndex )}${TRANSFORM_IGNORE_PATTERN_STRING}${fileContents.slice(transformEndIndex)}`; } - -function updateModuleFileExtenstions(fileContents: string) { - const MODULE_FILE_EXTENSIONS_AST_QUERY = - 'Identifier[name=moduleFileExtensions] ~ ArrayLiteralExpression > StringLiteral'; - const MODULE_FILE_EXTENSIONS_STRING = 'mjs'; - const MODULE_FILE_EXTENSIONS_FULL_STRING = `moduleFileExtensions: ['mjs', 'ts', 'js', 'html'],`; - - const ast = tsquery.ast(fileContents); - - const moduleFileExtensionsNodes = tsquery( - ast, - MODULE_FILE_EXTENSIONS_AST_QUERY, - { - visitAllChildren: true, - } - ); - - if (!moduleFileExtensionsNodes || moduleFileExtensionsNodes.length === 0) { - // add the full property - const insertPosition = fileContents.lastIndexOf('}') - 1; - - return `${fileContents.slice( - 0, - insertPosition - )}\n${MODULE_FILE_EXTENSIONS_FULL_STRING}${fileContents.slice( - insertPosition - )}`; - } - - // check if the extension already exists - const hasExtensionAlready = Boolean( - moduleFileExtensionsNodes.find((node) => - node.getText().includes(MODULE_FILE_EXTENSIONS_STRING) - ) - ); - if (hasExtensionAlready) { - return fileContents; - } - - // add the extension - const insertPosition = - moduleFileExtensionsNodes[moduleFileExtensionsNodes.length - 1].getEnd(); - - return `${fileContents.slice( - 0, - insertPosition - )}, '${MODULE_FILE_EXTENSIONS_STRING}'${fileContents.slice(insertPosition)}`; -} 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.js__tmpl__ index 73b57a1c57..24af80da73 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.js__tmpl__ @@ -8,12 +8,7 @@ module.exports = { stringifyContentPathRegex: '\\.(html|svg)$', } },<% if(testEnvironment) { %> - testEnvironment: '<%= testEnvironment %>',<% } %><% if(skipSerializers){ %> - transform: { - <% if (supportTsx){ %>'^.+\\.[tj]sx?$'<% } else { %>'^.+\\.[tj]s$'<% } %>: <% if (transformer == 'babel-jest') { %>'babel-jest'<% } else { %> 'ts-jest' <% } %> - },<% if (supportTsx) { %> - moduleFileExtensions: ['mjs', 'ts', 'tsx', 'js', 'jsx'],<% } else { %> - moduleFileExtensions: ['mjs', 'ts', 'js', 'html'],<% } %><% } %> + testEnvironment: '<%= testEnvironment %>',<% } %> coverageDirectory: '<%= offsetFromRoot %>coverage/<%= projectRoot %>', transform: { '^.+\\.(ts|mjs|js|html)$': 'jest-preset-angular'