Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(testing): update v14 migration and migrate jest.config.ts to use export default #10035

Merged
merged 10 commits into from
May 11, 2022
Merged
12 changes: 4 additions & 8 deletions e2e/jest/src/jest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,17 @@ import {
runCLIAsync,
uniq,
updateFile,
expectJestTestsToPass,
} from '@nrwl/e2e/utils';

describe('Jest', () => {
beforeAll(() => {
newProject({ name: uniq('proj') });
newProject({ name: uniq('proj-jest') });
});

it('should be able test projects using jest', async () => {
const mylib = uniq('mylib');
runCLI(`generate @nrwl/workspace:lib ${mylib} --unit-test-runner jest`);

const libResult = await runCLIAsync(`test ${mylib}`);
expect(libResult.combinedOutput).toContain(
'Test Suites: 1 passed, 1 total'
);
await expectJestTestsToPass('@nrwl/workspace:lib');
await expectJestTestsToPass('@nrwl/js:lib');
}, 500000);

it('should merge with jest config globals', async () => {
Expand Down
5 changes: 5 additions & 0 deletions e2e/js/src/js.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
checkFilesExist,
expectJestTestsToPass,
checkFilesDoNotExist,
newProject,
readFile,
Expand Down Expand Up @@ -242,4 +243,8 @@ describe('js e2e', () => {

checkFilesDoNotExist(`libs/${lib}/.babelrc`);
});

it('should run default jest tests', async () => {
await expectJestTestsToPass('@nrwl/js:lib');
});
});
4 changes: 4 additions & 0 deletions e2e/next/src/next.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
checkFilesExist,
cleanupProject,
createFile,
expectJestTestsToPass,
isNotWindows,
killPorts,
newProject,
Expand Down Expand Up @@ -409,6 +410,9 @@ describe('Next.js Applications', () => {
checkExport: false,
});
}, 300000);
it('should run default jest tests', async () => {
await expectJestTestsToPass('@nrwl/next:app');
});
});

function getData(port: number, path = ''): Promise<any> {
Expand Down
10 changes: 8 additions & 2 deletions e2e/node/src/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { stripIndents } from '@angular-devkit/core/src/utils/literals';
import {
checkFilesDoNotExist,
checkFilesExist,
expectJestTestsToPass,
killPorts,
newProject,
packageInstall,
Expand Down Expand Up @@ -359,9 +360,10 @@ describe('nest libraries', function () {
const jestConfigContent = readFile(`libs/${nestlib}/jest.config.ts`);

expect(stripIndents`${jestConfigContent}`).toEqual(
stripIndents`module.exports = {
stripIndents`/* eslint-disable */
export default {
displayName: '${nestlib}',
preset: '../../jest.preset.ts',
preset: '../../jest.preset.js',
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
Expand Down Expand Up @@ -481,4 +483,8 @@ exports.FooModel = FooModel;
checkFilesDoNotExist('workspace.json', 'angular.json')
).not.toThrow();
}, 1000000);

it('should run default jest tests', async () => {
await expectJestTestsToPass('@nrwl/node:lib');
});
});
6 changes: 6 additions & 0 deletions e2e/react/src/react.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
checkFilesDoNotExist,
checkFilesExist,
createFile,
expectJestTestsToPass,
killPorts,
newProject,
readFile,
Expand Down Expand Up @@ -289,4 +290,9 @@ describe('React Applications and Libs with PostCSS', () => {
expect(buildResults.combinedOutput).toMatch(/HELLO FROM APP/);
expect(buildResults.combinedOutput).not.toMatch(/HELLO FROM LIB/);
}, 250_000);

it('should run default jest tests', async () => {
await expectJestTestsToPass('@nrwl/react:lib');
await expectJestTestsToPass('@nrwl/react:app');
});
});
27 changes: 27 additions & 0 deletions e2e/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -881,3 +881,30 @@ export function waitUntil(
}, opts.timeout);
});
}

type GeneratorsWithDefaultTests =
| '@nrwl/js:lib'
| '@nrwl/node:lib'
| '@nrwl/react:lib'
| '@nrwl/react:app'
| '@nrwl/next:app'
| '@nrwl/angular:app'
| '@nrwl/workspace:lib'
| '@nrwl/web:app';

