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

Throw a descriptive error if the first argument supplied to a hook was not a function #6917

Merged
merged 6 commits into from Aug 30, 2018

Conversation

ranyitz
Copy link
Contributor

@ranyitz ranyitz commented Aug 29, 2018

Summary

This PR closes #6903 By adding validation for the first argument of the following hooks:

  • beforeEach
  • beforeAll
  • afterEach
  • afterAll

If the first argument was not a function Jest will throw a descriptive error:

image

Additional notes

  • I've upgraded flow-typed's Jest definition file to v23, because v21 didn't have support for jest.each.

Test plan

Verified that passing a string as first argument will throw an error, on both jasmine and circus runners.

@codecov-io
Copy link

codecov-io commented Aug 29, 2018

Codecov Report

Merging #6917 into master will decrease coverage by <.01%.
The diff coverage is 20%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #6917      +/-   ##
==========================================
- Coverage   66.98%   66.97%   -0.01%     
==========================================
  Files         250      250              
  Lines       10365    10375      +10     
  Branches        4        3       -1     
==========================================
+ Hits         6943     6949       +6     
- Misses       3421     3425       +4     
  Partials        1        1
Impacted Files Coverage Δ
packages/jest-jasmine2/src/jasmine/Env.js 0% <0%> (ø) ⬆️
packages/jest-circus/src/index.js 66.66% <100%> (+8.97%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8e72341...9d83f64. Read the comment docs.

@thymikee thymikee requested a review from SimenB August 30, 2018 07:27
const circus = require('../index.js');

describe('hooks error throwing', () => {
test.each([['beforeEach'], ['beforeAll'], ['afterEach'], ['afterAll']])(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need the nested arrays, can be just test.each(['beforeEach', 'beforeAll', 'afterEach', 'afterAll'])(

expect(() => {
circus[fn]('param');
}).toThrowError(
`Invalid first argument, param. It must be a callback function.`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

single quotes?

Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome, thanks!

@SimenB
Copy link
Member

SimenB commented Aug 30, 2018

Aww, the typing info is wrong :( (#6351)

@SimenB SimenB merged commit 66560c3 into jestjs:master Aug 30, 2018
@ranyitz ranyitz deleted the hooks-first-argument-validation branch August 30, 2018 08:44
@ranyitz
Copy link
Contributor Author

ranyitz commented Aug 30, 2018

Thanks @SimenB 👍

@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Naming the beforeEach function produces imprecise error: fn.call is not a function
6 participants