Skip to content

Commit

Permalink
Remove verbose, watch, watchman, testNamePattern from Project…
Browse files Browse the repository at this point in the history
…Config.
  • Loading branch information
cpojer committed Apr 26, 2017
1 parent 5a8decd commit e665d2d
Show file tree
Hide file tree
Showing 14 changed files with 126 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ exports[`jest --showConfig outputs config info and exits 1`] = `
"noStackTrace": false,
"notify": false,
"rootDir": "/mocked/root/path/jest/integration_tests/verbose_reporter",
"testPathPattern": "",
"testResultsProcessor": null,
"useStderr": false,
"verbose": null,
Expand Down
102 changes: 47 additions & 55 deletions packages/babel-jest/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,50 +24,47 @@ const BABEL_CONFIG_KEY = 'babel';
const PACAKAGE_JSON = 'package.json';
const THIS_FILE = fs.readFileSync(__filename);

const cache = Object.create(null);

let babel;

const getBabelRC = (filename, {useCache}) => {
const paths = [];
let directory = filename;
while (directory !== (directory = path.dirname(directory))) {
if (useCache && cache[directory]) {
break;
}
const createTransformer = (options: any) => {
const cache = Object.create(null);

paths.push(directory);
const configFilePath = path.join(directory, BABELRC_FILENAME);
if (fs.existsSync(configFilePath)) {
cache[directory] = fs.readFileSync(configFilePath, 'utf8');
break;
}
const configJsFilePath = path.join(directory, BABELRC_JS_FILENAME);
if (fs.existsSync(configJsFilePath)) {
// $FlowFixMe
cache[directory] = JSON.stringify(require(configJsFilePath));
break;
}
const packageJsonFilePath = path.join(directory, PACAKAGE_JSON);
if (fs.existsSync(packageJsonFilePath)) {
// $FlowFixMe
const packageJsonFileContents = require(packageJsonFilePath);
if (packageJsonFileContents[BABEL_CONFIG_KEY]) {
cache[directory] = JSON.stringify(
packageJsonFileContents[BABEL_CONFIG_KEY],
);
const getBabelRC = filename => {
const paths = [];
let directory = filename;
while (directory !== (directory = path.dirname(directory))) {
if (cache[directory]) {
break;
}
}
}
paths.forEach(directoryPath => {
cache[directoryPath] = cache[directory];
});

return cache[directory] || '';
};
paths.push(directory);
const configFilePath = path.join(directory, BABELRC_FILENAME);
if (fs.existsSync(configFilePath)) {
cache[directory] = fs.readFileSync(configFilePath, 'utf8');
break;
}
const configJsFilePath = path.join(directory, BABELRC_JS_FILENAME);
if (fs.existsSync(configJsFilePath)) {
// $FlowFixMe
cache[directory] = JSON.stringify(require(configJsFilePath));
break;
}
const packageJsonFilePath = path.join(directory, PACAKAGE_JSON);
if (fs.existsSync(packageJsonFilePath)) {
// $FlowFixMe
const packageJsonFileContents = require(packageJsonFilePath);
if (packageJsonFileContents[BABEL_CONFIG_KEY]) {
cache[directory] = JSON.stringify(
packageJsonFileContents[BABEL_CONFIG_KEY],
);
break;
}
}
}
paths.forEach(directoryPath => (cache[directoryPath] = cache[directory]));
return cache[directory] || '';
};

const createTransformer = (options: any) => {
options = Object.assign({}, options, {
plugins: (options && options.plugins) || [],
presets: ((options && options.presets) || []).concat([jestPreset]),
Expand All @@ -82,24 +79,20 @@ const createTransformer = (options: any) => {
fileData: string,
filename: Path,
configString: string,
{instrument, watch}: TransformOptions,
{instrument}: TransformOptions,
): string {
return (
crypto
.createHash('md5')
.update(THIS_FILE)
.update('\0', 'utf8')
.update(fileData)
.update('\0', 'utf8')
.update(configString)
.update('\0', 'utf8')
// Don't use the in-memory cache in watch mode because the .babelrc
// file may be modified.
.update(getBabelRC(filename, {useCache: !watch}))
.update('\0', 'utf8')
.update(instrument ? 'instrument' : '')
.digest('hex')
);
return crypto
.createHash('md5')
.update(THIS_FILE)
.update('\0', 'utf8')
.update(fileData)
.update('\0', 'utf8')
.update(configString)
.update('\0', 'utf8')
.update(getBabelRC(filename))
.update('\0', 'utf8')
.update(instrument ? 'instrument' : '')
.digest('hex');
},
process(
src: string,
Expand All @@ -116,7 +109,6 @@ const createTransformer = (options: any) => {
}

const theseOptions = Object.assign({filename}, options);

if (transformOptions && transformOptions.instrument) {
theseOptions.auxiliaryCommentBefore = ' istanbul ignore next ';
// Copied from jest-runtime transform.js
Expand Down
9 changes: 4 additions & 5 deletions packages/jest-cli/src/__tests__/watch-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,20 @@ jest.doMock(
);

const watch = require('../watch');
const globalConfig = {
watch: true,
};
afterEach(runJestMock.mockReset);

describe('Watch mode flows', () => {
let pipe;
let hasteMapInstances;
let argv;
let globalConfig;
let contexts;
let stdin;

beforeEach(() => {
const config = {roots: [], testPathIgnorePatterns: [], testRegex: ''};
pipe = {write: jest.fn()};
globalConfig = {watch: true};
hasteMapInstances = [{on: () => {}}];
argv = {};
contexts = [{config}];
Expand All @@ -55,7 +54,7 @@ describe('Watch mode flows', () => {

it('Correctly passing test path pattern', () => {
argv.testPathPattern = 'test-*';
contexts[0].config.testPathPattern = 'test-*';
globalConfig.testPathPattern = 'test-*';

watch(globalConfig, contexts, argv, pipe, hasteMapInstances, stdin);

Expand All @@ -72,7 +71,7 @@ describe('Watch mode flows', () => {

it('Correctly passing test name pattern', () => {
argv.testNamePattern = 'test-*';
contexts[0].config.testNamePattern = 'test-*';
globalConfig.testNamePattern = 'test-*';

watch(globalConfig, contexts, argv, pipe, hasteMapInstances, stdin);

Expand Down
2 changes: 1 addition & 1 deletion packages/jest-cli/src/cli/runCLI.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ module.exports = async (
console: new Console(pipe, pipe),
maxWorkers: getMaxWorkers(argv),
resetCache: !config.cache,
watch: config.watch,
watch: globalConfig.watch,
});
hasteMapInstances[index] = hasteMapInstance;
return createContext(config, await hasteMapInstance.build());
Expand Down
35 changes: 23 additions & 12 deletions packages/jest-cli/src/lib/__tests__/isValidPath-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,39 @@ const config = {
};

it('is valid when it is a file inside roots', () => {
expect(isValidPath(config, path.resolve(rootDir, 'src', 'index.js'))).toBe(
true,
);
expect(
isValidPath(config, path.resolve(rootDir, 'src', 'components', 'Link.js')),
isValidPath({}, config, path.resolve(rootDir, 'src', 'index.js')),
).toBe(true);
expect(
isValidPath(config, path.resolve(rootDir, 'src', 'lib', 'something.js')),
isValidPath(
{},
config,
path.resolve(rootDir, 'src', 'components', 'Link.js'),
),
).toBe(true);
expect(
isValidPath(
{},
config,
path.resolve(rootDir, 'src', 'lib', 'something.js'),
),
).toBe(true);
});

it('is not valid when it is a snapshot file', () => {
expect(
isValidPath(config, path.resolve(rootDir, 'src', 'index.js.snap')),
isValidPath({}, config, path.resolve(rootDir, 'src', 'index.js.snap')),
).toBe(false);
expect(
isValidPath(
{},
config,
path.resolve(rootDir, 'src', 'components', 'Link.js.snap'),
),
).toBe(false);
expect(
isValidPath(
{},
config,
path.resolve(rootDir, 'src', 'lib', 'something.js.snap'),
),
Expand All @@ -54,16 +64,17 @@ it('is not valid when it is a snapshot file', () => {

it('is not valid when it is a file in the coverage dir', () => {
expect(
isValidPath(config, path.resolve(rootDir, 'coverage', 'lib', 'index.js')),
isValidPath(
{},
config,
path.resolve(rootDir, 'coverage', 'lib', 'index.js'),
),
).toBe(false);

const configWithCoverage = Object.assign({}, config, {
coverageDirectory: 'cov-dir',
});

expect(
isValidPath(
configWithCoverage,
{coverageDirectory: 'cov-dir'},
config,
path.resolve(rootDir, 'src', 'cov-dir', 'lib', 'index.js'),
),
).toBe(false);
Expand Down
29 changes: 19 additions & 10 deletions packages/jest-cli/src/reporters/SummaryReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,17 @@ class SummaryReporter extends BaseReporter {
}

onRunStart(
config: GlobalConfig,
globalConfig: GlobalConfig,
aggregatedResults: AggregatedResult,
options: ReporterOnStartOptions,
) {
super.onRunStart(config, aggregatedResults, options);
super.onRunStart(globalConfig, aggregatedResults, options);
this._estimatedTime = options.estimatedTime;
}

onRunComplete(
contexts: Set<Context>,
config: GlobalConfig,
globalConfig: GlobalConfig,
aggregatedResults: AggregatedResult,
) {
const {numTotalTestSuites, testResults, wasInterrupted} = aggregatedResults;
Expand All @@ -99,16 +99,16 @@ class SummaryReporter extends BaseReporter {
// Print a newline if the last test did not fail to line up newlines
// similar to when an error would have been thrown in the test.
if (
!config.verbose &&
!globalConfig.verbose &&
lastResult &&
!lastResult.numFailingTests &&
!lastResult.testExecError
) {
this.log('');
}

this._printSummary(aggregatedResults, config);
this._printSnapshotSummary(aggregatedResults.snapshot, config);
this._printSummary(aggregatedResults, globalConfig);
this._printSnapshotSummary(aggregatedResults.snapshot, globalConfig);

if (numTotalTestSuites) {
const testSummary = wasInterrupted
Expand All @@ -130,7 +130,10 @@ class SummaryReporter extends BaseReporter {
}
}

_printSnapshotSummary(snapshots: SnapshotSummary, config: GlobalConfig) {
_printSnapshotSummary(
snapshots: SnapshotSummary,
globalConfig: GlobalConfig,
) {
if (
snapshots.added ||
snapshots.filesRemoved ||
Expand All @@ -145,7 +148,7 @@ class SummaryReporter extends BaseReporter {
process.env.npm_config_user_agent.match('yarn') !== null
? 'yarn'
: 'npm';
if (config.watch) {
if (globalConfig.watch) {
updateCommand = 'press `u`';
} else if (event) {
updateCommand = `run with \`${client + ' ' + prefix + event} -- -u\``;
Expand Down Expand Up @@ -215,7 +218,10 @@ class SummaryReporter extends BaseReporter {
}
}

