Jest integration question

If Jest is in my package.json, it is automatically detected and tests can be run within a test file. This is great. However, if Jest is not in my package.json, but is instead in the package.json of a dependency, it is not detected. 

Is there a way to set up IntelliJ IDEA so that it can run Jest tests using Jest from node_modules?

17 comments

The logic used for determining what test runner is available for a given test file is based on dependencies declarations in package.json nearest to the current file. If Jest is not listed among dependencies, IDEA can't detect that it's installed. Indirect dependencies are not taken into account

So, if you don't have a direct dependency on Jest, you have to create Jest run configurations manually via Run | Edit Configurations...

0

Hi Elena, 

This is quite annoying to add manually a Run configuration for each test file :(

It is also quite annoying to add Jest as devDependencies, knowing that Jest is not a peer dependency of the project : https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/package.json#L47

For a project generated with CRA (create-react-app), you do not need to add Jest as dev dependencies, thus you might not specify the same version on your own package.json which can be quite .... disappointing :)

It could be really intereseting to opt-in for a config to specify which test runner to use, don't you think ? 

 

0

For applications created with create-react-app Webstorm knows what test runner to use and shows the icons in the gutter (despite not having Jest listed explicitly in package.json).

 

if it doesn't work for you, please provide a test project that shows up the issue

 

0

Oh I see, are you checking the package.json for a "react-scripts" entry and them assuming it's Jest tests ? 

I have a fork of the CRA's react-scripts that's why I believe Intellij does not provide the quick launchers around the describe & it blocks. The fork is not named "react-scripts" in package.json.

Is the package.json lookup explained in depth somewhere so I can not bother you too much ? 

Is there something I can do ? 

 

Thanks anyway for your quick answer, really appreciate it.

0

>are you checking the package.json for a "react-scripts" entry and them assuming it's Jest tests ?

Yes

>Is the package.json lookup explained in depth somewhere so I can not bother you too much ? 

No, it's a technical information users shouldn't normally bother about

0

I have a similar question here but for the TypeScript version. We're using jest, ts-jest and react-scripts-ts and IntelliJ isn't detecting our test files as being tests that can be run directly from the gutter. In our case we do actually have both jest and ts-jest listed as devDependencies. Is there an expectation that this should still work in the TypeScript world?

0

yes, it should work with react-scripts-ts listed in dependencies/devDependencies:

 

what do your tests look like? please provide screenshots that show up the issue

0

Interesting, it seems that the reason it's not working for us is because we're using a function to look up the current file name to set the value for the "describe".

If I change that to a string literal then it works.

I'm not sure if this is intended behavior or not. I don't remember if we have a good reason to be doing this so I might just remove this behavior from our tests.

0

Unfortunately, IDE doesn't support references instead of string literals in describe() signature, as static analysis is not capable to figure out what value a reference will have in runtime - and the IDE needs this value to construct a testNamePattern

Please follow https://youtrack.jetbrains.com/issue/WEB-32774 and linked tickets for updates

0

I'm using Webstorm 2018.1 (also tried 2018.2 EAP) and am not able to get the right-click functionality to work.

  • I have set up the default Jest configuration
  • Jest is listed in the closest package.json as a devDependency
  • This setup works correctly in Rubymine

 

0

Please provide screenshots of the issue and of your Jest configuration and package.json. A test project would also be very helpful.

0

Here's a screenshot, unfortunately I cannot provide the other information as this relates to business use. My coworkers on rubymine have compared setups and they are identical. I've also checked on my machine with rubymine and have had no issues.

 If I manually create a run configuration with jest it works fine. But that's not a practical workaround.

 

 

0

Is the issue specific to certain test file/suite? Or, does it occur with each and every Jest test? Please provide a file that shows up the issue plus your idea.log (https://intellij-support.jetbrains.com/hc/en-us/articles/207241085-Locating-IDE-log-files)

0

Hi Elena,

It happens with all jest tests. I can't post my logs publicly, can I email or DM them to you?

Even a test as simple as the example below wont give me the jest option. Nor do I see the normal test options in my editor's gutter. My co-workers have repeated verified my settings to ensure they are identical. The only difference being that they are using RubyMine (2017.3.6). I've even installed RubyMine and tested (see final screenshot) and it works as expected.

// @flow

describe('something', () => {
it('works', () => {
expect(true).toBe(true);
});
});

 

 

No Gutter Icons

No Jest in Context Menu


Same Config With RubyMine


0

If you don't want to post your logs here, you can create a support ticket or upload them to our FTP  ftp://ftp.intellij.net/.uploads/ and let me know the file name

0

Hi Elena, I created a ticket to continue this conversation.

0

Circling back for those that stumble upon this thread, my package.json was using nohoist in the workspaces config and was causing the package.json to fail parsing[^1]. This will be fixed in 2018.2.1.

 

Thanks Elena for helping get to the bottom of the issue!

 

[1] https://youtrack.jetbrains.com/issue/WEB-33150

1

Please sign in to leave a comment.