Skip to content

Commit

Permalink
safe --json && --listTests
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronabramov committed Aug 8, 2017
1 parent f7b25b6 commit 53e0a52
Show file tree
Hide file tree
Showing 14 changed files with 335 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ exports[`--showConfig outputs config info and exits 1`] = `
\\"testPathPattern\\": \\"\\",
\\"testResultsProcessor\\": null,
\\"updateSnapshot\\": \\"all\\",
\\"useStderr\\": false,
\\"verbose\\": null,
\\"watch\\": false,
\\"watchman\\": true
Expand Down
3 changes: 1 addition & 2 deletions integration_tests/__tests__/custom_reporters.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,8 @@ describe('Custom Reporters Integration', () => {
]);

expect(status).toBe(0);
expect(stderr.trim()).toBe('');

expect(stdout).toMatchSnapshot();
expect(stderr.trim()).toBe('');
});

test('prints reporter errors', () => {
Expand Down
91 changes: 91 additions & 0 deletions integration_tests/__tests__/json_stdout.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* 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} = require('../utils');
const runJest = require('../runJest');

const DIR = path.resolve(os.tmpdir(), 'json_stdout_test');

skipOnWindows.suite();

beforeEach(() => cleanup(DIR));
afterAll(() => cleanup(DIR));

const reporterCode = `
console.log('at require time');
console.log(console.log.toString());
module.exports = class Reporter {
onRunStart() {
console.log('at runtime');
}
};
`;

test('does not pollute stdout from third party code (or any code)', () => {
writeFiles(DIR, {
'.watchmanconfig': '',
'__tests__/hey.test.js': `test('test', () => {})`,
'custom_reporter.js': reporterCode,
'package.json': JSON.stringify({
jest: {
// Reporter will be required as a regular module in the master process
// before any of the test run, which makes it a perfect place to try
// to break the stdout by console.logging some text.
reporters: ['<rootDir>/custom_reporter.js', 'default'],
},
}),
});

let stdout;
let stderr;
({stdout, stderr} = runJest(DIR, ['--json']));
expect(stdout).not.toMatch('at require time');
expect(stdout).not.toMatch('at runtime');
expect(stderr).toMatch('at require time');
expect(stderr).toMatch('at runtime');
expect(() => JSON.parse(stdout)).not.toThrow();

({stdout, stderr} = runJest(DIR, ['--listTests', '__tests__/hey.test.js']));

expect(stdout).not.toMatch('at require time');
expect(stdout).not.toMatch('at runtime');
expect(stderr).not.toMatch('at require time');
expect(stderr).not.toMatch('at runtime');
expect(() => JSON.parse(stdout)).not.toThrow();
expect(stdout).toMatch('hey.test.js');
});

test('prints to stdout if not in json mode', () => {
writeFiles(DIR, {
'.watchmanconfig': '',
'__tests__/hey.test.js': `test('test', () => {})`,
'custom_reporter.js': reporterCode,
'package.json': JSON.stringify({
jest: {
// Reporter will be required as a regular module in the master process
// before any of the test run, which makes it a perfect place to try
// to break the stdout by console.logging some text.
reporters: ['<rootDir>/custom_reporter.js', 'default'],
},
}),
});

const {status, stdout, stderr} = runJest(DIR);
expect(stderr).not.toMatch('at runtime');
expect(stderr).not.toMatch('at require time');
expect(stdout).toMatch('at require time');
expect(stdout).toMatch('at runtime');
expect(status).toBe(0);
});
7 changes: 6 additions & 1 deletion packages/jest-cli/bin/jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ if (process.env.NODE_ENV == null) {
process.env.NODE_ENV = 'test';
}

require('../build/cli').run();
const stdout = process.stdout;
// Prevent any code from printing to stdout because it will break JSON
// output if we run jest with `--json` or `--listTests` flags.
Object.defineProperty(process, 'stdout', {value: process.stderr});

require('../build/cli').run(undefined, undefined, stdout);
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ exports[`Watch mode flows Pressing "T" enters pattern mode 1`] = `
› should recognize null and undefined
› should not output colors to pipe
› should not output colors to outputStream
› should convert string to a RegExp
Expand Down Expand Up @@ -49,7 +49,7 @@ exports[`Watch mode flows Pressing "T" enters pattern mode 2`] = `
› should recognize null and undefined
› should not output colors to pipe
› should not output colors to outputStream
› should convert string to a RegExp
Expand Down Expand Up @@ -187,7 +187,7 @@ exports[`Watch mode flows Results in pattern mode get truncated appropriately 1`
› should recognize various types
› should not output colors to pipe
› should not output colors to outputStream
› should convert string to a RegExp
Expand Down
68 changes: 55 additions & 13 deletions packages/jest-cli/src/__tests__/watch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ const watch = require('../watch');
afterEach(runJestMock.mockReset);

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

beforeEach(() => {
const config = {roots: [], testPathIgnorePatterns: [], testRegex: ''};
pipe = {write: jest.fn()};
outputStream = {write: jest.fn()};
globalConfig = {watch: true};
hasteMapInstances = [{on: () => {}}];
contexts = [{config}];
Expand All @@ -54,45 +54,69 @@ describe('Watch mode flows', () => {
it('Correctly passing test path pattern', () => {
globalConfig.testPathPattern = 'test-*';

watch(globalConfig, contexts, pipe, hasteMapInstances, stdin);
watch({
contexts,
globalConfig,
hasteMapInstances,
outputStream,
stdin,
});

expect(runJestMock.mock.calls[0][0]).toMatchObject({
contexts,
globalConfig,
onComplete: expect.any(Function),
outputStream: pipe,
outputStream,
testWatcher: new TestWatcher({isWatchMode: true}),
});
});

it('Correctly passing test name pattern', () => {
globalConfig.testNamePattern = 'test-*';

watch(globalConfig, contexts, pipe, hasteMapInstances, stdin);
watch({
contexts,
globalConfig,
hasteMapInstances,
outputStream,
stdin,
});

expect(runJestMock.mock.calls[0][0]).toMatchObject({
contexts,
globalConfig,
onComplete: expect.any(Function),
outputStream: pipe,
outputStream,
testWatcher: new TestWatcher({isWatchMode: true}),
});
});

it('Runs Jest once by default and shows usage', () => {
watch(globalConfig, contexts, pipe, hasteMapInstances, stdin);
watch({
contexts,
globalConfig,
hasteMapInstances,
outputStream,
stdin,
});
expect(runJestMock.mock.calls[0][0]).toMatchObject({
contexts,
globalConfig,
onComplete: expect.any(Function),
outputStream: pipe,
outputStream,
testWatcher: new TestWatcher({isWatchMode: true}),
});
expect(pipe.write.mock.calls.reverse()[0]).toMatchSnapshot();
expect(outputStream.write.mock.calls.reverse()[0]).toMatchSnapshot();
});

it('Pressing "o" runs test in "only changed files" mode', () => {
watch(globalConfig, contexts, pipe, hasteMapInstances, stdin);
watch({
contexts,
globalConfig,
hasteMapInstances,
outputStream,
stdin,
});
runJestMock.mockReset();

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

it('Pressing "a" runs test in "watch all" mode', () => {
watch(globalConfig, contexts, pipe, hasteMapInstances, stdin);
watch({
contexts,
globalConfig,
hasteMapInstances,
outputStream,
stdin,
});
runJestMock.mockReset();

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

it('Pressing "ENTER" reruns the tests', () => {
watch(globalConfig, contexts, pipe, hasteMapInstances, stdin);
watch({
contexts,
globalConfig,
hasteMapInstances,
outputStream,
stdin,
});
expect(runJestMock).toHaveBeenCalledTimes(1);
stdin.emit(KEYS.ENTER);
expect(runJestMock).toHaveBeenCalledTimes(2);
});

it('Pressing "u" reruns the tests in "update snapshot" mode', () => {
watch(globalConfig, contexts, pipe, hasteMapInstances, stdin);
watch({
contexts,
globalConfig,
hasteMapInstances,
outputStream,
stdin,
});
runJestMock.mockReset();

stdin.emit(KEYS.U);
Expand Down
Loading

0 comments on commit 53e0a52

Please sign in to comment.