Skip to content

Commit

Permalink
chore(testing): pr feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
barbados-clemens committed Feb 23, 2023
1 parent 5603136 commit d6bab70
Show file tree
Hide file tree
Showing 10 changed files with 321 additions and 59 deletions.
27 changes: 5 additions & 22 deletions e2e/node/src/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,30 +422,13 @@ describe('nest libraries', function () {
const nestlib = uniq('nestlib');
runCLI(`generate @nrwl/nest:lib ${nestlib}`);

const jestConfigContent = readFile(`libs/${nestlib}/jest.config.ts`);

expect(stripIndents`${jestConfigContent}`).toEqual(
stripIndents`/* eslint-disable */
export default {
displayName: '${nestlib}',
preset: '../../jest.preset.js',
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
},
},
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': 'ts-jest',
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/libs/${nestlib}',
};
`
);

const lintResults = runCLI(`lint ${nestlib}`);
expect(lintResults).toContain('All files pass linting.');

const testResults = runCLI(`test ${nestlib}`);
expect(testResults).toContain(
`Successfully ran target test for project ${nestlib}`
);
}, 60000);

it('should be able to generate a nest library w/ service', async () => {
Expand Down
10 changes: 5 additions & 5 deletions packages/angular/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -1908,15 +1908,15 @@
"15.8.0-jest": {
"version": "15.8.0-beta.0",
"requires": {
"@angular-devkit/build-angular": ">=12.2.18 <16.0.0",
"@angular/compiler-cli": ">=12.2.16 <16.0.0",
"@angular/core": ">=12.2.16 <16.0.0",
"@angular/platform-browser-dynamic": ">=12.2.16 <16.0.0",
"@angular-devkit/build-angular": ">=13.0.0 <16.0.0",
"@angular/compiler-cli": ">=13.0.0 <16.0.0",
"@angular/core": ">=13.0.0 <16.0.0",
"@angular/platform-browser-dynamic": ">=13.0.0 <16.0.0",
"jest": "^29.0.0"
},
"packages": {
"jest-preset-angular": {
"version": "13.0.0-next.1",
"version": "13.0.0",
"alwaysAddToPackageJson": false
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/angular/src/utils/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const postcssUrlVersion = '~10.1.3';
export const autoprefixerVersion = '^10.4.0';
export const tsNodeVersion = '10.9.1';

export const jestPresetAngularVersion = '~13.0.0-next.1';
export const jestPresetAngularVersion = '~13.0.0';
export const protractorVersion = '~7.0.0';
export const karmaVersion = '~6.4.0';
export const karmaChromeLauncherVersion = '~3.1.0';
Expand Down
1 change: 0 additions & 1 deletion packages/jest/jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const j = require('./tsconfig.json');
/* eslint-disable */
export default {
transform: {
Expand Down
12 changes: 0 additions & 12 deletions packages/jest/src/generators/jest-project/jest-project.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,18 +325,6 @@ describe('jestProject', () => {
});

describe('--babelJest', () => {
// no longer true for jest v29
it.skip('should have globals.ts-jest configured when babelJest is false', async () => {
await jestProjectGenerator(tree, {
...defaultOptions,
project: 'lib1',
babelJest: false,
} as JestProjectSchema);
const jestConfig = tree.read('libs/lib1/jest.config.ts', 'utf-8');

expect(jestConfig).toMatchSnapshot();
});

it('should generate proper jest.transform when babelJest is true', async () => {
await jestProjectGenerator(tree, {
...defaultOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,73 @@ snapshotFormat: { escapeString: true, printBasicPrototype: true }
"
`;

exports[`Jest Migration - jest 29 update configs should update globalThis.ngJest.teardown to testEnvironmentOptions 1`] = `
"globalThis.ngJest = {
}
export default {
globals: { },
transform: {
'^.+.(ts|mjs|js|html)$': ['jest-preset-angular', {
tsconfig: '<rootDir>/tsconfig.spec.json',
stringifyContentPathRegex: '\\\\.(html|svg)$',
}],
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'],
displayName: 'jest',
testEnvironment: 'node',
preset: '../../jest.preset.js',
/* TODO: Update to latest Jest snapshotFormat
* By default Nx has kept the older style of Jest Snapshot formats
* to prevent breaking of any existing tests with snapshots.
* It's recommend you update to the latest format.
* You can do this by removing snapshotFormat property
* and running tests with --update-snapshot flag.
* Example: \\"nx test jest-preset-angular --update-snapshot\\"
* More info: https://jestjs.io/docs/upgrading-to-jest29#snapshot-format
*/
snapshotFormat: { escapeString: true, printBasicPrototype: true },
testEnvironmentOptions: { teardown: true },
};"
`;

exports[`Jest Migration - jest 29 update configs should update globalThis.ngJest.teardown to testEnvironmentOptions 2`] = `
"
globalThis.ngJest = {
ngcc: true,
}
module.exports = {
globals: { },
transform: {
'^.+.(ts|mjs|js|html)$': ['jest-preset-angular', {
tsconfig: '<rootDir>/tsconfig.spec.json',
stringifyContentPathRegex: '\\\\.(html|svg)$',
}],
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'],
testEnvironmentOptions: {
blah: 123,
teardown: false
},
displayName: 'jest',
testEnvironment: 'node',
preset: '../../jest.preset.js',
/* TODO: Update to latest Jest snapshotFormat
* By default Nx has kept the older style of Jest Snapshot formats
* to prevent breaking of any existing tests with snapshots.
* It's recommend you update to the latest format.
* You can do this by removing snapshotFormat property
* and running tests with --update-snapshot flag.
* Example: \\"nx test jest-preset-angular --update-snapshot\\"
* More info: https://jestjs.io/docs/upgrading-to-jest29#snapshot-format
*/
snapshotFormat: { escapeString: true, printBasicPrototype: true }
};"
`;

exports[`Jest Migration - jest 29 update configs should update jest.config.ts 1`] = `
"/* eslint-disable */
export default {
Expand Down Expand Up @@ -192,6 +259,58 @@ abc: [1234, true, {abc: 'yes'}] },
};"
`;

exports[`Jest Migration - jest 29 update configs should work with jest-preset-angular 1`] = `
"export default {
globals: { },
transform: {
'^.+.(ts|mjs|js|html)$': ['jest-preset-angular', {
tsconfig: '<rootDir>/tsconfig.spec.json',
stringifyContentPathRegex: '\\\\.(html|svg)$',
}],
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'],
displayName: 'jest',
testEnvironment: 'node',
preset: '../../jest.preset.js',
/* TODO: Update to latest Jest snapshotFormat
* By default Nx has kept the older style of Jest Snapshot formats
* to prevent breaking of any existing tests with snapshots.
* It's recommend you update to the latest format.
* You can do this by removing snapshotFormat property
* and running tests with --update-snapshot flag.
* Example: \\"nx test jest-preset-angular --update-snapshot\\"
* More info: https://jestjs.io/docs/upgrading-to-jest29#snapshot-format
*/
snapshotFormat: { escapeString: true, printBasicPrototype: true }
};"
`;

exports[`Jest Migration - jest 29 update configs should work with jest-preset-angular 2`] = `
"module.exports = {
globals: { },
transform: {
'^.+.(ts|mjs|js|html)$': ['jest-preset-angular', {
tsconfig: '<rootDir>/tsconfig.spec.json',
stringifyContentPathRegex: '\\\\.(html|svg)$',
}],
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'],
displayName: 'jest',
testEnvironment: 'node',
preset: '../../jest.preset.js',
/* TODO: Update to latest Jest snapshotFormat
* By default Nx has kept the older style of Jest Snapshot formats
* to prevent breaking of any existing tests with snapshots.
* It's recommend you update to the latest format.
* You can do this by removing snapshotFormat property
* and running tests with --update-snapshot flag.
* Example: \\"nx test jest-preset-angular --update-snapshot\\"
* More info: https://jestjs.io/docs/upgrading-to-jest29#snapshot-format
*/
snapshotFormat: { escapeString: true, printBasicPrototype: true }
};"
`;

exports[`Jest Migration - jest 29 update configs should work with multiple projects + configs 1`] = `
"/* eslint-disable */
export default {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ describe('Jest Migration - jest 29 update configs', () => {
beforeEach(() => {
tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' });
});
it.only('should update jest.config.ts', async () => {
afterAll(() => {
jest.resetAllMocks();
});
it('should update jest.config.ts', async () => {
await setup(tree, 'my-lib');

await updateConfigsJest29(tree);
Expand All @@ -34,7 +37,7 @@ describe('Jest Migration - jest 29 update configs', () => {

it('should work with multiple projects + configs', async () => {
await setup(tree, 'my-lib');
await setup(tree, 'another-lib');
await setup(tree, 'another-lib', projectGraph);
await updateConfigsJest29(tree);

const actualJestConfigTs1 = tree.read(
Expand All @@ -60,6 +63,121 @@ describe('Jest Migration - jest 29 update configs', () => {
expect(actualJestConfigJs2).toMatchSnapshot();
});

it('should update globalThis.ngJest.teardown to testEnvironmentOptions ', async () => {
await setup(tree, 'jest-preset-angular');
tree.write(
`libs/jest-preset-angular/jest.config.ts`,
`globalThis.ngJest = {
teardown: true
}
export default {
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
stringifyContentPathRegex: '\\.(html|svg)$',
}
},
transform: {
'^.+.(ts|mjs|js|html)$': 'jest-preset-angular',
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'],
displayName: 'jest',
testEnvironment: 'node',
preset: '../../jest.preset.js',
};`
);
tree.write(
`libs/jest-preset-angular/jest.config.js`,
`
globalThis.ngJest = {
ngcc: true,
teardown: false
}
module.exports = {
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
stringifyContentPathRegex: '\\.(html|svg)$',
}
},
transform: {
'^.+.(ts|mjs|js|html)$': 'jest-preset-angular',
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'],
testEnvironmentOptions: {
blah: 123,
},
displayName: 'jest',
testEnvironment: 'node',
preset: '../../jest.preset.js',
};`
);
await updateConfigsJest29(tree);
const jpaJestConfigTs = tree.read(
`libs/jest-preset-angular/jest.config.ts`,
'utf-8'
);
expect(jpaJestConfigTs).toMatchSnapshot();
const jpaJestConfigJs = tree.read(
`libs/jest-preset-angular/jest.config.js`,
'utf-8'
);
expect(jpaJestConfigJs).toMatchSnapshot();
});

it('should work with jest-preset-angular', async () => {
await setup(tree, 'jest-preset-angular');
tree.write(
`libs/jest-preset-angular/jest.config.ts`,
`export default {
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
stringifyContentPathRegex: '\\.(html|svg)$',
}
},
transform: {
'^.+.(ts|mjs|js|html)$': 'jest-preset-angular',
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html']
displayName: 'jest',
testEnvironment: 'node',
preset: '../../jest.preset.js',
};`
);
tree.write(
`libs/jest-preset-angular/jest.config.js`,
`module.exports = {
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
stringifyContentPathRegex: '\\.(html|svg)$',
}
},
transform: {
'^.+.(ts|mjs|js|html)$': 'jest-preset-angular',
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html']
displayName: 'jest',
testEnvironment: 'node',
preset: '../../jest.preset.js',
};`
);
await updateConfigsJest29(tree);
const jpaJestConfigTs = tree.read(
`libs/jest-preset-angular/jest.config.ts`,
'utf-8'
);
expect(jpaJestConfigTs).toMatchSnapshot();
const jpaJestConfigJs = tree.read(
`libs/jest-preset-angular/jest.config.js`,
'utf-8'
);
expect(jpaJestConfigJs).toMatchSnapshot();
});

it('should work if not using ts-jest transformer', async () => {
await setup(tree, 'no-ts-jest');
tree.write(
Expand Down Expand Up @@ -186,7 +304,7 @@ describe('Jest Migration - jest 29 update configs', () => {
});
});

async function setup(tree: Tree, name: string) {
async function setup(tree: Tree, name: string, existingGraph?: ProjectGraph) {
await libraryGenerator(tree, {
name,
});
Expand Down Expand Up @@ -250,8 +368,11 @@ preset: '../../jest.preset.js'
);

projectGraph = {
dependencies: {},
dependencies: {
...existingGraph?.dependencies,
},
nodes: {
...existingGraph?.nodes,
[name]: {
name,
type: 'lib',
Expand Down
Loading

0 comments on commit d6bab70

Please sign in to comment.