Skip to content

Commit

Permalink
jest-circus test utils (#5636)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronabramov authored and cpojer committed Feb 27, 2018
1 parent d584277 commit 158a76f
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"babel-preset-env": "^1.4.0",
"babel-preset-react": "^6.24.1",
"babel-preset-react-native": "^4.0.0",
"babel-register": "^6.26.0",
"browserify": "^14.4.0",
"chalk": "^2.0.1",
"codecov": "^3.0.0",
Expand Down
62 changes: 62 additions & 0 deletions packages/jest-circus/src/__mocks__/test_event_handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* 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';

import type {EventHandler} from 'types/Circus';

const testHandler: EventHandler = (event, state) => {
switch (event.name) {
case 'start_describe_definition': {
console.log(event.name + ':', event.blockName);
break;
}
case 'run_describe_start':
case 'run_describe_finish': {
console.log(event.name + ':', event.describeBlock.name);
break;
}
case 'test_start':
case 'test_done': {
console.log(event.name + ':', event.test.name);
break;
}

case 'add_test': {
console.log(event.name + ':', event.testName);
break;
}

case 'test_fn_start':
case 'test_fn_success':
case 'test_fn_failure': {
console.log(event.name + ':', event.test.name);
break;
}

case 'add_hook': {
console.log(event.name + ':', event.hookType);
break;
}

case 'hook_start':
case 'hook_success':
case 'hook_failure': {
console.log(event.name + ':', event.hook.type);
break;
}

default: {
console.log(event.name);
}
}
};

export default testHandler;
73 changes: 73 additions & 0 deletions packages/jest-circus/src/__mocks__/test_utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* 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';

import {spawnSync} from 'child_process';
import fs from 'fs';
import os from 'os';
import path from 'path';

const CIRCUS_PATH = require.resolve('../../build/index');
const CIRCUS_RUN_PATH = require.resolve('../../build/run');
const CIRCUS_STATE_PATH = require.resolve('../../build/state');
const TEST_EVENT_HANDLER_PATH = require.resolve('./test_event_handler');
const BABEL_REGISTER_PATH = require.resolve('babel-register');

export const runTest = (source: string) => {
const tmpFilename = path.join(os.tmpdir(), 'circus-test-file.js');

const content = `
require('${BABEL_REGISTER_PATH}');
const circus = require('${CIRCUS_PATH}');
global.test = circus.test;
global.describe = circus.describe;
global.beforeEach = circus.beforeEach;
global.afterEach = circus.afterEach;
global.beforeAll = circus.beforeAll;
global.afterAll = circus.afterAll;
const testEventHandler = require('${TEST_EVENT_HANDLER_PATH}').default;
const addEventHandler = require('${CIRCUS_STATE_PATH}').addEventHandler;
addEventHandler(testEventHandler);
${source};
const run = require('${CIRCUS_RUN_PATH}').default;
run();
`;

fs.writeFileSync(tmpFilename, content);
const result = spawnSync('node', [tmpFilename], {cwd: process.cwd()});

if (result.status !== 0) {
const message = `
STDOUT: ${result.stdout && result.stdout.toString()}
STDERR: ${result.stderr && result.stderr.toString()}
STATUS: ${result.status}
ERROR: ${String(result.error)}
`;
throw new Error(message);
}

result.stdout = String(result.stdout);
result.stderr = String(result.stderr);

if (result.stderr) {
throw new Error(
`
Unexpected stderr:
${result.stderr}
`,
);
}
return result;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`beforeEach is executed before each test in current/child describe blocks 1`] = `
"start_describe_definition: describe
add_hook: beforeEach
add_test: one
add_test: two
start_describe_definition: 2nd level describe
add_hook: beforeEach
add_test: 2nd level test
start_describe_definition: 3rd level describe
add_test: 3rd level test
add_test: 3rd level test#2
finish_describe_definition
finish_describe_definition
finish_describe_definition
start_describe_definition: 2nd describe
add_hook: beforeEach
add_test: 2nd describe test
finish_describe_definition
run_start
run_describe_start: ROOT_DESCRIBE_BLOCK
run_describe_start: describe
hook_start: beforeEach
hook_success: beforeEach
test_start: one
test_success
hook_start: beforeEach
hook_success: beforeEach
test_start: two
test_success
run_describe_start: 2nd level describe
hook_start: beforeEach
hook_success: beforeEach
hook_start: beforeEach
hook_success: beforeEach
test_start: 2nd level test
test_success
run_describe_start: 3rd level describe
hook_start: beforeEach
hook_success: beforeEach
hook_start: beforeEach
hook_success: beforeEach
test_start: 3rd level test
test_success
hook_start: beforeEach
hook_success: beforeEach
hook_start: beforeEach
hook_success: beforeEach
test_start: 3rd level test#2
test_success
run_describe_finish: 3rd level describe
run_describe_finish: 2nd level describe
run_describe_finish: describe
run_describe_start: 2nd describe
hook_start: beforeEach
hook_failure: beforeEach
test_start: 2nd describe test
test_success
run_describe_finish: 2nd describe
run_describe_finish: ROOT_DESCRIBE_BLOCK
run_finish
"
`;
42 changes: 42 additions & 0 deletions packages/jest-circus/src/__tests__/hooks.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* 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';

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

const SkipOnWindows = require('../../../../scripts/SkipOnWindows');
SkipOnWindows.suite();

test('beforeEach is executed before each test in current/child describe blocks', () => {
const {stdout} = runTest(`
describe('describe', () => {
beforeEach(() => {});
test('one', () => {});
test('two', () => {});
describe('2nd level describe', () => {
beforeEach(() => {});
test('2nd level test', () => {});
describe('3rd level describe', () => {
test('3rd level test', () => {});
test('3rd level test#2', () => {});
});
});
})
describe('2nd describe', () => {
beforeEach(() => { throw new Error('alabama'); });
test('2nd describe test', () => {});
})
`);

expect(stdout).toMatchSnapshot();
});

0 comments on commit 158a76f

Please sign in to comment.