/**
* Runs the pass in generator and then runs test on
* the generated project to make sure the default tests pass.
*/
export async function expectJestTestsToPass(
generator: GeneratorsWithDefaultTests | string
) {
const name = uniq('proj');
const generatedResults = runCLI(
`generate ${generator} ${name} --no-interactive`
);
expect(generatedResults).toContain(`jest.config.ts`);

const results = await runCLIAsync(`test ${name}`);
expect(results.combinedOutput).toContain('Test Suites: 1 passed, 1 total');
}
5 changes: 5 additions & 0 deletions e2e/web/src/web.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
uniq,
updateFile,
updateProjectConfig,
expectJestTestsToPass,
} from '@nrwl/e2e/utils';

describe('Web Components Applications', () => {
Expand Down Expand Up @@ -186,6 +187,10 @@ describe('Web Components Applications', () => {
checkFilesDoNotExist('workspace.json', 'angular.json')
).not.toThrow();
}, 1000000);

it('should run default jest tests', async () => {
await expectJestTestsToPass('@nrwl/web:app');
});
});

describe('CLI - Environment Variables', () => {
Expand Down
6 changes: 3 additions & 3 deletions e2e/workspace-core/src/aux-commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ describe('move project', () => {
checkFilesExist(jestConfigPath);
const jestConfig = readFile(jestConfigPath);
expect(jestConfig).toContain(`displayName: 'shared-${lib1}-data-access'`);
expect(jestConfig).toContain(`preset: '../../../../jest.preset.ts'`);
expect(jestConfig).toContain(`preset: '../../../../jest.preset.js'`);
expect(jestConfig).toContain(`'../../../../coverage/${newPath}'`);

const tsConfigPath = `${newPath}/tsconfig.json`;
Expand Down Expand Up @@ -399,7 +399,7 @@ describe('move project', () => {
checkFilesExist(jestConfigPath);
const jestConfig = readFile(jestConfigPath);
expect(jestConfig).toContain(`displayName: 'shared-${lib1}-data-access'`);
expect(jestConfig).toContain(`preset: '../../../../jest.preset.ts'`);
expect(jestConfig).toContain(`preset: '../../../../jest.preset.js'`);
expect(jestConfig).toContain(`'../../../../coverage/${newPath}'`);

const tsConfigPath = `${newPath}/tsconfig.json`;
Expand Down Expand Up @@ -531,7 +531,7 @@ describe('move project', () => {
checkFilesExist(jestConfigPath);
const jestConfig = readFile(jestConfigPath);
expect(jestConfig).toContain(`displayName: 'shared-${lib1}-data-access'`);
expect(jestConfig).toContain(`preset: '../../../../jest.preset.ts'`);
expect(jestConfig).toContain(`preset: '../../../../jest.preset.js'`);
expect(jestConfig).toContain(`'../../../../coverage/${newPath}'`);

const tsConfigPath = `${newPath}/tsconfig.json`;
Expand Down
12 changes: 3 additions & 9 deletions e2e/workspace-core/src/workspace-lib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
runCLIAsync,
uniq,
updateFile,
expectJestTestsToPass,
} from '@nrwl/e2e/utils';

let proj: string;
Expand Down Expand Up @@ -44,15 +45,8 @@ describe('@nrwl/workspace:library', () => {
});

describe('unit testing', () => {
it('should support jest', async () => {
const libName = uniq('mylib');

runCLI(`generate @nrwl/workspace:lib ${libName}`);

const { stderr: result } = await runCLIAsync(`test ${libName}`);

expect(result).toContain(`Test Suites: 1 passed, 1 total`);
expect(result).toContain('Tests: 1 passed, 1 total');
it('should run default jest tests', async () => {
await expectJestTestsToPass('@nrwl/workspace:lib');
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Array [
"apps/one/two/test-ui-lib-e2e/src/support/index.ts",
"apps/one/two/test-ui-lib-e2e/tsconfig.json",
"jest.config.ts",
"jest.preset.ts",
"jest.preset.js",
"libs/test-ui-lib/.eslintrc.json",
"libs/test-ui-lib/.storybook/main.js",
"libs/test-ui-lib/.storybook/preview.js",
Expand Down Expand Up @@ -160,7 +160,7 @@ Array [
"apps/test-ui-lib-e2e/src/support/index.ts",
"apps/test-ui-lib-e2e/tsconfig.json",
"jest.config.ts",
"jest.preset.ts",
"jest.preset.js",
"libs/test-ui-lib/.eslintrc.json",
"libs/test-ui-lib/.storybook/main.js",
"libs/test-ui-lib/.storybook/preview.js",
Expand Down
6 changes: 6 additions & 0 deletions packages/jest/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@
"cli": "nx",
"description": "Update move jest config files to .ts files.",
"factory": "./src/migrations/update-14-0-0/update-jest-config-ext"
},
"update-to-export-default": {
"version": "14.1.5-beta.0",
"cli": "nx",
"description": "Update to export default in jest config and revert jest.preset.ts to jest.preset.js",
"factory": "./src/migrations/update-14-1-5/update-exports-jest-config"
}
},
"packageJsonUpdates": {
Expand Down
4 changes: 3 additions & 1 deletion packages/jest/preset/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export = require('./jest-preset');
import { nxPreset } from './jest-preset';

export default nxPreset;
2 changes: 1 addition & 1 deletion packages/jest/preset/jest-preset.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export = {
export const nxPreset = {
testMatch: ['**/+(*.)+(spec|test).+(ts|js)?(x)'],
resolver: '@nrwl/jest/plugins/resolver',
moduleFileExtensions: ['ts', 'js', 'mjs', 'html'],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`jest should generate files 1`] = `
"import { getJestProjects } from '@nrwl/jest';

export default {
projects: getJestProjects()
};"
`;

exports[`jest should generate files 2`] = `
"const nxPreset = require('@nrwl/jest/preset').default;

module.exports = { ...nxPreset }"
`;

exports[`jest should generate files with --js flag 1`] = `
"const { getJestProjects } = require('@nrwl/jest');

module.exports = {
projects: getJestProjects()
};"
`;

exports[`jest should generate files with --js flag 2`] = `
"const nxPreset = require('@nrwl/jest/preset').default;

module.exports = { ...nxPreset }"
`;
29 changes: 20 additions & 9 deletions packages/jest/src/generators/init/init.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readJson, Tree, writeJson } from '@nrwl/devkit';
import { readJson, stripIndents, Tree, writeJson } from '@nrwl/devkit';
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import { jestInitGenerator } from './init';

Expand All @@ -9,17 +9,28 @@ describe('jest', () => {
tree = createTreeWithEmptyWorkspace();
});

it('should generate files', async () => {
it('should generate files with --js flag', async () => {
jestInitGenerator(tree, { js: true });

expect(tree.exists('jest.config.js')).toBeTruthy();
expect(
stripIndents`${tree.read('jest.config.js', 'utf-8')}`
).toMatchSnapshot();
expect(
stripIndents`${tree.read('jest.preset.js', 'utf-8')}`
).toMatchSnapshot();
});

it('should generate files ', async () => {
jestInitGenerator(tree, {});

expect(tree.exists('jest.config.ts')).toBeTruthy();
expect(tree.read('jest.config.ts', 'utf-8')).toMatchInlineSnapshot(`
"const { getJestProjects } = require('@nrwl/jest');

module.exports = {
projects: getJestProjects()
};"
`);
expect(
stripIndents`${tree.read('jest.config.ts', 'utf-8')}`
).toMatchSnapshot();
expect(
stripIndents`${tree.read('jest.preset.js', 'utf-8')}`
).toMatchSnapshot();
});

it('should not override existing files', async () => {
Expand Down
30 changes: 18 additions & 12 deletions packages/jest/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,28 @@ const schemaDefaults = {
function createJestConfig(host: Tree, js: boolean = false) {
// if the root ts config already exists then don't make a js one or vice versa
if (!host.exists('jest.config.ts') && !host.exists('jest.config.js')) {
host.write(
`jest.config.${js ? 'js' : 'ts'}`,
stripIndents`
const { getJestProjects } = require('@nrwl/jest');

module.exports = {
projects: getJestProjects()
};`
);
const contents = js
? stripIndents`
const { getJestProjects } = require('@nrwl/jest');

module.exports = {
projects: getJestProjects()
};`
: stripIndents`
import { getJestProjects } from '@nrwl/jest';

export default {
projects: getJestProjects()
};`;
host.write(`jest.config.${js ? 'js' : 'ts'}`, contents);
}

if (!host.exists('jest.preset.ts') && !host.exists('jest.preset.js')) {
if (!host.exists('jest.preset.js')) {
// preset is always js file.
host.write(
`jest.preset.${js ? 'js' : 'ts'}`,
`jest.preset.js`,
`
const nxPreset = require('@nrwl/jest/preset');
const nxPreset = require('@nrwl/jest/preset').default;

module.exports = { ...nxPreset }`
);
Expand Down