We currently have a couple of submodules that are outside of the src directory that we would like jest to ignore. The only way we've found to do this currently is to replace the test script with one of our own. While this isn't a big deal, it would be nice to be able to ignore these directories within our package.json
We're facing this with a Cordova app where Jest is trying to run tests in the plugins folder so a solution to this would be welcome: even if it means using a custom script (but not if it means ejecting!) Can you share your custom script please chrisdrackett?
for now we've just used https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/scripts/test.js practically verbatim.
I have a .vim dir inside the project root, when I run npm test and select a(all), jest scan the .vim dir:
PASS src/App.test.js
FAIL .vim/.vimundo/%src%src%App.test.js
● Test suite failed to run
SyntaxError: /Users/fzx/adev/ra/demo/.vim/.vimundo/%src%src%App.test.js: Unexpected character '�' (1:3)
> 1 | Vim�UnDo��'�Jx�Z2��@o'��0��~��_hhh�H
| ^
2 | """"Xh�b_�����Xh��5�_�����Xh��� � 5�_�����==V=Xh�3�
3 | � 5�_����==V=Xh�:�
4 | import ''5�_� ����==V=Xh�<�
at Parser.pp$5.raise (node_modules/.6.14.1@babylon/lib/index.js:4246:13)
at Parser.getTokenFromCode (node_modules/.6.14.1@babylon/lib/index.js:1065:10)
at Parser.readToken (node_modules/.6.14.1@babylon/lib/index.js:694:19)
at Parser.<anonymous> (node_modules/.6.14.1@babylon/lib/index.js:6435:20)
at Parser.readToken (node_modules/.6.14.1@babylon/lib/index.js:5370:22)
at Parser.nextToken (node_modules/.6.14.1@babylon/lib/index.js:684:19)
at Parser.next (node_modules/.6.14.1@babylon/lib/index.js:609:10)
at Parser.pp$3.parseIdentifier (node_modules/.6.14.1@babylon/lib/index.js:4131:8)
at Parser.pp$3.parseExprAtom (node_modules/.6.14.1@babylon/lib/index.js:3484:21)
at Parser.parseExprAtom (node_modules/.6.14.1@babylon/lib/index.js:6402:22)
Are there not CLI flags your can pass to npm test to handle this? ARGS after npm test -- ... are passed to jest. There's also a custom config file you can use, https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/utils/createJestConfig.js#L18
Closing as a duplicate of https://github.com/facebookincubator/create-react-app/issues/544.
We should just fix that instead.
While this issue was based upon ignoring a folder outside src, I have a very similar use case regarding a file/folder _inside_ src. My solution has been exactly the same as @chrisdrackett: to copy test.js and modify it to allow exclusions, which is not very DRY or ideal.
Because of that use case, I think it is relevant to the original request of this issue, to have the ability to add folders (and files) to have Jest ignore, and subsequently I don't think that this is necessarily a duplicate of #544. I would still like to see some sort of ignore/exclude functionality that applies not only to files/folders outside src, but inside as well.
Can you explain more about your use case? The comment about submodules mentioned they're outside src.
Actually, my use case is better exemplified by #1455, so I'll defer to that issue instead of continuing discussion in this one.
@gaearon It'd be great to be able to exclude files from jest. E.g., if I have some test lib with functions commonly used by differennt tests, I can't put it in the __tests__ directory, because jest will complain that a test suite must export at least one test.
We are only running tests inside src in next release if it helps. You can try the next release with canary version of react-scripts.
I'm not sure what you mean by your question though. A specific example would help.
@gaearon
> src/__tests__
testA.test.js
testB.test.js
data.json
normalised-test-data.js
testA: import data from './normalised-test-data.js; ...,
testB: import data from './normalised-test-data.js; ...,
normalised-test-data:
import data from './data.json';
export default const normalisedData = data.filter((el, i, arr) => err.indexOf(el) === i);
FAIL src/__tests__/normalised-test-data.js
- Test suite failed to run
Your test suite must contain at least one test.
Right, but why do you need to put normalised-test-data.js into __tests__? You can put it outside of the folder.
Can you explain more about your use case? The comment about submodules mentioned they're outside src.
@gaearon how would you go about handling submodules within the src file? Our use case is that the submodule runs tests in a different testEnvironment than the main React app (node instead of jsdom because we've encountered some issues with request headers in the jsdom environment).
We're using old react-scripts 0.9.5 right now, which uses an older version of Jest that does not support specifying different testEnvironments seen here
I'm completely open to moving the submodule outside of the src and then excluding it from the coverage report as mentioned in #1455, if thats the case, what would be the CRA recommended folder for that?
Hi i'm having a similar issue, I have fakers inside the __tests__ folder, I believe they belong there. It would be awesome to have a way to ignore a folder or files.
Right now I'm planning on solving this by moving them outside the __tests__ folder.
An easy approach could be ignoring files with a particular key. Like file.test.js but the opposite.
Maybe something like myFile.ignore.test.js, or someone with more creativity can think of a better one.
Thanks a lot!
Most helpful comment
Right, but why do you need to put
normalised-test-data.jsinto__tests__? You can put it outside of the folder.