Skip to content

Commit

Permalink
Remove hasDeprecationWarnings from watch function, move it up one…
Browse files Browse the repository at this point in the history
… level.
  • Loading branch information
cpojer committed Apr 12, 2017
1 parent 60ff35f commit 1f900d0
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 73 deletions.
8 changes: 5 additions & 3 deletions packages/jest-cli/src/TestRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,11 @@ class TestRunner {
this.addReporter(new CoverageReporter());
}

this.addReporter(new SummaryReporter({
getTestSummary: this._options.getTestSummary,
}));
this.addReporter(
new SummaryReporter({
getTestSummary: this._options.getTestSummary,
}),
);
if (config.notify) {
this.addReporter(new NotifyReporter(this._startRun));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ describe('Watch mode flows', () => {
let argv;
let context;
let config;
let hasDeprecationWarnings;
let stdin;

beforeEach(() => {
Expand All @@ -94,13 +93,12 @@ describe('Watch mode flows', () => {
argv = {};
context = {};
config = {};
hasDeprecationWarnings = false;
stdin = new MockStdin();
});

it('Pressing "P" enters pattern mode', () => {
config = {rootDir: ''};
watch(config, pipe, argv, hasteMap, context, hasDeprecationWarnings, stdin);
watch(config, pipe, argv, hasteMap, context, stdin);

// Write a enter pattern mode
stdin.emit(KEYS.P);
Expand Down Expand Up @@ -137,7 +135,7 @@ describe('Watch mode flows', () => {

it('Results in pattern mode get truncated appropriately', () => {
config = {rootDir: ''};
watch(config, pipe, argv, hasteMap, context, hasDeprecationWarnings, stdin);
watch(config, pipe, argv, hasteMap, context, stdin);

stdin.emit(KEYS.P);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ describe('Watch mode flows', () => {
let argv;
let context;
let config;
let hasDeprecationWarnings;
let stdin;

beforeEach(() => {
Expand All @@ -118,13 +117,12 @@ describe('Watch mode flows', () => {
argv = {};
context = {};
config = {};
hasDeprecationWarnings = false;
stdin = new MockStdin();
});

it('Pressing "T" enters pattern mode', () => {
config = {rootDir: ''};
watch(config, pipe, argv, hasteMap, context, hasDeprecationWarnings, stdin);
watch(config, pipe, argv, hasteMap, context, stdin);

// Write a enter pattern mode
stdin.emit(KEYS.T);
Expand Down Expand Up @@ -161,7 +159,7 @@ describe('Watch mode flows', () => {

it('Results in pattern mode get truncated appropriately', () => {
config = {rootDir: ''};
watch(config, pipe, argv, hasteMap, context, hasDeprecationWarnings, stdin);
watch(config, pipe, argv, hasteMap, context, stdin);

stdin.emit(KEYS.T);

Expand Down
16 changes: 7 additions & 9 deletions packages/jest-cli/src/__tests__/watch-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ describe('Watch mode flows', () => {
let argv;
let context;
let config;
let hasDeprecationWarnings;
let stdin;

beforeEach(() => {
Expand All @@ -50,15 +49,14 @@ describe('Watch mode flows', () => {
argv = {};
context = {};
config = {roots: [], testPathIgnorePatterns: [], testRegex: ''};
hasDeprecationWarnings = false;
stdin = new MockStdin();
});

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

watch(config, pipe, argv, hasteMap, context, hasDeprecationWarnings, stdin);
watch(config, pipe, argv, hasteMap, context, stdin);

expect(runJestMock).toBeCalledWith(
[context],
Expand All @@ -74,7 +72,7 @@ describe('Watch mode flows', () => {
argv.testNamePattern = 'test-*';
config.testNamePattern = 'test-*';

watch(config, pipe, argv, hasteMap, context, hasDeprecationWarnings, stdin);
watch(config, pipe, argv, hasteMap, context, stdin);

expect(runJestMock).toBeCalledWith(
[context],
Expand All @@ -87,7 +85,7 @@ describe('Watch mode flows', () => {
});

it('Runs Jest once by default and shows usage', () => {
watch(config, pipe, argv, hasteMap, context, hasDeprecationWarnings, stdin);
watch(config, pipe, argv, hasteMap, context, stdin);
expect(runJestMock).toBeCalledWith(
[context],
argv,
Expand All @@ -100,7 +98,7 @@ describe('Watch mode flows', () => {
});

it('Pressing "o" runs test in "only changed files" mode', () => {
watch(config, pipe, argv, hasteMap, context, hasDeprecationWarnings, stdin);
watch(config, pipe, argv, hasteMap, context, stdin);
runJestMock.mockReset();

stdin.emit(KEYS.O);
Expand All @@ -114,7 +112,7 @@ describe('Watch mode flows', () => {
});

it('Pressing "a" runs test in "watch all" mode', () => {
watch(config, pipe, argv, hasteMap, context, hasDeprecationWarnings, stdin);
watch(config, pipe, argv, hasteMap, context, stdin);
runJestMock.mockReset();

stdin.emit(KEYS.A);
Expand All @@ -128,14 +126,14 @@ describe('Watch mode flows', () => {
});

it('Pressing "ENTER" reruns the tests', () => {
watch(config, pipe, argv, hasteMap, context, hasDeprecationWarnings, stdin);
watch(config, pipe, argv, hasteMap, context, stdin);
expect(runJestMock).toHaveBeenCalledTimes(1);
stdin.emit(KEYS.ENTER);
expect(runJestMock).toHaveBeenCalledTimes(2);
});

it('Pressing "u" reruns the tests in "update snapshot" mode', () => {
watch(config, pipe, argv, hasteMap, context, hasDeprecationWarnings, stdin);
watch(config, pipe, argv, hasteMap, context, stdin);
runJestMock.mockReset();

stdin.emit(KEYS.U);
Expand Down
24 changes: 13 additions & 11 deletions packages/jest-cli/src/cli/runCLI.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ import type {Config, Path} from 'types/Config';

const Runtime = require('jest-runtime');

const chalk = require('chalk');
const {Console, clearLine} = require('jest-util');
const {createDirectory} = require('jest-util');
const chalk = require('chalk');
const createContext = require('../lib/createContext');
const getMaxWorkers = require('../lib/getMaxWorkers');
const handleDeprecationWarnings = require('../lib/handleDeprecationWarnings');
const logDebugMessages = require('../lib/logDebugMessages');
const preRunMessage = require('../preRunMessage');
const readConfig = require('jest-config').readConfig;
Expand Down Expand Up @@ -53,7 +54,7 @@ module.exports = async (
}

if (argv.watch || argv.watchAll) {
const {config} = configs[0];
const {config, hasDeprecationWarnings} = configs[0];
createDirectory(config.cacheDirectory);
const hasteMapInstance = Runtime.createHasteMap(config, {
console: new Console(pipe, pipe),
Expand All @@ -64,15 +65,16 @@ module.exports = async (

const hasteMap = await hasteMapInstance.build();
const context = createContext(config, hasteMap);
return watch(
config,
pipe,
argv,
hasteMapInstance,
context,
// TODO
configs[0].hasDeprecationWarnings,
);
if (hasDeprecationWarnings) {
try {
await handleDeprecationWarnings(pipe, process.stdin);
return watch(config, pipe, argv, hasteMapInstance, context);
} catch (e) {
process.exit(0);
}
}

return watch(config, pipe, argv, hasteMapInstance, context);
} else {
const contexts = await Promise.all(
configs.map(async ({config}) => {
Expand Down
46 changes: 46 additions & 0 deletions packages/jest-cli/src/lib/handleDeprecationWarnings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* 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 chalk = require('chalk');
const {KEYS} = require('../constants');

module.exports = (
pipe: stream$Writable | tty$WriteStream,
stdin: stream$Readable | tty$ReadStream = process.stdin,
) => {
return new Promise((resolve, reject) => {
if (typeof stdin.setRawMode === 'function') {
const messages = [
chalk.red('There are deprecation warnings.\n'),
chalk.dim(' \u203A Press ') + 'Enter' + chalk.dim(' to continue.'),
chalk.dim(' \u203A Press ') + 'Esc' + chalk.dim(' to exit.'),
];

pipe.write(messages.join('\n'));

// $FlowFixMe
stdin.setRawMode(true);
stdin.resume();
stdin.setEncoding('hex');
stdin.on('data', (key: string) => {
if (key === KEYS.ENTER) {
resolve();
} else if (
[KEYS.ESCAPE, KEYS.CONTROL_C, KEYS.CONTROL_D].indexOf(key) !== -1
) {
reject();
}
});
} else {
resolve();
}
});
};
42 changes: 0 additions & 42 deletions packages/jest-cli/src/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,8 @@ const watch = (
argv: Object,
hasteMap: HasteMap,
context: Context,
hasDeprecationWarnings?: boolean,
stdin?: stream$Readable | tty$ReadStream = process.stdin,
) => {
if (hasDeprecationWarnings) {
return handleDeprecatedWarnings(pipe, stdin)
.then(() => {
watch(config, pipe, argv, hasteMap, context);
})
.catch(() => process.exit(0));
}

setState(argv, argv.watch ? 'watch' : 'watchAll', {
testNamePattern: argv.testNamePattern,
testPathPattern: argv.testPathPattern ||
Expand Down Expand Up @@ -225,39 +216,6 @@ const watch = (
return Promise.resolve();
};

const handleDeprecatedWarnings = (
pipe: stream$Writable | tty$WriteStream,
stdin: stream$Readable | tty$ReadStream = process.stdin,
) => {
return new Promise((resolve, reject) => {
if (typeof stdin.setRawMode === 'function') {
const messages = [
chalk.red('There are deprecation warnings.\n'),
chalk.dim(' \u203A Press ') + 'Enter' + chalk.dim(' to continue.'),
chalk.dim(' \u203A Press ') + 'Esc' + chalk.dim(' to exit.'),
];

pipe.write(messages.join('\n'));

// $FlowFixMe
stdin.setRawMode(true);
stdin.resume();
stdin.setEncoding('hex');
stdin.on('data', (key: string) => {
if (key === KEYS.ENTER) {
resolve();
} else if (
[KEYS.ESCAPE, KEYS.CONTROL_C, KEYS.CONTROL_D].indexOf(key) !== -1
) {
reject();
}
});
} else {
resolve();
}
});
};

const usage = (argv, snapshotFailure, delimiter = '\n') => {
/* eslint-disable max-len */
const messages = [
Expand Down

0 comments on commit 1f900d0

Please sign in to comment.