-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d584277
commit 158a76f
Showing
5 changed files
with
242 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
64 changes: 64 additions & 0 deletions
64
packages/jest-circus/src/__tests__/__snapshots__/hooks.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |