From a9ce7e83bf1c8d66c4cb30e08ce06415502130b1 Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Wed, 9 Sep 2020 10:46:27 -0400 Subject: [PATCH 1/4] feat(testing): add projects into jest config --- packages/jest/migrations.json | 5 + packages/jest/preset/jest-preset.ts | 9 + .../jest/src/builders/jest/jest.impl.spec.ts | 8 + packages/jest/src/builders/jest/jest.impl.ts | 16 +- packages/jest/src/builders/jest/schema.d.ts | 2 +- .../update-projects-property.spec.ts | 161 ++++++++++++++++++ .../update-10-3-0/update-projects-property.ts | 98 +++++++++++ .../jest/src/schematics/init/init.spec.ts | 9 +- packages/jest/src/schematics/init/init.ts | 30 ++-- .../jest-project/files/jest.config.js__tmpl__ | 4 +- .../jest-project/jest-project.spec.ts | 20 ++- .../schematics/jest-project/jest-project.ts | 2 + .../jest-project/lib/update-jestconfig.ts | 18 ++ packages/jest/src/utils/config/functions.ts | 22 ++- .../src/utils/config/update-config.spec.ts | 2 +- .../src/schematics/library/library.spec.ts | 2 +- 16 files changed, 365 insertions(+), 43 deletions(-) create mode 100644 packages/jest/preset/jest-preset.ts create mode 100644 packages/jest/src/migrations/update-10-3-0/update-projects-property.spec.ts create mode 100644 packages/jest/src/migrations/update-10-3-0/update-projects-property.ts create mode 100644 packages/jest/src/schematics/jest-project/lib/update-jestconfig.ts diff --git a/packages/jest/migrations.json b/packages/jest/migrations.json index db222e7d82bbc..03a6134307b17 100644 --- a/packages/jest/migrations.json +++ b/packages/jest/migrations.json @@ -39,6 +39,11 @@ "version": "10.2.0", "description": "Remove deprecated jest builder options", "factory": "./src/migrations/update-10-2-0/update-10-2-0" + }, + "update-projects-property": { + "version": "10.3.0-beta.1", + "description": "Adds all jest projects into the root jest config", + "factory": "./src/migrations/update-10-3-0/update-projects-property" } }, "packageJsonUpdates": { diff --git a/packages/jest/preset/jest-preset.ts b/packages/jest/preset/jest-preset.ts new file mode 100644 index 0000000000000..9b614a8dca53b --- /dev/null +++ b/packages/jest/preset/jest-preset.ts @@ -0,0 +1,9 @@ +module.exports = { + testMatch: ['**/+(*.)+(spec|test).+(ts|js)?(x)'], + resolver: '@nrwl/jest/plugins/resolver', + moduleFileExtensions: ['ts', 'js', 'html'], + coverageReporters: ['html'], + transform: { + '^.+\\.(ts|js|html)$': 'ts-jest', + }, +}; diff --git a/packages/jest/src/builders/jest/jest.impl.spec.ts b/packages/jest/src/builders/jest/jest.impl.spec.ts index da50800f42d6b..6d5d596d947ed 100644 --- a/packages/jest/src/builders/jest/jest.impl.spec.ts +++ b/packages/jest/src/builders/jest/jest.impl.spec.ts @@ -63,6 +63,7 @@ describe('Jest Builder', () => { _: [], testPathPattern: [], watch: false, + projects: ['/root/jest.config.js'], }, ['/root/jest.config.js'] ); @@ -103,6 +104,7 @@ describe('Jest Builder', () => { coverageReporters: ['test'], coverageDirectory: '/test/path', watch: false, + projects: ['/root/jest.config.js'], }, ['/root/jest.config.js'] ); @@ -133,6 +135,7 @@ describe('Jest Builder', () => { testNamePattern: 'should load', testPathPattern: [], watch: false, + projects: ['/root/jest.config.js'], }, ['/root/jest.config.js'] ); @@ -201,6 +204,7 @@ describe('Jest Builder', () => { watch: false, watchAll: false, testLocationInResults: true, + projects: ['/root/jest.config.js'], }, ['/root/jest.config.js'] ); @@ -222,6 +226,7 @@ describe('Jest Builder', () => { _: [], maxWorkers: '50%', testPathPattern: [], + projects: ['/root/jest.config.js'], }, ['/root/jest.config.js'] ); @@ -245,6 +250,7 @@ describe('Jest Builder', () => { setupFilesAfterEnv: ['/root/test-setup.ts'], testPathPattern: [], watch: false, + projects: ['/root/jest.config.js'], }, ['/root/jest.config.js'] ); @@ -293,6 +299,7 @@ describe('Jest Builder', () => { setupFilesAfterEnv: ['/root/test-setup.ts'], testPathPattern: [], watch: false, + projects: ['/root/jest.config.js'], }, ['/root/jest.config.js'] ); @@ -330,6 +337,7 @@ describe('Jest Builder', () => { _: [], testPathPattern: [], watch: false, + projects: ['/root/jest.config.js'], }, ['/root/jest.config.js'] ); diff --git a/packages/jest/src/builders/jest/jest.impl.ts b/packages/jest/src/builders/jest/jest.impl.ts index b24aac04ade84..0019a1a243cd6 100644 --- a/packages/jest/src/builders/jest/jest.impl.ts +++ b/packages/jest/src/builders/jest/jest.impl.ts @@ -8,6 +8,7 @@ import * as path from 'path'; import { from, Observable } from 'rxjs'; import { map } from 'rxjs/operators'; import { JestBuilderOptions } from './schema'; +import { Config } from '@jest/types'; try { require('dotenv').config(); @@ -15,7 +16,7 @@ try { // noop } -if (process.env.NODE_ENV == null || process.env.NODE_ENV == undefined) { +if (process.env.NODE_ENV === null || process.env.NODE_ENV === undefined) { (process.env as any).NODE_ENV = 'test'; } @@ -40,7 +41,8 @@ function run( ); } - const config: any = { + const config: Config.Argv = { + $0: undefined, _: [], config: options.config, coverage: options.codeCoverage, @@ -67,6 +69,7 @@ function run( useStderr: options.useStderr, watch: options.watch, watchAll: options.watchAll, + projects: [options.jestConfig], }; // for backwards compatibility @@ -98,14 +101,17 @@ function run( config.reporters = options.reporters; } - if (options.coverageReporters && options.coverageReporters.length > 0) { + if ( + Array.isArray(options.coverageReporters) && + options.coverageReporters.length > 0 + ) { config.coverageReporters = options.coverageReporters; } return from(runCLI(config, [options.jestConfig])).pipe( - map((results) => { + map(({ results }) => { return { - success: results.results.success, + success: results.success, }; }) ); diff --git a/packages/jest/src/builders/jest/schema.d.ts b/packages/jest/src/builders/jest/schema.d.ts index d04905bb3c61b..f74bbb09ac7a5 100644 --- a/packages/jest/src/builders/jest/schema.d.ts +++ b/packages/jest/src/builders/jest/schema.d.ts @@ -25,7 +25,7 @@ export interface JestBuilderOptions extends JsonObject { colors?: boolean; reporters?: string[]; verbose?: boolean; - coverageReporters?: string; + coverageReporters?: string[]; coverageDirectory?: string; testResultsProcessor?: string; updateSnapshot?: boolean; diff --git a/packages/jest/src/migrations/update-10-3-0/update-projects-property.spec.ts b/packages/jest/src/migrations/update-10-3-0/update-projects-property.spec.ts new file mode 100644 index 0000000000000..af21f7b245a02 --- /dev/null +++ b/packages/jest/src/migrations/update-10-3-0/update-projects-property.spec.ts @@ -0,0 +1,161 @@ +import { tags } from '@angular-devkit/core'; +import { Tree } from '@angular-devkit/schematics'; +import { SchematicTestRunner } from '@angular-devkit/schematics/testing'; +import { serializeJson } from '@nrwl/workspace'; +import { createEmptyWorkspace } from '@nrwl/workspace/testing'; +import * as path from 'path'; + +describe('update projects property', () => { + let initialTree: Tree; + let schematicRunner: SchematicTestRunner; + + beforeEach(() => { + initialTree = createEmptyWorkspace(Tree.empty()); + + initialTree.create( + 'jest.config.js', + tags.stripIndents` + module.exports = { + testMatch: ['**/+(*.)+(spec|test).+(ts|js)?(x)'], + transform: { + '^.+\\\\.(ts|js|html)$': 'ts-jest', + }, + maxWorkers: 2, + }; + ` + ); + + initialTree.create( + 'apps/products/jest.config.js', + tags.stripIndents` + module.exports = { + name: 'products', + preset: '../../jest.config.js', + coverageDirectory: '../../coverage/apps/products', + snapshotSerializers: [ + 'jest-preset-angular/build/AngularSnapshotSerializer.js', + 'jest-preset-angular/build/HTMLCommentSerializer.js' + ], + setupFilesAfterEnv: ['/src/test-setup.ts'], + globals: { + 'ts-jest': { + tsConfig: '/tsconfig.spec.json', + stringifyContentPathRegex: '\\.(html|svg)$', + astTransformers: [ + 'jest-preset-angular/build/InlineFilesTransformer', + 'jest-preset-angular/build/StripStylesTransformer' + ] + } + } + }; + ` + ); + + initialTree.overwrite( + 'workspace.json', + serializeJson({ + version: 1, + projects: { + products: { + root: 'apps/products', + sourceRoot: 'apps/products/src', + architect: { + build: { + builder: '@angular-devkit/build-angular:browser', + }, + test: { + builder: '@nrwl/jest:jest', + options: { + jestConfig: 'apps/products/jest.config.js', + tsConfig: 'apps/products/tsconfig.spec.json', + setupFile: 'apps/products/src/test-setup.ts', + passWithNoTests: true, + }, + }, + }, + }, + cart: { + root: 'apps/cart', + sourceRoot: 'apps/cart/src', + architect: { + build: { + builder: '@nrwl/web:build', + }, + test: { + builder: '@nrwl/jest:jest', + options: { + jestConfig: 'apps/cart/jest.config.js', + passWithNoTests: true, + }, + }, + }, + }, + basket: { + root: 'apps/basket', + sourceRoot: 'apps/basket/src', + architect: { + build: { + builder: '@nrwl/web:build', + }, + }, + }, + }, + }) + ); + schematicRunner = new SchematicTestRunner( + '@nrwl/jest', + path.join(__dirname, '../../../migrations.json') + ); + }); + + it('should remove setupFile and tsconfig in test architect from workspace.json', async (done) => { + const result = await schematicRunner + .runSchematicAsync('update-projects-property', {}, initialTree) + .toPromise(); + + const updatedJestConfig = result.readContent('jest.config.js'); + expect(tags.stripIndents`${updatedJestConfig}`).toEqual(tags.stripIndents` + module.exports = { + projects: ['/apps/products', '/apps/cart'], + }; + `); + + const jestPreset = result.readContent('jest.preset.js'); + expect(tags.stripIndents`${jestPreset}`).toEqual(tags.stripIndents` + module.exports = { + testMatch: ['**/+(*.)+(spec|test).+(ts|js)?(x)'], + transform: { + '^.+\\\\.(ts|js|html)$': 'ts-jest', + }, + maxWorkers: 2, + preset: '@nrwl/jest/preset', + }; + `); + + const projectConfig = result.readContent('apps/products/jest.config.js'); + expect(tags.stripIndents`${projectConfig}`).toEqual(tags.stripIndents` + module.exports = { + preset: '../../jest.preset.js', + coverageDirectory: '../../coverage/apps/products', + snapshotSerializers: [ + 'jest-preset-angular/build/AngularSnapshotSerializer.js', + 'jest-preset-angular/build/HTMLCommentSerializer.js', + ], + setupFilesAfterEnv: ['/src/test-setup.ts'], + globals: { + 'ts-jest': { + tsConfig: '/tsconfig.spec.json', + stringifyContentPathRegex: '\\.(html|svg)$', + astTransformers: [ + 'jest-preset-angular/build/InlineFilesTransformer', + 'jest-preset-angular/build/StripStylesTransformer', + ], + }, + }, + displayName: 'products', + }; + `); + + done(); + }); +}); diff --git a/packages/jest/src/migrations/update-10-3-0/update-projects-property.ts b/packages/jest/src/migrations/update-10-3-0/update-projects-property.ts new file mode 100644 index 0000000000000..97819a80832a9 --- /dev/null +++ b/packages/jest/src/migrations/update-10-3-0/update-projects-property.ts @@ -0,0 +1,98 @@ +import { + chain, + Rule, + SchematicContext, + Tree, +} from '@angular-devkit/schematics'; +import { + formatFiles, + getWorkspace, + offsetFromRoot, + serializeJson, +} from '@nrwl/workspace'; +import { + addPropertyToJestConfig, + removePropertyFromJestConfig, +} from '../../utils/config/update-config'; + +function updateRootJestConfig(): Rule { + return async (host: Tree, context: SchematicContext) => { + const workspace = await getWorkspace(host); + + const rootDirs = []; + for (const [projectName, project] of workspace.projects) { + for (const [, target] of project.targets) { + if (target.builder !== '@nrwl/jest:jest') { + continue; + } + + rootDirs.push(`/${project.root}`); + + try { + addPropertyToJestConfig( + host, + target.options.jestConfig as string, + 'preset', + `${offsetFromRoot(project.root)}jest.preset.js` + ); + addPropertyToJestConfig( + host, + target.options.jestConfig as string, + 'displayName', + projectName + ); + removePropertyFromJestConfig( + host, + target.options.jestConfig as string, + 'name' + ); + } catch { + context.logger.error( + `Unable to update the jest preset for project ${projectName}. Please manually add "@nrwl/jest/preset" as the preset.` + ); + } + } + } + + if (rootDirs.length == 0) { + return; + } else { + try { + context.logger.info(` +The root jest.config.js file will be updated to include all references to each individual project's jest config. +A new jest.preset.js file will be created that would have your existing configuration. All projects will now have this preset. + `); + + const existingRootConfig = host + .read('jest.config.js') + .toString('utf-8'); + + host.create('jest.preset.js', existingRootConfig); + addPropertyToJestConfig( + host, + 'jest.preset.js', + 'preset', + '@nrwl/jest/preset' + ); + + host.overwrite( + 'jest.config.js', + ` + module.exports = { + projects: ${serializeJson(rootDirs)} + } + ` + ); + } catch { + context.logger.error(` +Unable to update the root jest.config.js with projects. Please add the "projects" property to the exported jest config with the following: +${serializeJson(rootDirs)} + `); + } + } + }; +} + +export default function update(): Rule { + return chain([updateRootJestConfig(), formatFiles()]); +} diff --git a/packages/jest/src/schematics/init/init.spec.ts b/packages/jest/src/schematics/init/init.spec.ts index 4e13d9c601e14..849626a1d5f91 100644 --- a/packages/jest/src/schematics/init/init.spec.ts +++ b/packages/jest/src/schematics/init/init.spec.ts @@ -1,3 +1,4 @@ +import { tags } from '@angular-devkit/core'; import { Tree } from '@angular-devkit/schematics'; import { readJsonInTree } from '@nrwl/workspace'; import { createEmptyWorkspace } from '@nrwl/workspace/testing'; @@ -19,13 +20,7 @@ describe('jest', () => { expect(resultTree.exists('jest.config.js')).toBeTruthy(); expect(resultTree.readContent('jest.config.js')).toMatchInlineSnapshot(` "module.exports = { - testMatch: ['**/+(*.)+(spec|test).+(ts|js)?(x)'], - transform: { - '^.+\\\\\\\\.(ts|js|html)$': 'ts-jest' - }, - resolver: '@nrwl/jest/plugins/resolver', - moduleFileExtensions: ['ts', 'js', 'html'], - coverageReporters: ['html'] + projects: [] };" `); }); diff --git a/packages/jest/src/schematics/init/init.ts b/packages/jest/src/schematics/init/init.ts index 45a5c2cbe5936..5a0db5e5332b4 100644 --- a/packages/jest/src/schematics/init/init.ts +++ b/packages/jest/src/schematics/init/init.ts @@ -44,23 +44,23 @@ const removeNrwlJestFromDeps = (host: Tree) => { }; const createJestConfig = (host: Tree) => { - if (host.exists('jest.config.js')) { - return; - } - - host.create( - 'jest.config.js', - stripIndents` + if (!host.exists('jest.config.js')) { + host.create( + 'jest.config.js', + stripIndents` module.exports = { - testMatch: ['**/+(*.)+(spec|test).+(ts|js)?(x)'], - transform: { - '^.+\\.(ts|js|html)$': 'ts-jest' - }, - resolver: '@nrwl/jest/plugins/resolver', - moduleFileExtensions: ['ts', 'js', 'html'], - coverageReporters: ['html'] + projects: [] };` - ); + ); + } + + if (!host.exists('jest.preset.js')) { + host.create( + 'jest.preset.js', + ` + module.exports = { preset: '@nrwl/jest/preset' }` + ); + } }; function updateDependencies(options: NormalizedSchema): Rule { diff --git a/packages/jest/src/schematics/jest-project/files/jest.config.js__tmpl__ b/packages/jest/src/schematics/jest-project/files/jest.config.js__tmpl__ index 567cc52fabd37..18593c456ad70 100644 --- a/packages/jest/src/schematics/jest-project/files/jest.config.js__tmpl__ +++ b/packages/jest/src/schematics/jest-project/files/jest.config.js__tmpl__ @@ -1,6 +1,6 @@ module.exports = { - name: '<%= project %>', - preset: '<%= offsetFromRoot %>jest.config.js',<% if(setupFile !== 'none') { %> + displayName: '<%= project %>', + preset: '<%= offsetFromRoot %>jest.preset.js',<% if(setupFile !== 'none') { %> setupFilesAfterEnv: ['/src/test-setup.ts'],<% } %><% if (transformer === 'ts-jest') { %> globals: { 'ts-jest': { diff --git a/packages/jest/src/schematics/jest-project/jest-project.spec.ts b/packages/jest/src/schematics/jest-project/jest-project.spec.ts index 1a20d4a2866c0..8a8a2ee2329aa 100644 --- a/packages/jest/src/schematics/jest-project/jest-project.spec.ts +++ b/packages/jest/src/schematics/jest-project/jest-project.spec.ts @@ -4,6 +4,7 @@ import { createEmptyWorkspace } from '@nrwl/workspace/testing'; import { callRule, runSchematic } from '../../utils/testing'; import { tags } from '@angular-devkit/core'; import { JestProjectSchema } from './schema.d'; +import { jestConfigObject } from '../../utils/config/functions'; describe('jestProject', () => { let appTree: Tree; @@ -29,7 +30,7 @@ describe('jestProject', () => { appTree ); appTree = await callRule( - updateJsonInTree('libs/lib1/tsconfig.json', (json) => { + updateJsonInTree('libs/lib1/tsconfig.json', () => { return { files: [], include: [], @@ -87,8 +88,8 @@ describe('jestProject', () => { expect( tags.stripIndents`${resultTree.readContent('libs/lib1/jest.config.js')}` ).toBe(tags.stripIndents`module.exports = { - name: 'lib1', - preset: '../../jest.config.js', + displayName: 'lib1', + preset: '@nrwl/jest/preset', globals: { 'ts-jest': { tsConfig: '/tsconfig.spec.json', @@ -104,6 +105,19 @@ describe('jestProject', () => { `); }); + it('should add a project reference in the root jest.config.js', async () => { + const resultTree = await runSchematic( + 'jest-project', + { + project: 'lib1', + }, + appTree + ); + const jestConfig = jestConfigObject(resultTree, 'jest.config.js'); + + expect(jestConfig.projects).toEqual(['/libs/lib1']); + }); + it('should add a reference to solution tsconfig.json', async () => { const resultTree = await runSchematic( 'jest-project', diff --git a/packages/jest/src/schematics/jest-project/jest-project.ts b/packages/jest/src/schematics/jest-project/jest-project.ts index e493d8b36d52d..37f1c2a586d61 100644 --- a/packages/jest/src/schematics/jest-project/jest-project.ts +++ b/packages/jest/src/schematics/jest-project/jest-project.ts @@ -4,6 +4,7 @@ import { checkForTestTarget } from './lib/check-for-test-target'; import { generateFiles } from './lib/generate-files'; import { updateTsConfig } from './lib/update-tsconfig'; import { updateWorkspace } from './lib/update-workspace'; +import { updateJestConfig } from './lib/update-jestconfig'; import { JestProjectSchema } from './schema'; const schemaDefaults = { @@ -45,5 +46,6 @@ export default function (schema: JestProjectSchema): Rule { generateFiles(options), updateTsConfig(options), updateWorkspace(options), + updateJestConfig(options), ]); } diff --git a/packages/jest/src/schematics/jest-project/lib/update-jestconfig.ts b/packages/jest/src/schematics/jest-project/lib/update-jestconfig.ts new file mode 100644 index 0000000000000..b16421f4736c4 --- /dev/null +++ b/packages/jest/src/schematics/jest-project/lib/update-jestconfig.ts @@ -0,0 +1,18 @@ +import { Rule, Tree } from '@angular-devkit/schematics'; +import { getWorkspace } from '@nrwl/workspace'; + +import { JestProjectSchema } from '../schema'; +import { addPropertyToJestConfig } from '../../../utils/config/update-config'; + +export function updateJestConfig(options: JestProjectSchema): Rule { + return async (host: Tree) => { + const workspace = await getWorkspace(host); + const project = workspace.projects.get(options.project); + addPropertyToJestConfig( + host, + 'jest.config.js', + 'projects', + `/${project.root}` + ); + }; +} diff --git a/packages/jest/src/utils/config/functions.ts b/packages/jest/src/utils/config/functions.ts index 1babb3cf9a185..c7d09515bb691 100644 --- a/packages/jest/src/utils/config/functions.ts +++ b/packages/jest/src/utils/config/functions.ts @@ -89,14 +89,20 @@ export function addOrUpdateProperty( return []; } - return [ - createInsertChange( - path, - value, - arrayLiteral.elements.end, - arrayLiteral.elements.hasTrailingComma - ), - ]; + if (arrayLiteral.elements.length === 0) { + return [ + new InsertChange(path, arrayLiteral.elements.end, value as string), + ]; + } else { + return [ + createInsertChange( + path, + value, + arrayLiteral.elements.end, + arrayLiteral.elements.hasTrailingComma + ), + ]; + } } else if ( propertyAssignment.initializer.kind === ts.SyntaxKind.ObjectLiteralExpression diff --git a/packages/jest/src/utils/config/update-config.spec.ts b/packages/jest/src/utils/config/update-config.spec.ts index c61156b5d26e6..d920915609d53 100644 --- a/packages/jest/src/utils/config/update-config.spec.ts +++ b/packages/jest/src/utils/config/update-config.spec.ts @@ -29,7 +29,7 @@ describe('Update jest.config.js', () => { childArray: ['value1', 'value2'] } }, - numeric: 0 + numeric: 0, } ` ); diff --git a/packages/nest/src/schematics/library/library.spec.ts b/packages/nest/src/schematics/library/library.spec.ts index f027ce99e0625..8dc45934e5fee 100644 --- a/packages/nest/src/schematics/library/library.spec.ts +++ b/packages/nest/src/schematics/library/library.spec.ts @@ -415,7 +415,7 @@ describe('lib', () => { const jestConfig = getFileContent(tree, 'libs/my-lib/jest.config.js'); expect(stripIndents`${jestConfig}`) .toEqual(stripIndents`module.exports = { - name: 'my-lib', + displayName: 'my-lib', preset: '../../jest.config.js', globals: { 'ts-jest': { From 08fb6c0f39a978a3af3ae0c54003594a95f92868 Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Mon, 21 Sep 2020 10:35:07 -0400 Subject: [PATCH 2/4] chore(testing): update unit tests and fix presets with spreading a default --- .../__snapshots__/configuration.spec.ts.snap | 1 + packages/jest/preset/index.ts | 1 + packages/jest/preset/jest-preset.ts | 2 +- .../update-projects-property.spec.ts | 3 +- .../update-10-3-0/update-projects-property.ts | 29 ++++++++++++------- packages/jest/src/schematics/init/init.ts | 4 ++- .../jest-project/jest-project.spec.ts | 2 +- .../src/schematics/library/library.spec.ts | 2 +- .../application/application.spec.ts | 4 +-- .../src/schematics/library/library.spec.ts | 4 +-- .../application/application.spec.ts | 4 +-- 11 files changed, 35 insertions(+), 21 deletions(-) create mode 100644 packages/jest/preset/index.ts diff --git a/packages/angular/src/schematics/storybook-configuration/__snapshots__/configuration.spec.ts.snap b/packages/angular/src/schematics/storybook-configuration/__snapshots__/configuration.spec.ts.snap index 1c23638c73f6a..209ac906636a2 100644 --- a/packages/angular/src/schematics/storybook-configuration/__snapshots__/configuration.spec.ts.snap +++ b/packages/angular/src/schematics/storybook-configuration/__snapshots__/configuration.spec.ts.snap @@ -8,6 +8,7 @@ Array [ "/tsconfig.base.json", "/tslint.json", "/jest.config.js", + "/jest.preset.js", "/libs/test-ui-lib/README.md", "/libs/test-ui-lib/tsconfig.lib.json", "/libs/test-ui-lib/tslint.json", diff --git a/packages/jest/preset/index.ts b/packages/jest/preset/index.ts new file mode 100644 index 0000000000000..6256a436c833d --- /dev/null +++ b/packages/jest/preset/index.ts @@ -0,0 +1 @@ +export = require('./jest-preset'); diff --git a/packages/jest/preset/jest-preset.ts b/packages/jest/preset/jest-preset.ts index 9b614a8dca53b..9168996861e84 100644 --- a/packages/jest/preset/jest-preset.ts +++ b/packages/jest/preset/jest-preset.ts @@ -1,4 +1,4 @@ -module.exports = { +export = { testMatch: ['**/+(*.)+(spec|test).+(ts|js)?(x)'], resolver: '@nrwl/jest/plugins/resolver', moduleFileExtensions: ['ts', 'js', 'html'], diff --git a/packages/jest/src/migrations/update-10-3-0/update-projects-property.spec.ts b/packages/jest/src/migrations/update-10-3-0/update-projects-property.spec.ts index af21f7b245a02..b1c58049c7e08 100644 --- a/packages/jest/src/migrations/update-10-3-0/update-projects-property.spec.ts +++ b/packages/jest/src/migrations/update-10-3-0/update-projects-property.spec.ts @@ -122,13 +122,14 @@ describe('update projects property', () => { const jestPreset = result.readContent('jest.preset.js'); expect(tags.stripIndents`${jestPreset}`).toEqual(tags.stripIndents` + const nxPreset = require('@nrwl/jest/preset'); module.exports = { + ...nxPreset, testMatch: ['**/+(*.)+(spec|test).+(ts|js)?(x)'], transform: { '^.+\\\\.(ts|js|html)$': 'ts-jest', }, maxWorkers: 2, - preset: '@nrwl/jest/preset', }; `); diff --git a/packages/jest/src/migrations/update-10-3-0/update-projects-property.ts b/packages/jest/src/migrations/update-10-3-0/update-projects-property.ts index 97819a80832a9..56d7effb3b6d4 100644 --- a/packages/jest/src/migrations/update-10-3-0/update-projects-property.ts +++ b/packages/jest/src/migrations/update-10-3-0/update-projects-property.ts @@ -7,6 +7,8 @@ import { import { formatFiles, getWorkspace, + insert, + InsertChange, offsetFromRoot, serializeJson, } from '@nrwl/workspace'; @@ -14,6 +16,7 @@ import { addPropertyToJestConfig, removePropertyFromJestConfig, } from '../../utils/config/update-config'; +import { jestConfigObjectAst } from '../../utils/config/functions'; function updateRootJestConfig(): Rule { return async (host: Tree, context: SchematicContext) => { @@ -63,17 +66,23 @@ The root jest.config.js file will be updated to include all references to each i A new jest.preset.js file will be created that would have your existing configuration. All projects will now have this preset. `); - const existingRootConfig = host - .read('jest.config.js') - .toString('utf-8'); + let existingRootConfig = host.read('jest.config.js').toString('utf-8'); - host.create('jest.preset.js', existingRootConfig); - addPropertyToJestConfig( - host, - 'jest.preset.js', - 'preset', - '@nrwl/jest/preset' - ); + existingRootConfig = + "const nxPreset = require('@nrwl/jest/preset'); \n" + + existingRootConfig; + + const presetPath = 'jest.preset.js'; + + host.create(presetPath, existingRootConfig); + const configObject = jestConfigObjectAst(host, presetPath); + insert(host, presetPath, [ + new InsertChange( + presetPath, + configObject.getStart() + 1, + '\n...nxPreset,' + ), + ]); host.overwrite( 'jest.config.js', diff --git a/packages/jest/src/schematics/init/init.ts b/packages/jest/src/schematics/init/init.ts index 5a0db5e5332b4..4c652c8c1a650 100644 --- a/packages/jest/src/schematics/init/init.ts +++ b/packages/jest/src/schematics/init/init.ts @@ -58,7 +58,9 @@ const createJestConfig = (host: Tree) => { host.create( 'jest.preset.js', ` - module.exports = { preset: '@nrwl/jest/preset' }` + const nxPreset = require('@nrwl/jest/preset'); + + module.exports = { ...nxPreset }` ); } }; diff --git a/packages/jest/src/schematics/jest-project/jest-project.spec.ts b/packages/jest/src/schematics/jest-project/jest-project.spec.ts index 8a8a2ee2329aa..f3784fdd10359 100644 --- a/packages/jest/src/schematics/jest-project/jest-project.spec.ts +++ b/packages/jest/src/schematics/jest-project/jest-project.spec.ts @@ -89,7 +89,7 @@ describe('jestProject', () => { tags.stripIndents`${resultTree.readContent('libs/lib1/jest.config.js')}` ).toBe(tags.stripIndents`module.exports = { displayName: 'lib1', - preset: '@nrwl/jest/preset', + preset: '../../jest.preset.js', globals: { 'ts-jest': { tsConfig: '/tsconfig.spec.json', diff --git a/packages/nest/src/schematics/library/library.spec.ts b/packages/nest/src/schematics/library/library.spec.ts index 8dc45934e5fee..70548f046e715 100644 --- a/packages/nest/src/schematics/library/library.spec.ts +++ b/packages/nest/src/schematics/library/library.spec.ts @@ -416,7 +416,7 @@ describe('lib', () => { expect(stripIndents`${jestConfig}`) .toEqual(stripIndents`module.exports = { displayName: 'my-lib', - preset: '../../jest.config.js', + preset: '../../jest.preset.js', globals: { 'ts-jest': { tsConfig: '/tsconfig.spec.json', diff --git a/packages/node/src/schematics/application/application.spec.ts b/packages/node/src/schematics/application/application.spec.ts index dcb667288a520..616cd51a547e0 100644 --- a/packages/node/src/schematics/application/application.spec.ts +++ b/packages/node/src/schematics/application/application.spec.ts @@ -281,8 +281,8 @@ describe('app', () => { expect(tree.readContent(`apps/my-node-app/jest.config.js`)) .toMatchInlineSnapshot(` "module.exports = { - name: 'my-node-app', - preset: '../../jest.config.js', + displayName: 'my-node-app', + preset: '../../jest.preset.js', transform: { '^.+\\\\\\\\.[tj]s$': [ 'babel-jest', { cwd: __dirname, configFile: './babel-jest.config.json' }] diff --git a/packages/node/src/schematics/library/library.spec.ts b/packages/node/src/schematics/library/library.spec.ts index a4fbb32ac7d2d..8f399054f1f9a 100644 --- a/packages/node/src/schematics/library/library.spec.ts +++ b/packages/node/src/schematics/library/library.spec.ts @@ -382,8 +382,8 @@ describe('lib', () => { expect(tree.readContent(`libs/my-lib/jest.config.js`)) .toMatchInlineSnapshot(` "module.exports = { - name: 'my-lib', - preset: '../../jest.config.js', + displayName: 'my-lib', + preset: '../../jest.preset.js', transform: { '^.+\\\\\\\\.[tj]sx?$': [ 'babel-jest', diff --git a/packages/web/src/schematics/application/application.spec.ts b/packages/web/src/schematics/application/application.spec.ts index 57cf899e8547d..78ebe720e19c1 100644 --- a/packages/web/src/schematics/application/application.spec.ts +++ b/packages/web/src/schematics/application/application.spec.ts @@ -347,8 +347,8 @@ describe('app', () => { expect(tree.readContent(`apps/my-app/jest.config.js`)) .toMatchInlineSnapshot(` "module.exports = { - name: 'my-app', - preset: '../../jest.config.js', + displayName: 'my-app', + preset: '../../jest.preset.js', setupFilesAfterEnv: ['/src/test-setup.ts'], transform: { '^.+\\\\\\\\.[tj]s$': [ From 5e2c7c152df93095dde458b5b08598b5d8cc7f46 Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Mon, 21 Sep 2020 14:02:22 -0400 Subject: [PATCH 3/4] chore(testing): fix node e2e --- e2e/node/src/node.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/e2e/node/src/node.test.ts b/e2e/node/src/node.test.ts index 30b84abc08154..a3678cffa95e8 100644 --- a/e2e/node/src/node.test.ts +++ b/e2e/node/src/node.test.ts @@ -355,8 +355,8 @@ forEachCli((currentCLIName) => { expect(stripIndents`${jestConfigContent}`).toEqual( stripIndents`module.exports = { - name: '${nestlib}', - preset: '../../jest.config.js', + displayName: '${nestlib}', + preset: '../../jest.preset.js', globals: { 'ts-jest': { tsConfig: '/tsconfig.spec.json', From 271ee379ee37722c29adcc56aeae5d542da56ebe Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Tue, 22 Sep 2020 18:24:47 -0400 Subject: [PATCH 4/4] chore(testing): review comment changes --- packages/jest/src/builders/jest/jest.impl.spec.ts | 8 -------- packages/jest/src/builders/jest/jest.impl.ts | 1 - packages/jest/src/schematics/init/init.spec.ts | 1 - 3 files changed, 10 deletions(-) diff --git a/packages/jest/src/builders/jest/jest.impl.spec.ts b/packages/jest/src/builders/jest/jest.impl.spec.ts index 6d5d596d947ed..da50800f42d6b 100644 --- a/packages/jest/src/builders/jest/jest.impl.spec.ts +++ b/packages/jest/src/builders/jest/jest.impl.spec.ts @@ -63,7 +63,6 @@ describe('Jest Builder', () => { _: [], testPathPattern: [], watch: false, - projects: ['/root/jest.config.js'], }, ['/root/jest.config.js'] ); @@ -104,7 +103,6 @@ describe('Jest Builder', () => { coverageReporters: ['test'], coverageDirectory: '/test/path', watch: false, - projects: ['/root/jest.config.js'], }, ['/root/jest.config.js'] ); @@ -135,7 +133,6 @@ describe('Jest Builder', () => { testNamePattern: 'should load', testPathPattern: [], watch: false, - projects: ['/root/jest.config.js'], }, ['/root/jest.config.js'] ); @@ -204,7 +201,6 @@ describe('Jest Builder', () => { watch: false, watchAll: false, testLocationInResults: true, - projects: ['/root/jest.config.js'], }, ['/root/jest.config.js'] ); @@ -226,7 +222,6 @@ describe('Jest Builder', () => { _: [], maxWorkers: '50%', testPathPattern: [], - projects: ['/root/jest.config.js'], }, ['/root/jest.config.js'] ); @@ -250,7 +245,6 @@ describe('Jest Builder', () => { setupFilesAfterEnv: ['/root/test-setup.ts'], testPathPattern: [], watch: false, - projects: ['/root/jest.config.js'], }, ['/root/jest.config.js'] ); @@ -299,7 +293,6 @@ describe('Jest Builder', () => { setupFilesAfterEnv: ['/root/test-setup.ts'], testPathPattern: [], watch: false, - projects: ['/root/jest.config.js'], }, ['/root/jest.config.js'] ); @@ -337,7 +330,6 @@ describe('Jest Builder', () => { _: [], testPathPattern: [], watch: false, - projects: ['/root/jest.config.js'], }, ['/root/jest.config.js'] ); diff --git a/packages/jest/src/builders/jest/jest.impl.ts b/packages/jest/src/builders/jest/jest.impl.ts index 0019a1a243cd6..2cdd6e2eac887 100644 --- a/packages/jest/src/builders/jest/jest.impl.ts +++ b/packages/jest/src/builders/jest/jest.impl.ts @@ -69,7 +69,6 @@ function run( useStderr: options.useStderr, watch: options.watch, watchAll: options.watchAll, - projects: [options.jestConfig], }; // for backwards compatibility diff --git a/packages/jest/src/schematics/init/init.spec.ts b/packages/jest/src/schematics/init/init.spec.ts index 849626a1d5f91..e8291d5e67ff6 100644 --- a/packages/jest/src/schematics/init/init.spec.ts +++ b/packages/jest/src/schematics/init/init.spec.ts @@ -1,4 +1,3 @@ -import { tags } from '@angular-devkit/core'; import { Tree } from '@angular-devkit/schematics'; import { readJsonInTree } from '@nrwl/workspace'; import { createEmptyWorkspace } from '@nrwl/workspace/testing';