Skip to content

Commit

Permalink
Fix local circus flaky tests (jestjs#6362)
Browse files Browse the repository at this point in the history
* Fix local circus flaky tests

* cleanup

* calculate hashe from test content

* log babel register path

* Skip on windows
  • Loading branch information
thymikee authored and cpojer committed Jun 2, 2018
1 parent 6f5b2f9 commit 12d91ad
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 24 deletions.
1 change: 1 addition & 0 deletions packages/jest-circus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"stack-utils": "^1.0.1"
},
"devDependencies": {
"execa": "^0.10.0",
"jest-runtime": "^23.1.0"
}
}
16 changes: 13 additions & 3 deletions packages/jest-circus/src/__mocks__/test_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
*/

'use strict';

import {spawnSync} from 'child_process';
// $FlowFixMe - execa is untyped
import {sync as spawnSync} from 'execa';
import fs from 'fs';
import os from 'os';
import path from 'path';
import crypto from 'crypto';
import ConditionalTest from '../../../../scripts/ConditionalTest';

const CIRCUS_PATH = require.resolve('../../build/index');
Expand All @@ -25,7 +26,11 @@ const BABEL_REGISTER_PATH = require.resolve('babel-register');
ConditionalTest.skipSuiteOnWindows();

export const runTest = (source: string) => {
const tmpFilename = path.join(os.tmpdir(), 'circus-test-file.js');
const filename = crypto
.createHash('md5')
.update(source)
.digest('hex');
const tmpFilename = path.join(os.tmpdir(), filename);

const content = `
require('${BABEL_REGISTER_PATH}');
Expand All @@ -51,6 +56,9 @@ export const runTest = (source: string) => {
fs.writeFileSync(tmpFilename, content);
const result = spawnSync('node', [tmpFilename], {cwd: process.cwd()});

// For compat with cross-spawn
result.status = result.code;

if (result.status !== 0) {
const message = `
STDOUT: ${result.stdout && result.stdout.toString()}
Expand All @@ -64,6 +72,8 @@ export const runTest = (source: string) => {
result.stdout = String(result.stdout);
result.stderr = String(result.stderr);

fs.unlinkSync(tmpFilename);

if (result.stderr) {
throw new Error(
`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ run_describe_finish: describe
run_describe_finish: ROOT_DESCRIBE_BLOCK
run_finish
unhandledErrors: 0
"
unhandledErrors: 0"
`;

exports[`describe block cannot have hooks and no tests 1`] = `
Expand All @@ -45,8 +44,7 @@ run_describe_finish: describe
run_describe_finish: ROOT_DESCRIBE_BLOCK
run_finish
unhandledErrors: 4
"
unhandledErrors: 4"
`;

exports[`tests are not marked done until their parent afterAll runs 1`] = `
Expand Down Expand Up @@ -110,6 +108,5 @@ run_describe_finish: 2nd describe
run_describe_finish: ROOT_DESCRIBE_BLOCK
run_finish
unhandledErrors: 1
"
unhandledErrors: 1"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ run_describe_finish: describe
run_describe_finish: ROOT_DESCRIBE_BLOCK
run_finish
unhandledErrors: 0
"
unhandledErrors: 0"
`;

exports[`simple test 1`] = `
Expand Down Expand Up @@ -64,6 +63,5 @@ run_describe_finish: describe
run_describe_finish: ROOT_DESCRIBE_BLOCK
run_finish
unhandledErrors: 0
"
unhandledErrors: 0"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,5 @@ run_describe_finish: 2nd describe
run_describe_finish: ROOT_DESCRIBE_BLOCK
run_finish
unhandledErrors: 0
"
unhandledErrors: 0"
`;
10 changes: 5 additions & 5 deletions packages/jest-circus/src/__tests__/circus_it_test_error.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
let circusIt;
let circusTest;

//using jest-jasmine2's 'it' to test jest-circus's 'it'. Had to differentiate
//the two with this aliaser.
// using jest-jasmine2's 'it' to test jest-circus's 'it'. Had to differentiate
// the two with this alias.

const aliasCircusIt = () => {
const {it, test} = require('../index.js');
Expand All @@ -23,9 +23,9 @@ const aliasCircusIt = () => {

aliasCircusIt();

//A few of these tests require incorrect types to throw errors and thus pass
//the test. The typechecks on jest-circus would prevent that, so
//this file has been listed in the .flowconfig ignore section.
// A few of these tests require incorrect types to throw errors and thus pass
// the test. The typechecks on jest-circus would prevent that, so
// this file has been listed in the .flowconfig ignore section.

describe('test/it error throwing', () => {
it(`it doesn't throw an error with valid arguments`, () => {
Expand Down
3 changes: 0 additions & 3 deletions packages/jest-circus/src/__tests__/hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@

import {runTest} from '../__mocks__/test_utils';

const ConditionalTest = require('../../../../scripts/ConditionalTest');
ConditionalTest.skipSuiteOnWindows();

test('beforeEach is executed before each test in current/child describe blocks', () => {
const {stdout} = runTest(`
describe('describe', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import type {Environment} from 'types/Environment';
import type {GlobalConfig, ProjectConfig} from 'types/Config';
import type {TestResult} from 'types/TestResult';
// eslint-disable-next-line import/no-extraneous-dependencies
import type Runtime from 'jest-runtime';

const FRAMEWORK_INITIALIZER = require.resolve('./jest_adapter_init');
Expand Down

0 comments on commit 12d91ad

Please sign in to comment.