fix(angular): remove unnecessary addition of module file extensions (#7725)
This commit is contained in:
parent
24bbb848eb
commit
18f2e703dd
@ -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'], };"
|
||||
};"
|
||||
`;
|
||||
|
||||
@ -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: ['<rootDir>/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: '<rootDir>/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: ['<rootDir>/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: '<rootDir>/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: ['<rootDir>/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: '<rootDir>/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: ['<rootDir>/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: '<rootDir>/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: ['<rootDir>/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: '<rootDir>/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: ['<rootDir>/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: '<rootDir>/tsconfig.spec.json',
|
||||
},
|
||||
},
|
||||
displayName: 'common-platform',
|
||||
|
||||
moduleFileExtensions: ['mjs', 'ts', 'js', 'html'], };
|
||||
"
|
||||
`);
|
||||
});
|
||||
});
|
||||
|
||||
@ -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)}`;
|
||||
}
|
||||
|
||||
@ -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'
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user