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 v24 #1135

Merged
merged 3 commits into from
Mar 11, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions docs/api-builders/cypress.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ Type: `string`

Dev server target to run tests against.

### headless
### exit

Default: `false`
Default: `true`

Type: `boolean`

Whether or not to open the Cypress application to run the tests. If set to 'true', will run in headless mode
Whether or not the Cypress Test Runner will stay open after running tests in a spec file

### exit
### headless

Default: `true`
Default: `false`

Type: `boolean`

Whether or not the Cypress Test Runner will stay open after running tests in a spec file
Whether or not to open the Cypress application to run the tests. If set to 'true', will run in headless mode

### record

Expand Down
4 changes: 2 additions & 2 deletions e2e/schematics/jest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('Jest', () => {
const jestResult = await runCLIAsync(`test ${mylib}`);
expect(jestResult.stderr).toContain('Test Suites: 3 passed, 3 total');
done();
}, 10000);
}, 45000);

it('should be able to generate a testable application using jest', async () => {
ensureProject();
Expand All @@ -35,5 +35,5 @@ describe('Jest', () => {
]);
const jestResult = await runCLIAsync(`test ${myapp}`);
expect(jestResult.stderr).toContain('Test Suites: 3 passed, 3 total');
}, 10000);
}, 450000);
});
6 changes: 6 additions & 0 deletions e2e/schematics/web.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ describe('Web Applications', () => {
newLib(`${libName} --framework react`);

const mainPath = `apps/${appName}/src/main.tsx`;
updateFile(
`apps/${appName}/src/app/app.css`,
`body {
display: block;
}`
);
updateFile(mainPath, `import '@proj/${libName}';\n` + readFile(mainPath));

const lintResults = runCLI(`lint ${appName}`);
Expand Down
1 change: 1 addition & 0 deletions e2e/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export function copyMissingPackages(): void {
'jest',
'@types/jest',
'jest-preset-angular',
'identity-obj-proxy',
'karma',
'karma-chrome-launcher',
'karma-coverage-istanbul-reporter',
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"@types/express": "4.16.0",
"@types/jasmine": "~2.8.6",
"@types/jasminewd2": "~2.0.3",
"@types/jest": "^23.3.2",
"@types/jest": "24.0.9",
"@types/node": "~8.9.4",
"@types/prettier": "^1.10.0",
"@types/react": "^16.8.4",
Expand All @@ -74,13 +74,14 @@
"graphviz": "^0.0.8",
"html-webpack-plugin": "^3.2.0",
"husky": "^1.0.0-rc.13",
"identity-obj-proxy": "3.0.0",
"ignore": "^5.0.4",
"jasmine-core": "~2.99.1",
"jasmine-marbles": "0.4.0",
"jasmine-spec-reporter": "~4.2.1",
"jest": "^23.4.0",
"jest-jasmine2": "^23.4.1",
"jest-preset-angular": "^6.0.2",
"jest": "^24.1.0",
"jest-jasmine2": "^24.1.0",
"jest-preset-angular": "7.0.0",
"karma": "~2.0.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.1",
Expand Down
1 change: 1 addition & 0 deletions packages/builders/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@angular-devkit/build-webpack": "~0.13.1",
"copy-webpack-plugin": "4.6.0",
"fork-ts-checker-webpack-plugin": "0.4.9",
"identity-obj-proxy": "3.0.0",
"license-webpack-plugin": "^1.4.0",
"rxjs": "6.3.3",
"ts-loader": "5.3.1",
Expand Down
14 changes: 12 additions & 2 deletions packages/builders/plugins/jest/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { dirname } from 'path';
import { dirname, extname } from 'path';
import * as ts from 'typescript';
import defaultResolver from 'jest-resolve/build/default_resolver';
import defaultResolver from 'jest-resolve/build/defaultResolver';

interface ResolveOptions {
rootDir: string;
Expand Down Expand Up @@ -37,6 +37,16 @@ function getCompilerSetup(rootDir: string) {
let compilerSetup;

module.exports = function(path: string, options: ResolveOptions) {
const ext = extname(path);
if (
ext === '.css' ||
ext === '.scss' ||
ext === '.sass' ||
ext === '.less' ||
ext === '.styl'
) {
return require.resolve('identity-obj-proxy');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this necessary?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

react apps expect styles to be an object when imported? Not totally sure.. @jaysoo has more info.

}
// Try to use the defaultResolver
try {
return defaultResolver(path, options);
Expand Down
36 changes: 27 additions & 9 deletions packages/builders/src/jest/jest.builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,15 @@ describe('Jest Builder', () => {
{
globals: JSON.stringify({
'ts-jest': {
tsConfigFile: path.relative(root, './tsconfig.test.json')
},
__TRANSFORM_HTML__: true
tsConfig: path.join(
'<rootDir>',
path.relative(root, './tsconfig.test.json')
),
stringifyContentPathRegex: '\\.html$',
astTransformers: [
'jest-preset-angular/InlineHtmlStripStylesTransformer'
]
}
}),
watch: false
},
Expand Down Expand Up @@ -74,9 +80,15 @@ describe('Jest Builder', () => {
{
globals: JSON.stringify({
'ts-jest': {
tsConfigFile: path.relative(root, './tsconfig.test.json')
},
__TRANSFORM_HTML__: true
tsConfig: path.join(
'<rootDir>',
path.relative(root, './tsconfig.test.json')
),
stringifyContentPathRegex: '\\.html$',
astTransformers: [
'jest-preset-angular/InlineHtmlStripStylesTransformer'
]
}
}),
watch: false,
coverage: true,
Expand Down Expand Up @@ -113,9 +125,15 @@ describe('Jest Builder', () => {
{
globals: JSON.stringify({
'ts-jest': {
tsConfigFile: path.relative(root, './tsconfig.test.json')
},
__TRANSFORM_HTML__: true
tsConfig: path.join(
'<rootDir>',
path.relative(root, './tsconfig.test.json')
),
stringifyContentPathRegex: '\\.html$',
astTransformers: [
'jest-preset-angular/InlineHtmlStripStylesTransformer'
]
}
}),
setupTestFrameworkScriptFile: path.join(
'<rootDir>',
Expand Down
26 changes: 21 additions & 5 deletions packages/builders/src/jest/jest.builder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {
Builder,
BuildEvent,
BuilderConfiguration
BuilderConfiguration,
BuilderContext
} from '@angular-devkit/architect';

import { Observable, from } from 'rxjs';
Expand Down Expand Up @@ -37,6 +38,24 @@ export default class JestBuilder implements Builder<JestBuilderOptions> {
builderConfig: BuilderConfiguration<JestBuilderOptions>
): Observable<BuildEvent> {
const options = builderConfig.options;
const tsJestConfig = {
tsConfig: path.join(
'<rootDir>',
path.relative(builderConfig.root, options.tsConfig)
)
};

// TODO: This is hacky, We should probably just configure it in the user's workspace
// If jest-preset-angular is installed, apply settings
try {
require.resolve('jest-preset-angular');
Object.assign(tsJestConfig, {
stringifyContentPathRegex: '\\.html$',
astTransformers: [
'jest-preset-angular/InlineHtmlStripStylesTransformer'
]
});
} catch (e) {}
const config: any = {
watch: options.watch,
coverage: options.codeCoverage,
Expand All @@ -48,10 +67,7 @@ export default class JestBuilder implements Builder<JestBuilderOptions> {
silent: options.silent,
runInBand: options.runInBand,
globals: JSON.stringify({
'ts-jest': {
tsConfigFile: path.relative(builderConfig.root, options.tsConfig)
},
__TRANSFORM_HTML__: true
'ts-jest': tsJestConfig
})
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {

import { join } from 'path';
import { readJsonInTree } from '../../src/utils/ast-utils';
import { serializeJson } from '../../src/utils/fileutils';

describe('Update 7.7.0', () => {
let initialTree: Tree;
Expand Down Expand Up @@ -33,4 +34,51 @@ describe('Update 7.7.0', () => {
).toEqual('angular');
});
});

describe('jest update', () => {
beforeEach(() => {
initialTree.create(
'package.json',
serializeJson({
devDependencies: {
jest: '23.10.5',
'jest-preset-angular': '6.0.2'
}
})
);
initialTree.create(
'jest.config.js',
`module.exports = {
testMatch: ['**/+(*.)+(spec|test).+(ts|js)?(x)'],
transform: {
'^.+\\.(ts|js|html)$': 'jest-preset-angular/preprocessor.js'
},
resolver: '@nrwl/builders/plugins/jest/resolver',
moduleFileExtensions: ['ts', 'js', 'html'],
collectCoverage: true,
coverageReporters: ['html']
};`
);
});

it('should update jest dependencies', async () => {
const result = await schematicRunner
.runSchematicAsync('update-7.7.0', {}, initialTree)
.toPromise();

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

it('should update jest.config.js', async () => {
const result = await schematicRunner
.runSchematicAsync('update-7.7.0', {}, initialTree)
.toPromise();

expect(result.readContent('jest.config.js')).not.toContain(
'jest-preset-angular/preprocessor.js'
);
});
});
});
58 changes: 55 additions & 3 deletions packages/schematics/migrations/update-7-7-0/update-7-7-0.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { chain, Rule } from '@angular-devkit/schematics';
import { chain, Rule, Tree } from '@angular-devkit/schematics';

import { updateJsonInTree } from '../../src/utils/ast-utils';
import { updateJsonInTree, insert } from '../../src/utils/ast-utils';
import { formatFiles } from '../../src/utils/rules/format-files';
import { Framework } from '../../src/utils/frameworks';

import * as ts from 'typescript';
import { getSourceNodes } from '@schematics/angular/utility/ast-utils';
import { ReplaceChange } from '@schematics/angular/utility/change';

const setDefaults = updateJsonInTree('angular.json', json => {
if (!json.schematics) {
json.schematics = {};
Expand All @@ -17,6 +21,54 @@ const setDefaults = updateJsonInTree('angular.json', json => {
return json;
});

const updateDependencies = updateJsonInTree('package.json', json => {
json.devDependencies = json.devDependencies || {};
if (json.devDependencies['jest']) {
json.devDependencies['jest'] = '24.1.0';
}
if (json.devDependencies['@types/jest']) {
json.devDependencies['@types/jest'] = '24.0.9';
}
if (json.devDependencies['jest-preset-angular']) {
json.devDependencies['jest-preset-angular'] = '7.0.0';
}
return json;
});

function updateJestConfig(host: Tree) {
if (host.exists('jest.config.js')) {
const contents = host.read('jest.config.js').toString();
const sourceFile = ts.createSourceFile(
'jest.config.js',
contents,
ts.ScriptTarget.Latest
);
const changes: ReplaceChange[] = [];
getSourceNodes(sourceFile).forEach(node => {
if (
ts.isPropertyAssignment(node) &&
ts.isStringLiteral(node.initializer) &&
node.initializer.text === 'jest-preset-angular/preprocessor.js'
) {
changes.push(
new ReplaceChange(
'jest.config.js',
node.initializer.getStart(sourceFile),
node.initializer.getText(sourceFile),
"'ts-jest'"
)
);
}
});
insert(host, 'jest.config.js', changes);
}
}

export default function(): Rule {
return chain([setDefaults, formatFiles()]);
return chain([
setDefaults,
updateDependencies,
updateJestConfig,
formatFiles()
]);
}
Loading