Skip to content

Commit

Permalink
feat(testing): update to jest-preset-angular v8.0.0 part 1
Browse files Browse the repository at this point in the history
Updates to jest-preset-angular to v8.0.0, includes migrations to fix any existing projects affected
by the jest-preset-angular update.

closes nrwl#1979
  • Loading branch information
mentzerj authored and FrozenPandaz committed Jan 31, 2020
1 parent b6e6b3f commit c856bba
Show file tree
Hide file tree
Showing 14 changed files with 404 additions and 258 deletions.
8 changes: 4 additions & 4 deletions .prettierrc
@@ -1,4 +1,4 @@
{
"singleQuote": true,
"endOfLine": "lf"
}
{
"singleQuote": true,
"endOfLine": "lf"
}
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -147,8 +147,8 @@
"jasmine-spec-reporter": "~4.2.1",
"jest": "^24.1.0",
"jest-jasmine2": "^24.1.0",
"jest-preset-angular": "7.0.0",
"jest-worker": "24.9.0",
"jest-preset-angular": "8.0.0",
"jest-worker": "^25.1.0",
"karma": "~4.0.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.1",
Expand Down
11 changes: 11 additions & 0 deletions packages/angular/src/schematics/application/application.spec.ts
Expand Up @@ -91,6 +91,17 @@ describe('app', () => {
expect(tsconfigE2E.extends).toEqual('./tsconfig.json');
});

it('should setup jest with serializers', async () => {
const tree = await runSchematic('app', { name: 'myApp' }, appTree);

expect(tree.readContent('apps/my-app/jest.config.js')).toContain(
`'jest-preset-angular/build/AngularSnapshotSerializer.js'`
);
expect(tree.readContent('apps/my-app/jest.config.js')).toContain(
`'jest-preset-angular/build/HTMLCommentSerializer.js'`
);
});

it('should default the prefix to npmScope', async () => {
const noPrefix = await runSchematic(
'app',
Expand Down
2 changes: 1 addition & 1 deletion packages/angular/src/utils/versions.ts
Expand Up @@ -4,4 +4,4 @@ export const angularDevkitVersion = '0.900.0-rc.12';
export const angularJsVersion = '1.6.6';
export const ngrxVersion = '8.5.0';
export const rxjsVersion = '~6.5.0';
export const jestPresetAngularVersion = '7.0.0';
export const jestPresetAngularVersion = '8.0.0';
5 changes: 5 additions & 0 deletions packages/jest/migrations.json
Expand Up @@ -9,6 +9,11 @@
"version": "8.7.0",
"description": "Update Jest testPathPattern option",
"factory": "./src/migrations/update-8-7-0/update-8-7-0"
},
"update-8.8.0": {
"version": "8.8.0",
"description": "Upgrades jest-preset-angular and runs migrations for breaking changes",
"factory": "./src/migrations/update-8-8-0/update-8-8-0"
}
}
}
18 changes: 12 additions & 6 deletions packages/jest/src/builders/jest/jest.impl.spec.ts
Expand Up @@ -57,7 +57,8 @@ describe('Jest Builder', () => {
tsConfig: '/root/tsconfig.test.json',
stringifyContentPathRegex: '\\.(html|svg)$',
astTransformers: [
'jest-preset-angular/InlineHtmlStripStylesTransformer'
'jest-preset-angular/build/InlineFilesTransformer',
'jest-preset-angular/build/StripStylesTransformer'
]
}
}),
Expand Down Expand Up @@ -99,7 +100,8 @@ describe('Jest Builder', () => {
tsConfig: '/root/tsconfig.test.json',
stringifyContentPathRegex: '\\.(html|svg)$',
astTransformers: [
'jest-preset-angular/InlineHtmlStripStylesTransformer'
'jest-preset-angular/build/InlineFilesTransformer',
'jest-preset-angular/build/StripStylesTransformer'
]
}
}),
Expand Down Expand Up @@ -142,7 +144,8 @@ describe('Jest Builder', () => {
tsConfig: '/root/tsconfig.test.json',
stringifyContentPathRegex: '\\.(html|svg)$',
astTransformers: [
'jest-preset-angular/InlineHtmlStripStylesTransformer'
'jest-preset-angular/build/InlineFilesTransformer',
'jest-preset-angular/build/StripStylesTransformer'
]
}
}),
Expand Down Expand Up @@ -199,7 +202,8 @@ describe('Jest Builder', () => {
tsConfig: '/root/tsconfig.test.json',
stringifyContentPathRegex: '\\.(html|svg)$',
astTransformers: [
'jest-preset-angular/InlineHtmlStripStylesTransformer'
'jest-preset-angular/build/InlineFilesTransformer',
'jest-preset-angular/build/StripStylesTransformer'
]
}
}),
Expand Down Expand Up @@ -251,7 +255,8 @@ describe('Jest Builder', () => {
tsConfig: '/root/tsconfig.test.json',
stringifyContentPathRegex: '\\.(html|svg)$',
astTransformers: [
'jest-preset-angular/InlineHtmlStripStylesTransformer'
'jest-preset-angular/build/InlineFilesTransformer',
'jest-preset-angular/build/StripStylesTransformer'
]
}
}),
Expand Down Expand Up @@ -307,7 +312,8 @@ describe('Jest Builder', () => {
tsConfig: '/root/tsconfig.test.json',
stringifyContentPathRegex: '\\.(html|svg)$',
astTransformers: [
'jest-preset-angular/InlineHtmlStripStylesTransformer'
'jest-preset-angular/build/InlineFilesTransformer',
'jest-preset-angular/build/StripStylesTransformer'
]
}
}),
Expand Down
5 changes: 4 additions & 1 deletion packages/jest/src/builders/jest/jest.impl.ts
Expand Up @@ -72,7 +72,10 @@ function run(
require.resolve('jest-preset-angular');
Object.assign(tsJestConfig, {
stringifyContentPathRegex: '\\.(html|svg)$',
astTransformers: ['jest-preset-angular/InlineHtmlStripStylesTransformer']
astTransformers: [
'jest-preset-angular/build/InlineFilesTransformer',
'jest-preset-angular/build/StripStylesTransformer'
]
});
} catch (e) {}

