Hi, having a little trouble seeing if it's possible for Jest to locate tests by a file pattern instead of a directory name. I prefer to have my tests alongside source files, such as myComponent-test.jsx.
Are there affordances for this? If not, would you accept a PR?
:+1: ping
For example:
app/
components/
MyComponent.js
MyComponent.test.js
I'd like to collocate tests with source files. I would be able to easily tell, looking at the tree view or file searches in my editor, whether the source file I'm looking for has a corresponding test file and open both of them for editing more easily.
It's against the norm, but React has taught me to care less about the norm.
In the meantime, I override the testDirectoryName to point to my application then set testFileExtensions to match test.js. See the code in context. Hopefully this will work well.
Nice workaround! Thanks.
+1
Seems this one has a normally working and configurable solution. I guess we may keep default __tests__ pattern, and close this issue?
+1 I would love to see it in regular documentation rather than just in this issue.
At least with iojs-v2.3, no tests are executed when testFileExtensions is set to something like ["test.js"] (and the tests do end with test.js).
EDIT: of course it works :) I was using files ending with -test.js, instead of .test.js
would love to have this properly supported in the docs
Feel free to send a pull request to update the documentation to include this :)
The "testDirectoryName": "app" workaround is the 'correct' solution when you have all of your tests in a single subdirectory (e.g. app/), but it doesn't account for collocating tests in multiple subdirectories, or directly in the root directory because Jest will only look inside <rootDir>/app/.
It would be helpful to eliminate the requirement to specify a testDirectoryName entirely so that Jest could look for files everywhere in a repo. In my case, I have app-specific tests in <rootDir>/app/ and unit tests for my application server in <rootDir>/server/, but I can only set testDirectoryName to one or the other.
I tried tweaking the Jest code to allow "testDirectoryName": null, which would match test files everywhere, but the current code requires that all test filenames match a path that includes a root directory name, so refactoring requires a if (testDirectoryName === null) block that is a little awkward and I'm reluctant to submit a PR.
Yes we definitely want to do this (for the next release maybe). @dmitriiabramov was looking into it I believe.
We have finally implemented this for Jest 12.1.0. The option is called testRegex and the default is this: https://github.com/facebook/jest/blob/9d2b2bcf5e0705c64d5eba631c225426c09965ef/packages/jest-cli/src/config/defaults.js#L30
Excellent - any sense of when docs will be added? Looks like it's working well for my project but I don't want to be too maverick by using undocumented APIs
It's here: http://facebook.github.io/jest/docs/api.html#moduledirectories-array-string but there isn't a guide for this yet.
Ah okay, I am using testRegex directly in my jest config in package.json, e.g. "testRegex": "(src|server)/.*\\.test\\.js$".
I guess I could flip that over to use moduleDirectories if it's the preferred option, but the regex covers all of the path config I need in one swoop - I couldn't find mention of testRegex in the docs, though.
I'm sorry, I thought you asked about the moduleDirectories setting because I've been closing a lot of issues with that.
Ok, I added testRegex to the documentation :)
Historical note: the "testDirectoryName" option has been deprecated according to the docs: https://facebook.github.io/jest/docs/configuration.html
Most helpful comment
Ok, I added
testRegexto the documentation :)