jest icon indicating copy to clipboard operation
jest copied to clipboard

Run tests by filename pattern instead of directory?

Open quicksnap opened this issue 11 years ago • 20 comments

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?

quicksnap avatar Mar 31 '15 16:03 quicksnap

:+1: ping

jehoshua02 avatar Apr 26 '15 21:04 jehoshua02

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.

jehoshua02 avatar Apr 26 '15 22:04 jehoshua02

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.

jehoshua02 avatar Apr 26 '15 22:04 jehoshua02

Nice workaround! Thanks.

quicksnap avatar Apr 28 '15 20:04 quicksnap

+1

franleplant avatar May 02 '15 03:05 franleplant

Seems this one has a normally working and configurable solution. I guess we may keep default __tests__ pattern, and close this issue?

DmitrySoshnikov avatar Jun 08 '15 23:06 DmitrySoshnikov

+1 I would love to see it in regular documentation rather than just in this issue.

zhon avatar Jun 09 '15 13:06 zhon

~~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

g-p-g avatar Jun 15 '15 19:06 g-p-g

would love to have this properly supported in the docs

cogell avatar Dec 10 '15 21:12 cogell

Feel free to send a pull request to update the documentation to include this :)

cpojer avatar Dec 10 '15 21:12 cpojer

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.

ghost avatar Apr 25 '16 23:04 ghost

Yes we definitely want to do this (for the next release maybe). @dmitriiabramov was looking into it I believe.

cpojer avatar Apr 25 '16 23:04 cpojer

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

cpojer avatar May 20 '16 20:05 cpojer

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

ghost avatar May 20 '16 21:05 ghost

It's here: http://facebook.github.io/jest/docs/api.html#moduledirectories-array-string but there isn't a guide for this yet.

cpojer avatar May 20 '16 21:05 cpojer

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.

ghost avatar May 20 '16 21:05 ghost

I'm sorry, I thought you asked about the moduleDirectories setting because I've been closing a lot of issues with that.

cpojer avatar May 20 '16 21:05 cpojer

Ok, I added testRegex to the documentation :)

cpojer avatar May 20 '16 21:05 cpojer

Historical note: the "testDirectoryName" option has been deprecated according to the docs: https://facebook.github.io/jest/docs/configuration.html

markhu avatar Aug 21 '18 18:08 markhu

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

github-actions[bot] avatar May 12 '21 13:05 github-actions[bot]