Expand Down
155 changes: 155 additions & 0 deletions packages/jest/src/migrations/update-8-8-0/update-8-8-0.spec.ts
@@ -0,0 +1,155 @@
import { Tree } from '@angular-devkit/schematics';
import { readJsonInTree } from '@nrwl/workspace/src/utils/ast-utils';
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
import * as path from 'path';
import { createEmptyWorkspace } from '@nrwl/workspace/testing';
import { serializeJson } from '@nrwl/workspace';

describe.only('Update 8.8.0', () => {
let initialTree: Tree;
let schematicRunner: SchematicTestRunner;

beforeEach(async () => {
initialTree = createEmptyWorkspace(Tree.empty());

schematicRunner = new SchematicTestRunner(
'@nrwl/jest',
path.join(__dirname, '../../../migrations.json')
);

initialTree.overwrite(
'package.json',
serializeJson({ devDependencies: { 'jest-preset-angular': '7.0.0' } })
);

initialTree.overwrite(
'workspace.json',
serializeJson({
projects: {
'angular-one': {
root: 'apps/angular-one/',
architect: {
test: {
builder: '@nrwl/jest:jest',
options: {
jestConfig: 'apps/angular-one/jest.config.js'
}
}
}
},
'angular-two': {
root: 'apps/angular-two/',
architect: {
test: {
builder: '@nrwl/jest:jest',
options: {
jestConfig: 'apps/angular-two/jest.config.js'
}
}
}
},
'non-angular-one': {
root: 'apps/non-angular-one/',
architect: {
test: {
builder: '@nrwl/jest:jest',
options: {
jestConfig: 'apps/non-angular-one/jest.config.js'
}
}
}
}
}
})
);

initialTree.create(
'apps/angular-one/jest.config.js',
`module.exports = {
name: 'angular-one',
preset: '../../jest.config.js',
coverageDirectory:
'../../coverage/apps/angular-one',
snapshotSerializers: [
'jest-preset-angular/AngularSnapshotSerializer.js',
'jest-preset-angular/HTMLCommentSerializer.js'
]
};`
);

initialTree.create(
'apps/angular-two/jest.config.js',
`module.exports = {
name: 'angular-two',
preset: '../../jest.config.js',
coverageDirectory:
'../../coverage/apps/angular-two',
snapshotSerializers: [
'jest-preset-angular/AngularSnapshotSerializer.js',
'jest-preset-angular/HTMLCommentSerializer.js'
]
};`
);

initialTree.create(
'apps/non-angular-one/jest.config.js',
`module.exports = {
name: 'non-angular-one',
preset: '../../jest.config.js',
coverageDirectory:
'../../coverage/apps/non-angular-one',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html']
};`
);
});

it('should update jest-preset-angular to 8.0.0', async () => {
const result = await schematicRunner
.runSchematicAsync('update-8.8.0', {}, initialTree)
.toPromise();

const { devDependencies } = readJsonInTree(result, 'package.json');
expect(devDependencies['jest-preset-angular']).toEqual('8.0.0');
});

it(`it should add '/build' into jest-preset-angular snapshotSerializers in any jest.config.js where it exists`, async () => {
const result = await schematicRunner
.runSchematicAsync('update-8.8.0', {}, initialTree)
.toPromise();

const updateJestAngularOne = result.readContent(
'apps/angular-one/jest.config.js'
);
const updateJestAngularTwo = result.readContent(
'apps/angular-two/jest.config.js'
);
const updateJestNonAngularOne = result.readContent(
'apps/non-angular-one/jest.config.js'
);

expect(updateJestAngularOne).not.toContain(
'jest-preset-angular/AngularSnapshotSerializer.js'
);
expect(updateJestAngularOne).not.toContain(
'jest-preset-angular/HTMLCommentSerializer.js'
);
expect(updateJestAngularTwo).not.toContain(
'jest-preset-angular/AngularSnapshotSerializer.js'
);
expect(updateJestAngularTwo).not.toContain(
'jest-preset-angular/HTMLCommentSerializer.js'
);
expect(updateJestAngularOne).toContain(
'jest-preset-angular/build/AngularSnapshotSerializer.js'
);
expect(updateJestAngularOne).toContain(
'jest-preset-angular/build/HTMLCommentSerializer.js'
);
expect(updateJestAngularTwo).toContain(
'jest-preset-angular/build/AngularSnapshotSerializer.js'
);
expect(updateJestAngularTwo).toContain(
'jest-preset-angular/build/HTMLCommentSerializer.js'
);
});
});

0 comments on commit c856bba

Please sign in to comment.