-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Test file locations #4
Conversation
This means there should be a |
One advantage of having the tests in the same source tree as the app code is that when using typescript the tests do not need a separate tsconfig.json or a separate build step to verify them etc. Since they are part of the app code, just build the app and the tests are also built. Now it becomes a matter of deciding if you want to the test-runner to run the compiled tests in In this monorepo, some packages are referring other packages code. So in order for the tests to work in the first place, the referred packages needs to be built (or at least I think that is the case). So maybe it makes sense to also build the package being tested before testing it. |
Now the problem with running the compiled tests under |
Seems like the choices we have for snapshot files are that we either:
In a non-monorepo I think (1) is the simplest option. However since this is a monorepo, the only code that can be tested as |
I did an experiment with the snapshot resolver option. It seems to work well, we can run tests in lib/ and it writes/read snapshots in src/. However I did discover that running tests in |
I went back to using ts-jest and putting each package as a separate project with its own config. This was the only way I could make ts-jest use the tsconfig.json for each package. I like this setup, the only drawback is that we cannot run tests for one project only this way. See this issue. So in order to run for one project/package we need to put separate jest.config.js in each package instead of having a single root jest.config.js. |
This PR will re-organize the location of the test files.
It seems like jest has a convention/recommendation of putting the tests in the same source tree as the app files under a folder named
__test__
or alongside the app files likefoo.ts
andfoo.tests.ts
. I have not found an offical recommendation but there are several places this is mentioned like here and here. Also looking at the jest source itself, it uses this pattern for example here.Initially I tried to keep all the tests separated into their own folder but to do that we need to fight how jest was intended to be used so I've come to the conclusion it is better to follow the convention. See this issue in jest for some info about the issues in putting tests in an isolated folder.
One interesting thing about having the test files in the
src
folder is that they will get built to thelib
folder and therefore if we are not careful they will also be published to npm. I looked into how the jest source avoids this and it seems they have a.npmignore
file that excludes the test, like here. We currently use package.jsonfiles
filed to whitelist published files instead of blacklisting in.npmignore
. This blog post saysfiles
is better, but under the heading "The one time npmignore is ok" it also specifcially mentions the__test__
folder and recommends having both files and .npmignore in that case.