New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow custom environments or other setup scripts to bind to Circus events. #8307
Closed
Closed
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5b3f707
Allow custom environments or other setup scripts to bind to Circus ev…
scotthovestadt e1412ad
Add copyright header.
scotthovestadt 5fc39f9
Update CHANGELOG.md
scotthovestadt 242370b
Execute Circus it tests isolated from Circus when run with Circus and…
scotthovestadt 7caef88
Fix lint error.
scotthovestadt eb8d0b7
Merge branch 'master' into circus-expose-state
scotthovestadt 22e5153
Update README.md
scotthovestadt ac15df1
Update README.md
scotthovestadt File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion
2
packages/jest-circus/src/__tests__/__snapshots__/hooks.test.ts.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
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
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,196 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import chalk from 'chalk'; | ||
import {bind as bindEach} from 'jest-each'; | ||
import {formatExecError} from 'jest-message-util'; | ||
import {ErrorWithStack, isPromise} from 'jest-util'; | ||
import {Global} from '@jest/types'; | ||
import { | ||
BlockFn, | ||
HookFn, | ||
HookType, | ||
TestFn, | ||
BlockMode, | ||
BlockName, | ||
TestName, | ||
TestMode, | ||
} from './types'; | ||
import {dispatch} from './state'; | ||
|
||
type THook = (fn: HookFn, timeout?: number) => void; | ||
type DescribeFn = (blockName: BlockName, blockFn: BlockFn) => void; | ||
|
||
export const describe = (() => { | ||
const describe = (blockName: BlockName, blockFn: BlockFn) => | ||
_dispatchDescribe(blockFn, blockName, describe); | ||
const only = (blockName: BlockName, blockFn: BlockFn) => | ||
_dispatchDescribe(blockFn, blockName, only, 'only'); | ||
const skip = (blockName: BlockName, blockFn: BlockFn) => | ||
_dispatchDescribe(blockFn, blockName, skip, 'skip'); | ||
|
||
describe.each = bindEach(describe, false); | ||
|
||
only.each = bindEach(only, false); | ||
skip.each = bindEach(skip, false); | ||
|
||
describe.only = only; | ||
describe.skip = skip; | ||
|
||
return describe; | ||
})(); | ||
|
||
const _dispatchDescribe = ( | ||
blockFn: BlockFn, | ||
blockName: BlockName, | ||
describeFn: DescribeFn, | ||
mode?: BlockMode, | ||
) => { | ||
const asyncError = new ErrorWithStack(undefined, describeFn); | ||
if (blockFn === undefined) { | ||
asyncError.message = `Missing second argument. It must be a callback function.`; | ||
throw asyncError; | ||
} | ||
if (typeof blockFn !== 'function') { | ||
asyncError.message = `Invalid second argument, ${blockFn}. It must be a callback function.`; | ||
throw asyncError; | ||
} | ||
dispatch({ | ||
asyncError, | ||
blockName, | ||
mode, | ||
name: 'start_describe_definition', | ||
}); | ||
const describeReturn = blockFn(); | ||
|
||
// TODO throw in Jest 25 | ||
if (isPromise(describeReturn)) { | ||
console.log( | ||
formatExecError( | ||
new ErrorWithStack( | ||
chalk.yellow( | ||
'Returning a Promise from "describe" is not supported. Tests must be defined synchronously.\n' + | ||
'Returning a value from "describe" will fail the test in a future version of Jest.', | ||
), | ||
describeFn, | ||
), | ||
{rootDir: '', testMatch: []}, | ||
{noStackTrace: false}, | ||
), | ||
); | ||
} else if (describeReturn !== undefined) { | ||
console.log( | ||
formatExecError( | ||
new ErrorWithStack( | ||
chalk.yellow( | ||
'A "describe" callback must not return a value.\n' + | ||
'Returning a value from "describe" will fail the test in a future version of Jest.', | ||
), | ||
describeFn, | ||
), | ||
{rootDir: '', testMatch: []}, | ||
{noStackTrace: false}, | ||
), | ||
); | ||
} | ||
|
||
dispatch({blockName, mode, name: 'finish_describe_definition'}); | ||
}; | ||
|
||
const _addHook = ( | ||
fn: HookFn, | ||
hookType: HookType, | ||
hookFn: THook, | ||
timeout?: number, | ||
) => { | ||
const asyncError = new ErrorWithStack(undefined, hookFn); | ||
|
||
if (typeof fn !== 'function') { | ||
asyncError.message = | ||
'Invalid first argument. It must be a callback function.'; | ||
|
||
throw asyncError; | ||
} | ||
|
||
dispatch({asyncError, fn, hookType, name: 'add_hook', timeout}); | ||
}; | ||
|
||
// Hooks have to pass themselves to the HOF in order for us to trim stack traces. | ||
export const beforeEach: THook = (fn, timeout) => | ||
_addHook(fn, 'beforeEach', beforeEach, timeout); | ||
export const beforeAll: THook = (fn, timeout) => | ||
_addHook(fn, 'beforeAll', beforeAll, timeout); | ||
export const afterEach: THook = (fn, timeout) => | ||
_addHook(fn, 'afterEach', afterEach, timeout); | ||
export const afterAll: THook = (fn, timeout) => | ||
_addHook(fn, 'afterAll', afterAll, timeout); | ||
|
||
export const test: Global.It = (() => { | ||
const test = (testName: TestName, fn: TestFn, timeout?: number): void => | ||
_addTest(testName, undefined, fn, test, timeout); | ||
const skip = (testName: TestName, fn?: TestFn, timeout?: number): void => | ||
_addTest(testName, 'skip', fn, skip, timeout); | ||
const only = (testName: TestName, fn: TestFn, timeout?: number): void => | ||
_addTest(testName, 'only', fn, test.only, timeout); | ||
|
||
test.todo = (testName: TestName, ...rest: Array<any>): void => { | ||
if (rest.length > 0 || typeof testName !== 'string') { | ||
throw new ErrorWithStack( | ||
'Todo must be called with only a description.', | ||
test.todo, | ||
); | ||
} | ||
return _addTest(testName, 'todo', () => {}, test.todo); | ||
}; | ||
|
||
const _addTest = ( | ||
testName: TestName, | ||
mode: TestMode, | ||
fn: TestFn | undefined, | ||
testFn: (testName: TestName, fn: TestFn, timeout?: number) => void, | ||
timeout?: number, | ||
) => { | ||
const asyncError = new ErrorWithStack(undefined, testFn); | ||
|
||
if (typeof testName !== 'string') { | ||
asyncError.message = `Invalid first argument, ${testName}. It must be a string.`; | ||
|
||
throw asyncError; | ||
} | ||
if (fn === undefined) { | ||
asyncError.message = | ||
'Missing second argument. It must be a callback function. Perhaps you want to use `test.todo` for a test placeholder.'; | ||
|
||
throw asyncError; | ||
} | ||
if (typeof fn !== 'function') { | ||
asyncError.message = `Invalid second argument, ${fn}. It must be a callback function.`; | ||
|
||
throw asyncError; | ||
} | ||
|
||
return dispatch({ | ||
asyncError, | ||
fn, | ||
mode, | ||
name: 'add_test', | ||
testName, | ||
timeout, | ||
}); | ||
}; | ||
|
||
test.each = bindEach(test); | ||
only.each = bindEach(only); | ||
skip.each = bindEach(skip); | ||
|
||
test.only = only; | ||
test.skip = skip; | ||
|
||
return test; | ||
})(); | ||
|
||
export const it: Global.It = test; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we don't need to add
/
afterbuild
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'll resolve to the index.js path. It could probably be without
build
as well, and it'll usemain
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
adding the
/
wasn't strictly necessary, it's just a result of churn because I changed the import path to something else and then back againdoesn't make a difference either way, it'll resolve to index