Skip to content

Commit

Permalink
jest-circus test utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronabramov committed Feb 21, 2018
1 parent 1203ed6 commit 6d1f34c
Show file tree
Hide file tree
Showing 7 changed files with 779 additions and 344 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;
76 changes: 76 additions & 0 deletions packages/jest-circus/src/__mocks__/test_utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* 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 = path.resolve(__dirname, '../../build/index');
const CIRCUS_RUN_PATH = path.resolve(__dirname, '../../build/run');
const CIRCUS_STATE_PATH = path.resolve(__dirname, '../../build/state');
const TEST_EVENT_HANDLER_PATH = path.resolve(__dirname, './test_event_handler');
const BABEL_REGISTER_PATH = path.resolve(
__dirname,
'../../../../node_modules/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[`tests are not marked done until their parent afterAll runs 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
"
`;
39 changes: 39 additions & 0 deletions packages/jest-circus/src/__tests__/hooks.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* 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';

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();
});
3 changes: 1 addition & 2 deletions packages/jest-util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
"graceful-fs": "^4.1.11",
"is-ci": "^1.0.10",
"jest-message-util": "^22.4.0",
"mkdirp": "^0.5.1",
"source-map": "^0.6.0"
"mkdirp": "^0.5.1"
},
"devDependencies": {
"jest-mock": "^22.2.0"
Expand Down
Loading

0 comments on commit 6d1f34c

Please sign in to comment.