_printSummary(aggregatedResults: AggregatedResult, config: GlobalConfig) {
_printSummary(
aggregatedResults: AggregatedResult,
globalConfig: GlobalConfig,
) {
// If there were any failing tests and there was a large number of tests
// executed, re-print the failing results at the end of execution output.
const failedTests = aggregatedResults.numFailedTests;
Expand All @@ -229,7 +235,10 @@ class SummaryReporter extends BaseReporter {
const {failureMessage} = testResult;
if (failureMessage) {
this._write(
getResultHeader(testResult, config) + '\n' + failureMessage + '\n',
getResultHeader(testResult, globalConfig) +
'\n' +
failureMessage +
'\n',
);
}
});
Expand Down
14 changes: 10 additions & 4 deletions packages/jest-cli/src/runJest.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ const getNoTestsFoundMessage = (testRunData, pattern) => {
);
};

const getTestPaths = async (context, pattern, argv, pipe) => {
const getTestPaths = async (globalConfig, context, pattern, argv, pipe) => {
const source = new SearchSource(context);
let data = await source.getTestPaths(pattern);
if (!data.tests.length) {
if (pattern.onlyChanged && data.noSCM) {
if (context.config.watch) {
if (globalConfig.watch) {
// Run all the tests
setState(argv, 'watchAll', {
noSCM: true,
Expand Down Expand Up @@ -158,7 +158,13 @@ const runJest = async (
let allTests = [];
const testRunData = await Promise.all(
contexts.map(async context => {
const matches = await getTestPaths(context, pattern, argv, pipe);
const matches = await getTestPaths(
globalConfig,
context,
pattern,
argv,
pipe,
);
allTests = allTests.concat(matches.tests);
return {context, matches};
}),
Expand All @@ -172,10 +178,10 @@ const runJest = async (
globalConfig.silent !== true &&
globalConfig.verbose !== false
) {
// $FlowFixMe
globalConfig = Object.freeze(
Object.assign({}, globalConfig, {verbose: true}),
);
setConfig(contexts, {verbose: true});
}

// When using more than one context, make all printed paths relative to the
Expand Down
Loading

0 comments on commit e665d2d

Please sign in to comment.