Skip to content

Commit

Permalink
fix(testing): disable collectCoverage by default (#1586)
Browse files Browse the repository at this point in the history
* fix(testing): disable collectCoverage by default

Disabling collectCoverage by default as this causes issues with latest version of Jest

Temp fix #1531

* Adding back code coverage reporters

* feat(nx): add migration to update collectCoverage to false in jest.config.js

Added new migration script to update jest.config.js collectCoverage property to false

* refactor(nx): move migrations to @nrwl/jest

Move the collectCoverage migration to the jest package

* chore: refactor to use AST utils
  • Loading branch information
wesleygrimes authored and FrozenPandaz committed Jul 22, 2019
1 parent 98e5bbc commit e29f37e
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 4 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@
"node_modules",
"<rootDir>/build/packages/angular/spec"
],
"collectCoverage": true,
"coverageReporters": [
"html"
],
Expand Down
8 changes: 7 additions & 1 deletion packages/jest/migrations.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
{
"schematics": {}
"schematics": {
"update-8.3.0": {
"version": "8.3.0",
"description": "Update jest.config.js",
"factory": "./src/migrations/update-8-3-0/update-8-3-0"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
testMatch: ['**/+(*.)+(spec|test).+(ts|js)?(x)'],
transform: {
'^.+\\.(ts|js|html)$': 'ts-jest'
},
resolver: '@nrwl/jest/plugins/resolver',
moduleFileExtensions: ['ts', 'js', 'html'],
collectCoverage: true,
coverageReporters: ['html']
};
35 changes: 35 additions & 0 deletions packages/jest/src/migrations/update-8-3-0/update-8-3-0.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Tree, mergeWith, url } from '@angular-devkit/schematics';
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';

import * as path from 'path';
import { readFileSync } from 'fs';

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

beforeEach(async () => {
initialTree = Tree.empty();
schematicRunner = new SchematicTestRunner(
'@nrwl/jest',
path.join(__dirname, '../../../migrations.json')
);
});

it('should collectCoverage to false in the jest.config.js', async () => {
initialTree.create(
'jest.config.js',
readFileSync(
path.join(__dirname, './test-files/jest.config.js')
).toString()
);

const result = await schematicRunner
.runSchematicAsync('update-8.3.0', {}, initialTree)
.toPromise();

const updatedJestConfigFile = result.readContent('jest.config.js');

expect(updatedJestConfigFile).not.toContain('collectCoverage: true');
});
});
40 changes: 40 additions & 0 deletions packages/jest/src/migrations/update-8-3-0/update-8-3-0.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Rule, Tree } from '@angular-devkit/schematics';
import { insert } from '@nrwl/workspace';
import * as ts from 'typescript';
import {
getSourceNodes,
RemoveChange
} from '@nrwl/workspace/src/utils/ast-utils';

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: RemoveChange[] = [];

getSourceNodes(sourceFile).forEach(node => {
if (
ts.isPropertyAssignment(node) &&
ts.isIdentifier(node.name) &&
node.name.text === 'collectCoverage'
) {
changes.push(
new RemoveChange(
'jest.config.js',
node.getStart(sourceFile),
node.getFullText(sourceFile)
)
);
}
});
insert(host, 'jest.config.js', changes);
}
}

export default function(): Rule {
return updateJestConfig;
}
1 change: 0 additions & 1 deletion packages/jest/src/schematics/ng-add/files/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ module.exports = {
},
resolver: '@nrwl/jest/plugins/resolver',
moduleFileExtensions: ['ts', 'js', 'html'],
collectCoverage: true,
coverageReporters: ['html']
};
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ describe('Update 7.7.0', () => {
},
resolver: '@nrwl/builders/plugins/jest/resolver',
moduleFileExtensions: ['ts', 'js', 'html'],
collectCoverage: true,
coverageReporters: ['html']
};`
);
Expand Down

0 comments on commit e29f37e

Please sign in to comment.