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

feat(testing): update to jest-preset-angular v8.0.0 #2401

Merged
merged 3 commits into from Feb 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions .prettierrc
@@ -1,4 +1,4 @@
{
"singleQuote": true,
"endOfLine": "lf"
}
{
"singleQuote": true,
"endOfLine": "lf"
}
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Expand Up @@ -80,7 +80,7 @@ While developing you may want to try out the changes you have made. The easier w
yarn create-playground
```

You can then go to `tmp/nx` (this is set up to use Nx CLI) or `tmp/angular` (this is set up to use Angular CLI), where you will find an empty workspace with your changes in it, something this that:
You can then go to `tmp/nx` (this is set up to use Nx CLI) or `tmp/angular` (this is set up to use Angular CLI), where you will find an empty workspace with your changes in it.:

```bash
yarn create-playground
Expand Down
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
117 changes: 73 additions & 44 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 Expand Up @@ -350,53 +361,71 @@ describe('app', () => {
});
});

describe('--unit-test-runner karma', () => {
it('should generate a karma config', async () => {
const tree = await runSchematic(
'app',
{ name: 'myApp', unitTestRunner: 'karma' },
appTree
);
describe('--unit-test-runner', () => {
describe('default (jest)', () => {
it('should generate jest.config.js with serializers', async () => {
const tree = await runSchematic('app', { name: 'myApp' }, appTree);

expect(tree.exists('apps/my-app/tsconfig.spec.json')).toBeTruthy();
expect(tree.exists('apps/my-app/karma.conf.js')).toBeTruthy();
const workspaceJson = readJsonInTree(tree, 'workspace.json');
expect(workspaceJson.projects['my-app'].architect.test.builder).toEqual(
'@angular-devkit/build-angular:karma'
);
expect(
workspaceJson.projects['my-app'].architect.lint.options.tsConfig
).toEqual([
'apps/my-app/tsconfig.app.json',
'apps/my-app/tsconfig.spec.json'
]);
const tsconfigAppJson = readJsonInTree(
tree,
'apps/my-app/tsconfig.app.json'
);
expect(tsconfigAppJson.compilerOptions.outDir).toEqual(
'../../dist/out-tsc'
);
expect(tree.readContent('apps/my-app/jest.config.js')).toContain(
`'jest-preset-angular/build/AngularNoNgAttributesSnapshotSerializer.js'`
);
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'`
);
});
});
});

describe('--unit-test-runner none', () => {
it('should not generate test configuration', async () => {
const tree = await runSchematic(
'app',
{ name: 'myApp', unitTestRunner: 'none' },
appTree
);
expect(tree.exists('apps/my-app/src/test-setup.ts')).toBeFalsy();
expect(tree.exists('apps/my-app/src/test.ts')).toBeFalsy();
expect(tree.exists('apps/my-app/tsconfig.spec.json')).toBeFalsy();
expect(tree.exists('apps/my-app/jest.config.js')).toBeFalsy();
expect(tree.exists('apps/my-app/karma.config.js')).toBeFalsy();
const workspaceJson = readJsonInTree(tree, 'workspace.json');
expect(workspaceJson.projects['my-app'].architect.test).toBeUndefined();
expect(
workspaceJson.projects['my-app'].architect.lint.options.tsConfig
).toEqual(['apps/my-app/tsconfig.app.json']);
describe('karma', () => {
it('should generate a karma config', async () => {
const tree = await runSchematic(
'app',
{ name: 'myApp', unitTestRunner: 'karma' },
appTree
);

expect(tree.exists('apps/my-app/tsconfig.spec.json')).toBeTruthy();
expect(tree.exists('apps/my-app/karma.conf.js')).toBeTruthy();
const workspaceJson = readJsonInTree(tree, 'workspace.json');
expect(workspaceJson.projects['my-app'].architect.test.builder).toEqual(
'@angular-devkit/build-angular:karma'
);
expect(
workspaceJson.projects['my-app'].architect.lint.options.tsConfig
).toEqual([
'apps/my-app/tsconfig.app.json',
'apps/my-app/tsconfig.spec.json'
]);
const tsconfigAppJson = readJsonInTree(
tree,
'apps/my-app/tsconfig.app.json'
);
expect(tsconfigAppJson.compilerOptions.outDir).toEqual(
'../../dist/out-tsc'
);
});
});

describe('none', () => {
it('should not generate test configuration', async () => {
const tree = await runSchematic(
'app',
{ name: 'myApp', unitTestRunner: 'none' },
appTree
);
expect(tree.exists('apps/my-app/src/test-setup.ts')).toBeFalsy();
expect(tree.exists('apps/my-app/src/test.ts')).toBeFalsy();
expect(tree.exists('apps/my-app/tsconfig.spec.json')).toBeFalsy();
expect(tree.exists('apps/my-app/jest.config.js')).toBeFalsy();
expect(tree.exists('apps/my-app/karma.config.js')).toBeFalsy();
const workspaceJson = readJsonInTree(tree, 'workspace.json');
expect(workspaceJson.projects['my-app'].architect.test).toBeUndefined();
expect(
workspaceJson.projects['my-app'].architect.lint.options.tsConfig
).toEqual(['apps/my-app/tsconfig.app.json']);
});
});
});

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';
16 changes: 16 additions & 0 deletions packages/jest/migrations.json
Expand Up @@ -9,6 +9,22 @@
"version": "8.7.0",
"description": "Update Jest testPathPattern option",
"factory": "./src/migrations/update-8-7-0/update-8-7-0"
},
"update-9.0.0": {
"version": "9.0.0-beta.1",
"description": "Upgrades jest-preset-angular to 8.0.0",
"factory": "./src/migrations/update-9-0-0/update-9-0-0"
}
},
"packageJsonUpdates": {
"9.0.0": {
"version": "9.0.0-beta.1",
"packages": {
"jest-preset-angular": {
"version": "8.0.0",
"alwaysAddToPackageJson": false
}
}
}
}
}
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