-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Configurable snapshots directory for colocated tests #1650
Comments
snapshots are colocated with tests and they are definitely not meant to be for machine readability. They are meant to be reviewed by humans, both the author of a test and the code reviewers. |
Jest also ensures that there are never any stale snapshots in the system. This means we both need a way to map from a snapshot to its associated test and back. This seems kinda hard to configure via string values in the JSON config :( |
Thanks for the quick reply! I understand that the snapshots are meant to be read and reviewed. It just seems a bit messy to have In the meantime, I've just started dropping tests into I also understand this is a bit hard to configure. My thought was to define some top level directory for It might just be me that finds this messy. Curious to hear from others that colocate tests with components and have multiple directories of components. |
I don't really quite understand what you mean. The snapshots are colocated with tests and there is a 1:1 mapping between |
Currently my directory looks a bit like this:
The As an alternative, my thought was a directory structure like this might be easier to understand/comb through:
|
but that is the opposite of colocating snapshot files with their tests, isn't it? |
I'm proposing that the snapshots don't necessarily need to be colocated with tests. I find it more important that tests are colocated with components. Having all three colocated feels messy, mostly due to the several |
I'm not religiously opposed to this if you want to do all the work necessary to support this, find a way to make this maintainable and test it well. |
We can also help on our discord channel and happy to review code, of course :) |
I think my main argument is that the snapshots are auto generated and aren't meant to be modified manually, nor really imported directly into the codebase. So it doesn't feel right to have them intermixed within the code which you are actively editing/maintaining/importing. I'm not particularly married to this idea either :) Just wanted to propose it to see what others thought. If I'm not alone in this, I may take the time to mock up a PR and see what it might look like. |
I'm sure you find many people that share your opinion. Testing is like a religion to people and I'm happy to support all kinds of doing this as long as it integrates well with Jest and is well tested itself. |
This is something where we'll need some help from the community. I think a good way to support this is by adding a config option
this way we can call these methods to resolve the snapshot path from a test path and the other way round and it will enable use to keep snapshot files consistent. |
I'm gonna close this given the inactivity. If somebody would like to work on this, I'm happy to reopen this and review a PR :) |
I have the same problem as #1653. preprocessor approach is not good in case of
Well, as a workaround, |
👀 |
Actually at this point I don't really think I want to support this feature. It adds unnecessary complexity and gives almost no value. |
So. How did end it?. I really like to have each test with its component. But I'd prefer to get all snapshot file in a unique folder, not around the code. |
Yeah, I think would be good to have this feature. |
And I don't like either to replicate the project structure on test folder... |
+1, I would also like to have all snapshots moved to one folder. |
Cool, I'll try to work on it this weekend and propose a PR. Then we move this discussion there. |
Great! Let me know if I can help you. I've been thinking about complexity. The main problem is what happens if there are Test with same file name. Probably best option would be to get relative path of test file and replicate folder structure on snapshot folder. |
@lucasfeliciano @luispuig Any progress on this? Do yout want some help with something? |
Let's say I have service A and service B that expose identical REST API. I want tests for both services and I want to use snapshots feature to store some filtered JSON response returned by service A and service B. Let's say I have written tests once that may hit one service at time (A or B). I can run them against service A, get snapshots, then I can run them against service B and get new snapshots. It turns out that service A and service B may produce different snapshots for some known reasons. So it sounds reasonable to specify different snapshots locations depending on what service is tested. Because I can't do it through Jest config, I have to maintain copy of the same test stored in the different folder, but I really want to have just one test and two different snapshots. I hope this sounds like a real use case to get it supported. I can see it is a rare case, but I think it is related to other cases that people brought here. Thanks! |
Looks like there's no progress on this. I would love to try working on it, since I need this feature badly |
@cpojer Disagree, for React Native projects, which we can have more than one platform, is really useful. Check the rendering of the component on iOS, Android, and whatever else show up. Just a way to override the 'SNAP' extension would help a lot already. |
This is really unfortunate if you're using a setup where you have source files (e.g. TypeScript) which are checked in and output files (e.g. the corresponding JS) which aren't checked in. The snapshot files end up in a normally- |
So much clutter without this function... |
Snapshots are driving my project. Without this function I’m forced to build my own solution, which eliminates the need of using Jest... Bummer. |
I don't like the idea of having "files.exclude": {
"**/__snapshots__": true
} This way |
I spent a few hours looking into this, put up a PR #6143 based on a previous suggestion for anyone that's interested. |
Seems like you are moving foward with this @viddo. Thanks for your work on this, I'd really like to see this get merged. |
Thanks @viddo! 😍 |
This has landed and will be available in Jest 24. It's currently available by installing |
In case others find it helpful, here is the contents of my
This results in
|
If you want to place test files ( // jest.config.js // snapshotResolver.js module.exports = { resolveTestPath: (snapshotFilePath, snapshotExtension) => testPathForConsistencyCheck: 'src/components/some.test.js', |
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. |
The React community seems to be moving towards colocating tests with components. I appreciate the changes to the default
testRegex
that looks for any.test.js
file.However, using this colocation method means a bunch of scattered
__snapshots__
directories throughout the codebase. Since snapshots seem to be more for machine readability than human readability, I would tend to throw them all in a single top-level directory (like you might have for build/dist files). What are your thoughts on making the snapshots directory configurable?The text was updated successfully, but these errors were encountered: