Run jest test from editor with different config depending on file location

I have a run configuration set up to run all of my jest unit tests with a specific jest configuration file. However when I try to run an individual test from the editor by clicking on the circle with a play button on it next to the line number, it runs the test without the configuration file which causes an error. Is there a way to specify what configuration file should be used when you run tests individually depending on the file location? I know you can set defaults for jest, but I have two separate jest configs, one for my react jest tests and one for my node jest tests, and I haven't found a way to specify the config file based on the file location (use node config if in server folder and use react config if in client folder).

8 comments

Currently, there is no way to specify a configuration file to be used when a single test is run. What are names of your configs? Where are they located relative to the project root?

0

having a similar issue, although I only have one config file: I can set that up in a 'configuration', for a global run of all tests, but if I want to use the UI to run a specific test, it doesn't take the config file, and I have to stop it, and edit the auto-generated test config to point it to the jest config.

Is there a way to configure the way the single test configuration is generated?

 

0

You can specify config for Jest Default run/debug configuration:

* on the main menu "Run | Edit Configurations...";

* expand "Defaults" node in the left area and find Jest run/debug configuration;

* specify needed config there, now all newly created Jest run/debug configurations will have the specified config.

3

I have added a segment in my test file names giving essentially the config I need.  (e.g. hooks.test.web.js, hooks.test.native.js).  Would be more flexible to define glob patterns, and then the associated configs.  Might need to order them so that the first matching glob was used to apply the config.  Or possibly an option in the config form to select a set of test files for the given config, or a directory, or glob pattern.

1

>Or possibly an option in the config form to select a set of test files for the given config, or a directory, or glob pattern.

Jest configs already define set of test files. See `testPathDirs`, `testRegex` from https://jest-bot.github.io/jest/docs/configuration.html. Sounds like a good idea to track all these Jest config files and auto-update set of test files per config. With this knowledge, Jest config can be inferred for a test file automatically. Would be great to file an issue with an example project to reproduce.

1

Good news! WebStorm 2018.3 brings an improvement here: it contains the fix for https://youtrack.jetbrains.com/issue/WEB-35553. Now, working directory is set to the closest directory with `jest.config.js`, allowing to use different `jest.config.js` files for different test locations.
Please note it works only if Jest config file name is constant - `jest.config.js`.

1

This only fixes a limited number of cases

See https://youtrack.jetbrains.com/issue/WEB-35553#focus=streamItem-27-3211461-0-0 for a scenario that isn't fixed.

0

Please sign in to leave a comment.