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

Crashes when expect with no following statement. #357

Closed
garyking opened this issue Jul 31, 2019 · 3 comments · Fixed by #358
Closed

Crashes when expect with no following statement. #357

garyking opened this issue Jul 31, 2019 · 3 comments · Fixed by #358
Labels

Comments

@garyking
Copy link
Contributor

If you have code like expect() without anything after, like toBe, then it crashes the linter. It shouldn't crash the linter. I imagine Jest would do enough to notify you that something is wrong, or at least the linter should indicate that the following statement is missing.

Sample code:

test('test', () => {
  expect('a');
})

Which results in the following error when linting:

TypeError: Cannot read property 'name' of undefined
Occurred while linting asdf.test.js:2
    at methodName (/usr/local/lib/node_modules/eslint-plugin-jest/lib/rules/util.js:66:40)
    at expectToBeCase (/usr/local/lib/node_modules/eslint-plugin-jest/lib/rules/util.js:42:140)
    at CallExpression (/usr/local/lib/node_modules/eslint-plugin-jest/lib/rules/prefer-to-be-null.js:25:45)
    at listeners.(anonymous function).forEach.listener (/usr/local/lib/node_modules/eslint/lib/util/safe-emitter.js:45:58)
    at Array.forEach (<anonymous>)
    at Object.emit (/usr/local/lib/node_modules/eslint/lib/util/safe-emitter.js:45:38)
    at NodeEventGenerator.applySelector (/usr/local/lib/node_modules/eslint/lib/util/node-event-generator.js:251:26)
    at NodeEventGenerator.applySelectors (/usr/local/lib/node_modules/eslint/lib/util/node-event-generator.js:280:22)
    at NodeEventGenerator.enterNode (/usr/local/lib/node_modules/eslint/lib/util/node-event-generator.js:294:14)
    at CodePathAnalyzer.enterNode (/usr/local/lib/node_modules/eslint/lib/code-path-analysis/code-path-analyzer.js:632:23)
@G-Rath
Copy link
Collaborator

G-Rath commented Jul 31, 2019

This shouldn't be happening! 😱

I'm going to make a quick patching PR, but this is one of the problems that'll be fixed once we've finished converting to TypeScript (I've rewritten all the guards, for exactly this sort of use-case).

I imagine Jest would do enough to notify you that something is wrong, or at least the linter should indicate that the following statement is missing.

This should be covered by the valid-expect rule - All other rules should just gracefully bail under the idea that valid-expect will handle making noise about the validity of an expect.

(I can't actually remember if we consider expect(<arg>) as "invalid", as it'll run just fine, even if it is completely meaningless, but it definitely shouldn't cause a crash)

@SimenB
Copy link
Member

SimenB commented Jul 31, 2019

It should be flagged by valid-expect, yes. Jest itself has no way of detecting whether a matcher is used or not

@SimenB
Copy link
Member

SimenB commented Jul 31, 2019

🎉 This issue has been resolved in version 22.14.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants