Skip to content
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

Validate hook functions first argument #6931

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

- `[jest-resolve]` Only resolve realpath once in try-catch ([#6925](https://github.com/facebook/jest/pull/6925))
- `[expect]` Fix TypeError in `toBeInstanceOf` on `null` or `undefined` ([#6912](https://github.com/facebook/jest/pull/6912))
- `[jest-jasmine2]` Throw a descriptive error if the first argument supplied to a hook was not a function ([#6917](https://github.com/facebook/jest/pull/6917))
- `[jest-circus]` Throw a descriptive error if the first argument supplied to a hook was not a function ([#6917](https://github.com/facebook/jest/pull/6917))
- `[jest-jasmine2]` Throw a descriptive error if the first argument supplied to a hook was not a function ([#6917](https://github.com/facebook/jest/pull/6917)) and ([#6931](https://github.com/facebook/jest/pull/6931))
- `[jest-circus]` Throw a descriptive error if the first argument supplied to a hook was not a function ([#6917](https://github.com/facebook/jest/pull/6917)) and ([#6931](https://github.com/facebook/jest/pull/6931))
- `[expect]` Fix variadic custom asymmetric matchers ([#6898](https://github.com/facebook/jest/pull/6898))
- `[jest-cli]` Fix incorrect `testEnvironmentOptions` warning ([#6852](https://github.com/facebook/jest/pull/6852))
- `[jest-each]` Prevent done callback being supplied to describe ([#6843](https://github.com/facebook/jest/pull/6843))
Expand Down
27 changes: 25 additions & 2 deletions flow-typed/npm/jest_v23.x.x.js
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,19 @@ declare var describe: {
/**
* Skip running this describe block
*/
skip(name: JestTestName, fn: () => void): void
skip(name: JestTestName, fn: () => void): void,

/**
* each runs this test against array of argument arrays per each run
*
* @param {table} table of Test
*/
each(
table: Array<Array<mixed>>
): (
name: JestTestName,
fn?: (...args: Array<any>) => ?Promise<mixed>
) => void,
};

/** An individual test unit */
Expand Down Expand Up @@ -984,7 +996,18 @@ declare var it: {
name: JestTestName,
fn?: (done: () => void) => ?Promise<mixed>,
timeout?: number
): void
): void,
/**
* each runs this test against array of argument arrays per each run
*
* @param {table} table of Test
*/
each(
table: Array<Array<mixed>>
): (
name: JestTestName,
fn?: (...args: Array<any>) => ?Promise<mixed>
) => void,
};
declare function fit(
name: JestTestName,
Expand Down
36 changes: 24 additions & 12 deletions packages/jest-circus/src/__tests__/hooks_error.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,27 @@

const circus = require('../index.js');

describe('hooks error throwing', () => {
test.each([['beforeEach'], ['beforeAll'], ['afterEach'], ['afterAll']])(
'%s throws an error when the first argument is not a function',
fn => {
expect(() => {
circus[fn]('param');
}).toThrowError(
'Invalid first argument, param. It must be a callback function.',
);
},
);
});
describe.each([['beforeEach'], ['beforeAll'], ['afterEach'], ['afterAll']])(
'%s hooks error throwing',
fn => {
test.each([
['String'],
[1],
[[]],
[{}],
[Symbol('hello')],
[true],
[null],
[undefined],
])(
`${fn} throws an error when %p is provided as a first argument to it`,
el => {
expect(() => {
circus[fn](el);
}).toThrowError(
'Invalid first argument. It must be a callback function.',
);
},
);
},
);
4 changes: 1 addition & 3 deletions packages/jest-circus/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ const _dispatchDescribe = (blockFn, blockName, mode?: BlockMode) => {

const _addHook = (fn: HookFn, hookType: HookType, hookFn, timeout: ?number) => {
if (typeof fn !== 'function') {
throw new Error(
`Invalid first argument, ${fn}. It must be a callback function.`,
);
throw new Error('Invalid first argument. It must be a callback function.');
}

const asyncError = new Error();
Expand Down
36 changes: 24 additions & 12 deletions packages/jest-jasmine2/src/__tests__/hooks_error.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,27 @@

'use strict';

describe('hooks error throwing', () => {
test.each([['beforeEach'], ['beforeAll'], ['afterEach'], ['afterAll']])(
'%s throws an error when the first argument is not a function',
fn => {
expect(() => {
global[fn]('param');
}).toThrowError(
'Invalid first argument, param. It must be a callback function.',
);
},
);
});
describe.each([['beforeEach'], ['beforeAll'], ['afterEach'], ['afterAll']])(
'%s hooks error throwing',
fn => {
test.each([
['String'],
[1],
[[]],
[{}],
[Symbol('hello')],
[true],
[null],
[undefined],
])(
`${fn} throws an error when %p is provided as a first argument to it`,
el => {
expect(() => {
global[fn](el);
}).toThrowError(
'Invalid first argument. It must be a callback function.',
);
},
);
},
);
24 changes: 0 additions & 24 deletions packages/jest-jasmine2/src/jasmine/Env.js
Original file line number Diff line number Diff line change
Expand Up @@ -503,12 +503,6 @@ export default function(j$) {
};

this.beforeEach = function(beforeEachFunction, timeout) {
if (typeof beforeEachFunction !== 'function') {
throw new Error(
`Invalid first argument, ${beforeEachFunction}. It must be a callback function.`,
);
}

currentDeclarationSuite.beforeEach({
fn: beforeEachFunction,
timeout() {
Expand All @@ -518,12 +512,6 @@ export default function(j$) {
};

this.beforeAll = function(beforeAllFunction, timeout) {
if (typeof beforeAllFunction !== 'function') {
throw new Error(
`Invalid first argument, ${beforeAllFunction}. It must be a callback function.`,
);
}

currentDeclarationSuite.beforeAll({
fn: beforeAllFunction,
timeout() {
Expand All @@ -533,12 +521,6 @@ export default function(j$) {
};

this.afterEach = function(afterEachFunction, timeout) {
if (typeof afterEachFunction !== 'function') {
throw new Error(
`Invalid first argument, ${afterEachFunction}. It must be a callback function.`,
);
}

currentDeclarationSuite.afterEach({
fn: afterEachFunction,
timeout() {
Expand All @@ -548,12 +530,6 @@ export default function(j$) {
};

this.afterAll = function(afterAllFunction, timeout) {
if (typeof afterAllFunction !== 'function') {
throw new Error(
`Invalid first argument, ${afterAllFunction}. It must be a callback function.`,
);
}

currentDeclarationSuite.afterAll({
fn: afterAllFunction,
timeout() {
Expand Down
20 changes: 20 additions & 0 deletions packages/jest-jasmine2/src/jasmine/jasmine_light.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,38 @@ exports.interface = function(jasmine: Jasmine, env: any) {
},

beforeEach() {
if (typeof arguments[0] !== 'function') {
throw new Error(
'Invalid first argument. It must be a callback function.',
);
}
return env.beforeEach.apply(env, arguments);
},

afterEach() {
if (typeof arguments[0] !== 'function') {
throw new Error(
'Invalid first argument. It must be a callback function.',
);
}
return env.afterEach.apply(env, arguments);
},

beforeAll() {
if (typeof arguments[0] !== 'function') {
throw new Error(
'Invalid first argument. It must be a callback function.',
);
}
return env.beforeAll.apply(env, arguments);
},

afterAll() {
if (typeof arguments[0] !== 'function') {
throw new Error(
'Invalid first argument. It must be a callback function.',
);
}
return env.afterAll.apply(env, arguments);
},

Expand Down