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

Generating coverage for files passed in --findRelatedTests, --onlyChanged and --watch scenarios #5601

Merged
merged 25 commits into from
Mar 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
b45fd58
Jump start using the work from #4246
stipsan Feb 17, 2018
0a39903
checking how it looks without an extra flag
stipsan Feb 17, 2018
055177c
makes more sense to colocate the test here
stipsan Feb 17, 2018
e1472a3
remove mystery files
stipsan Feb 17, 2018
8f073bc
remove references to the flag
stipsan Feb 17, 2018
61937f5
update docs
stipsan Feb 18, 2018
b253210
add test case for --onlyChanged and --coverage
stipsan Feb 18, 2018
812e46f
update snapshot to what it should be
stipsan Feb 18, 2018
5b82b34
apply collectCoverageFrom patterns after finding tests
stipsan Feb 18, 2018
139ed5a
Collect possible coverage patterns while searching for tests
stipsan Feb 18, 2018
63243a5
improve test results
stipsan Feb 18, 2018
a5891db
hi there flow
stipsan Feb 18, 2018
a61c7dd
skip on windows
stipsan Feb 18, 2018
7556f02
will the windows build pass this time?
stipsan Feb 18, 2018
644efaf
fix test on windows
SimenB Feb 18, 2018
26b919b
remove underscore as it's no longer a private api
stipsan Feb 18, 2018
1e46f07
polymorphing
stipsan Feb 18, 2018
cddc4bd
simplify test
stipsan Feb 18, 2018
1b8e4be
increase test coverage in jest-config/normalize
stipsan Feb 22, 2018
0cbaf3a
make the linter happy again
stipsan Feb 22, 2018
0e234b6
should use argv as it's not a config option
stipsan Feb 22, 2018
35f367b
test coverage for run_jest
stipsan Mar 18, 2018
7e92015
update snapshots
stipsan Mar 18, 2018
988081b
remove object spread, replaced with Object.assign
stipsan Mar 18, 2018
7d83ec8
add changelog entry
stipsan Mar 18, 2018
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

### Features

* `[jest-cli]` Add support for using `--coverage` in combination with watch
mode, `--onlyChanged`, `--findRelatedTests` and more
([#5601](https://github.com/facebook/jest/pull/5601))
* `[jest-jasmine2]` Adds error throwing and descriptive errors to `it`/ `test`
for invalid arguments. `[jest-circus]` Adds error throwing and descriptive
errors to `it`/ `test` for invalid arguments
Expand Down
4 changes: 3 additions & 1 deletion docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,9 @@ Alias: `-e`. Use this flag to show full diffs and errors instead of a patch.

Find and run the tests that cover a space separated list of source files that
were passed in as arguments. Useful for pre-commit hook integration to run the
minimal amount of tests necessary.
minimal amount of tests necessary. Can be used together with `--coverage` to
include a test coverage for the source files, no duplicate
`--collectCoverageFrom` arguments needed.

### `--forceExit`

Expand Down
1 change: 0 additions & 1 deletion integration-tests/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ const cleanupStackTrace = (output: string) => {

module.exports = {
cleanup,
cleanupStackTrace,
copyDir,
createEmptyPackage,
extractSummary,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`--findRelatedTests flag generates coverage report for filename 1`] = `
"Test Suites: 2 passed, 2 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites.
"
`;

exports[`--findRelatedTests flag generates coverage report for filename 2`] = `
"

PASS __tests__/a.test.js
PASS __tests__/b.test.js"
`;

exports[`--findRelatedTests flag generates coverage report for filename 3`] = `
"----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
a.js | 100 | 100 | 100 | 100 | |
b.js | 100 | 100 | 100 | 100 | |
----------|----------|----------|----------|----------|-------------------|
"
`;

exports[`--findRelatedTests flag generates coverage report for filename 4`] = `
"Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites related to files matching /a.js/i.
"
`;

exports[`--findRelatedTests flag generates coverage report for filename 5`] = `
"PASS __tests__/a.test.js
✓ a

"
`;

exports[`--findRelatedTests flag generates coverage report for filename 6`] = `
"----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
a.js | 100 | 100 | 100 | 100 | |
----------|----------|----------|----------|----------|-------------------|
"
`;
77 changes: 64 additions & 13 deletions integration-tests/__tests__/find_related_files.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import runJest from '../runJest';
import os from 'os';
import path from 'path';

const {cleanup, writeFiles} = require('../Utils');
const {cleanup, writeFiles, extractSummary} = require('../Utils');

const SkipOnWindows = require('../../scripts/SkipOnWindows');
const DIR = path.resolve(os.tmpdir(), 'find_related_tests_test');
Expand All @@ -23,23 +23,74 @@ SkipOnWindows.suite();
beforeEach(() => cleanup(DIR));
afterEach(() => cleanup(DIR));

test('runs tests related to filename', () => {
writeFiles(DIR, {
'.watchmanconfig': '',
'__tests__/test.test.js': `
describe('--findRelatedTests flag', () => {
test('runs tests related to filename', () => {
writeFiles(DIR, {
'.watchmanconfig': '',
'__tests__/test.test.js': `
const a = require('../a');
test('a', () => {});
`,
'a.js': 'module.exports = {};',
'package.json': JSON.stringify({jest: {testEnvironment: 'node'}}),
'a.js': 'module.exports = {};',
'package.json': JSON.stringify({jest: {testEnvironment: 'node'}}),
});

const {stdout} = runJest(DIR, ['a.js']);
expect(stdout).toMatch('');

const {stderr} = runJest(DIR, ['--findRelatedTests', 'a.js']);
expect(stderr).toMatch('PASS __tests__/test.test.js');

const summaryMsg = 'Ran all test suites related to files matching /a.js/i.';
expect(stderr).toMatch(summaryMsg);
});

const {stdout} = runJest(DIR, ['a.js']);
expect(stdout).toMatch('');
test('generates coverage report for filename', () => {
writeFiles(DIR, {
'.watchmanconfig': '',
'__tests__/a.test.js': `
require('../a');
require('../b');
test('a', () => expect(1).toBe(1));
`,
'__tests__/b.test.js': `
require('../b');
test('b', () => expect(1).toBe(1));
`,
'a.js': 'module.exports = {}',
'b.js': 'module.exports = {}',
'package.json': JSON.stringify({
jest: {collectCoverage: true, testEnvironment: 'node'},
}),
});

let stdout;
let stderr;

const {stderr} = runJest(DIR, ['--findRelatedTests', 'a.js']);
expect(stderr).toMatch('PASS __tests__/test.test.js');
({stdout, stderr} = runJest(DIR));
let summary;
let rest;
({summary, rest} = extractSummary(stderr));
expect(summary).toMatchSnapshot();
expect(
rest
.split('\n')
.map(s => s.trim())
.sort()
.join('\n'),
).toMatchSnapshot();

const summaryMsg = 'Ran all test suites related to files matching /a.js/i.';
expect(stderr).toMatch(summaryMsg);
// both a.js and b.js should be in the coverage
expect(stdout).toMatchSnapshot();

({stdout, stderr} = runJest(DIR, ['--findRelatedTests', 'a.js']));

({summary, rest} = extractSummary(stderr));

expect(summary).toMatchSnapshot();
// should only run a.js
expect(rest).toMatchSnapshot();
// coverage should be collected only for a.js
expect(stdout).toMatchSnapshot();
});
});
45 changes: 45 additions & 0 deletions integration-tests/__tests__/only_changed.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,51 @@ test('run only changed files', () => {
expect(stderr).toMatch(/PASS __tests__(\/|\\)file3.test.js/);
});

test('report test coverage for only changed files', () => {
writeFiles(DIR, {
'__tests__/a.test.js': `
require('../a');
require('../b');
test('a', () => expect(1).toBe(1));
`,
'__tests__/b.test.js': `
require('../b');
test('b', () => expect(1).toBe(1));
`,
'a.js': 'module.exports = {}',
'b.js': 'module.exports = {}',
'package.json': JSON.stringify({
jest: {
collectCoverage: true,
coverageReporters: ['text'],
testEnvironment: 'node',
},
}),
});

run(`${GIT} init`, DIR);
run(`${GIT} add .`, DIR);
run(`${GIT} commit -m "first"`, DIR);

writeFiles(DIR, {
'a.js': 'module.exports = {modified: true}',
});

let stdout;

({stdout} = runJest(DIR));

// both a.js and b.js should be in the coverage
expect(stdout).toMatch('a.js');
expect(stdout).toMatch('b.js');

({stdout} = runJest(DIR, ['-o']));

// coverage should be collected only for a.js
expect(stdout).toMatch('a.js');
expect(stdout).not.toMatch('b.js');
});

test('onlyChanged in config is overwritten by --all or testPathPattern', () => {
writeFiles(DIR, {
'.watchmanconfig': '',
Expand Down
112 changes: 112 additions & 0 deletions packages/jest-cli/src/__tests__/run_jest_with_coverage.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import runJest from '../run_jest';

jest.mock('jest-util');

jest.mock(
'../test_scheduler',
() =>
class {
constructor(globalConfig) {
this._globalConfig = globalConfig;
}

scheduleTests() {
return {_globalConfig: this._globalConfig};
}
},
);

jest.mock(
'../test_sequencer',
() =>
class {
sort(allTests) {
return allTests;
}
cacheResults() {}
},
);

jest.mock(
'../search_source',
() =>
class {
constructor(context) {
this._context = context;
}

async getTestPaths(globalConfig, changedFilesPromise) {
const {files} = await changedFilesPromise;
const paths = files.filter(path => path.match(/__tests__/));

return {
collectCoverageFrom: files.filter(path => !path.match(/__tests__/)),
tests: paths.map(path => ({
context: this._context,
duration: null,
path,
})),
};
}
},
);

const config = {roots: [], testPathIgnorePatterns: [], testRegex: ''};
let globalConfig;
const defaults = {
changedFilesPromise: Promise.resolve({
files: ['foo.js', '__tests__/foo-test.js', 'dont/cover.js'],
}),
contexts: [{config}],
onComplete: runResults => (globalConfig = runResults._globalConfig),
outputStream: {},
startRun: {},
testWatcher: {isInterrupted: () => false},
};

describe('collectCoverageFrom patterns', () => {
it('should apply collectCoverageFrom patterns coming from SearchSource', async () => {
expect.assertions(1);

await runJest(
Object.assign({}, defaults, {
globalConfig: {
rootDir: '',
},
}),
);
expect(globalConfig.collectCoverageFrom).toEqual([
'foo.js',
'dont/cover.js',
]);
});

it('excludes coverage from files outside the global collectCoverageFrom config', async () => {
expect.assertions(1);

await runJest(
Object.assign({}, defaults, {
globalConfig: {
collectCoverageFrom: ['**/dont/*.js'],
rootDir: '',
},
}),
);
expect(globalConfig.collectCoverageFrom).toEqual(['dont/cover.js']);
});

it('respects coveragePathIgnorePatterns', async () => {
expect.assertions(1);

await runJest(
Object.assign({}, defaults, {
globalConfig: {
collectCoverageFrom: ['**/*.js'],
coveragePathIgnorePatterns: ['dont'],
rootDir: '',
},
}),
);
expect(globalConfig.collectCoverageFrom).toEqual(['foo.js']);
});
});
Loading