Testing with Jest outside of __tests__ directory
Hello,
In Webstorm, we can right-click and select "Run 'xxxxx.js'" to run the tests with Jest. It works fine, and the output is quite helpful. It is also good so we don't need to run the entire suite of tests everyone time we just want to run one file. We can also right click on the green arrows that appears next to the line numbers of "describe" or "test" lines to run those sections by themselves.
However, when the test files are located outside the __tests__ directory, they don't work the same. (This is a React Native project, so the __tests__ directory is automatically created.) If you attempt to right-click and select "Run 'xxxx.js'", instead of running it with Jest, it attempts to run it as a normal NodeJS program, and that doesn't work. There is no way to get it to automatically run in Jest again. The only way is to create a new configuration file for each file that you want to run, and that is quite unnecessarily tedious.
Marking the directory that they are in as a Test Source Root also doesn't help. They only have the option to automatically run in Jest if they are in the root __tests__ directory. Also, creating a new __tests__ directory in another folder doesn't work either.
I've checked the configuration options as well as package.json and wherever else I can think of, but there is nothing explicitly pointing to the __tests__ folder that is configurable from my end. Am I missing something, or is this just a bug?
Thank you very much.
Please sign in to leave a comment.
in 171.3780, I can use icons in gutter/file right-click menu to run Jest tests located in folders other than __tests_ - see screenshots.
If it doesn't work for you, can you provide a sample project that shows up the issue?
Right now it's only not working in my large private project. I tried it again in a smaller project that I created recently and it worked.
Additionally, for me it only works in some directories. Observe:
It does not show up properly for the file in this directory, js/shared.
However, I copied and pasted the same file into the parent directory js/ and...
Tada, it works, quite inexplicably. I've spent a couple hours trying all manner of different settings or configurations, I even deleted the project and re-added it, but still no changes. It just does not work in some directories for no explicit reason. Even though you can see the icon for the file has changed to the JS file with the orange/green arrows over it, indicating the the IDE does indeed detect that it contains test code. Most curious!
Thank you very much.
strange... but I definitely need a project that shows up an issue to see what's going on
Actually, I'm happy to report that I've solved this issue, and figured out what was causing it.
For my React Native project, I was using the technique found here that allows one to import from absolute paths in RN, so we don't have to have tons of "../.../.." all over the place. Source: https://medium.com/@davidjwoody/how-to-use-absolute-paths-in-react-native-6b06ae3f65d1
So since I had a small package.json file in each of the affected folders, it stopped working. I suppose Webstorm scans package.json and searches for a dependency on Jest. So I briefly just added jest to the devDependencies of each of these little folders, and it started working. This probably isn't the ideal solution (just as this technique for getting RN to compile with absolute paths isn't ideal), but it works.
Thanks for reading!
Great, thanks for update!
>I suppose Webstorm scans package.json and searches for a dependency on Jest.
Yes, exactly:) It searches for the nearest package.json while traversing up toward the project root and, if the found package.json has 'jest' or 'jest-cli' in its dependencies lists, provides the corresponding actions