-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
--generateCoverageForFiles #4246
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`--generateCoverageForFiles 1`] = ` | ||
"Test Suites: 2 passed, 2 total | ||
Tests: 2 passed, 2 total | ||
Snapshots: 0 total | ||
Time: <<REPLACED>> | ||
Ran all test suites. | ||
" | ||
`; | ||
|
||
exports[`--generateCoverageForFiles 2`] = ` | ||
" | ||
|
||
PASS __tests__/a.test.js | ||
PASS __tests__/b.test.js" | ||
`; | ||
|
||
exports[`--generateCoverageForFiles 3`] = ` | ||
"----------|----------|----------|----------|----------|----------------| | ||
File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines | | ||
----------|----------|----------|----------|----------|----------------| | ||
All files | 100 | 100 | 100 | 100 | | | ||
a.js | 100 | 100 | 100 | 100 | | | ||
b.js | 100 | 100 | 100 | 100 | | | ||
----------|----------|----------|----------|----------|----------------| | ||
" | ||
`; | ||
|
||
exports[`--generateCoverageForFiles 4`] = ` | ||
"Test Suites: 1 passed, 1 total | ||
Tests: 1 passed, 1 total | ||
Snapshots: 0 total | ||
Time: <<REPLACED>> | ||
Ran all test suites matching /a.js/i. | ||
" | ||
`; | ||
|
||
exports[`--generateCoverageForFiles 5`] = ` | ||
" PASS __tests__/a.test.js | ||
✓ a | ||
|
||
" | ||
`; | ||
|
||
exports[`--generateCoverageForFiles 6`] = ` | ||
"----------|----------|----------|----------|----------|----------------| | ||
File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines | | ||
----------|----------|----------|----------|----------|----------------| | ||
All files | 100 | 100 | 100 | 100 | | | ||
a.js | 100 | 100 | 100 | 100 | | | ||
----------|----------|----------|----------|----------|----------------| | ||
" | ||
`; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @flow | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const path = require('path'); | ||
const os = require('os'); | ||
const skipOnWindows = require('../../scripts/skip_on_windows'); | ||
const {cleanup, writeFiles, extractSummary} = require('../utils'); | ||
const runJest = require('../runJest'); | ||
|
||
const DIR = path.resolve(os.tmpdir(), 'generate_coverage_for_files_test'); | ||
|
||
skipOnWindows.suite(); | ||
|
||
beforeEach(() => cleanup(DIR)); | ||
afterAll(() => cleanup(DIR)); | ||
|
||
test('--generateCoverageForFiles', () => { | ||
writeFiles(DIR, { | ||
'.watchmanconfig': '', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this really needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think if you have watchman installed on your computer (which we have at fb) it'll complain in stdout if this file is not present, which will in turn make it harder to snapshot the output :( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe it's worth creating a wraper function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm for explicitness here, it's easier not to loose the context |
||
'__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: {testEnvironment: 'node'}}), | ||
}); | ||
|
||
let stdout; | ||
let stderr; | ||
|
||
({stdout, stderr} = runJest(DIR, ['--coverage'])); | ||
let summary; | ||
let rest; | ||
({summary, rest} = extractSummary(stderr)); | ||
expect(summary).toMatchSnapshot(); | ||
expect( | ||
rest.split('\n').map(s => s.trim()).sort().join('\n'), | ||
).toMatchSnapshot(); | ||
// both a.js and b.js should be in the coverage | ||
expect(stdout).toMatchSnapshot(); | ||
|
||
({stdout, stderr} = runJest(DIR, ['--generateCoverageForFiles', '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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. btw, not really needed here, but recently I came up with more descriptive snapshot testing such scenarios: describe(('--generateCoverageForFiles') => {
test('summary', () => expect(summary).toMatchSnapshot())
test('rest', () => expect(rest).toMatchSnapshot())
test('stdout', () => expect(stdout).toMatchSnapshot())
}) This gives more descriptive snapshot names, together with error messages when diffing later (it's easy to loose context in large snapshots, and names are easier to understand than checking which number is for which snap) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wait.. are you running the subprocess outsude of |
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -538,6 +538,21 @@ function normalize(options: InitialOptions, argv: Argv) { | |
.filter(reporter => reporter !== 'text'); | ||
} | ||
|
||
// generateCoverageForFiles is an alias for | ||
// `--findRelatedTests '/rootDir/file1.js' --coverage --collectCoverageFrom 'file1.js'` | ||
// where arguments to `--collectCoverageFrom` should be globs (or relative | ||
// paths to the rootDir) | ||
if (argv.generateCoverageForFiles) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we good that it's only for CLI? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah! i don't think we'd want to put it in a config, since it's a very interactive option :) |
||
newOptions.findRelatedTests = true; | ||
newOptions.collectCoverage = true; | ||
newOptions.collectCoverageFrom = argv._.map(filename => { | ||
filename = _replaceRootDirInPath(options.rootDir, filename); | ||
return path.isAbsolute(filename) | ||
? path.relative(options.rootDir, filename) | ||
: filename; | ||
}); | ||
} | ||
|
||
return { | ||
hasDeprecationWarnings, | ||
options: newOptions, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is incorrect (also broken in master for
--findRelatedTests
)i filed the issue #4247