-
-
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
Inline snapshots are not written to js files containing JSX syntax when the file is located in a subfolder #11741
Comments
Hey @JimMalmborg! Thanks for providing a detailed description along with repo links. All 3 tests successfully produce inline snapshots in a properly configured environment here, so I do not believe this is a bug in Jest. For troubleshooting help, please see the tips I gave another user reporting a similar problem in #11730 |
Yes, that might be the case. But I did try to set up the replit env according to the jest getting started documentation + babel from scratch, super basic, and almost zero configuration. Also, changing the version back to 26.6.3 solved the issue, that's is why I assumed it was a bug. Do you have a repo with a properly configured environment I can check? |
Hm, I did get my examples to work by just renaming
Update: I did get my tests to pass by just renaming my Anyway, feel free to close the issue if you still think it's not a bug. |
Yeah don't worry, it's not unusual to make that assumption. But what many seem to not consider well enough is that packages in the NPM ecosystem tend to follow semantic versioning, and a bump in major version (e.g. 26.x to 27.x) signals potential breaking changes. This can for example be directly in the package you are using and/or related to dependencies.
Good job! 🙌
Hmm...I might have to eat my words when it comes to "properly configured environment". When you said that In parts of the Babel docs these are mentioned in a way that makes them sound like they are treated equally. And the Jest docs do tell you to use You are also right in that the error appears to happen even when the test itself is correctly transformed. Which, upon second thought, I agree seems odd. It might suggest a discrepancy in config loading. So I took a look at how Jest calls Babel to parse into AST for building the inline snapshot: Notice how the When Babel then attempts to build a config chain, it first tries to resolve what it should treat as the root directory of the project. This is determined by an option called Now I do suddenly understand what you meant with things working fine if the tests are 'not in a subfolder' - as in; at the root level of the project. That makes sense, because if the test and And the reason it works with the So this is starting to look like a potential bug to me. |
@mmkal - could I pick your brain on this one? The code in question was introduced in (the rather impressive 😅 ) PR #7792 that you worked on. Was there a reason (that you can remember) behind setting If I delete the line which sets the Looking at this, I wonder if perhaps the test has a flaw when you consider how Babel looks for a I.e. unless a dummy |
@sigveio I'm afraid I might not be much help here. I'm not very familiar with babel (I learned a lot of what I know about it writing that PR - and that was three years ago!). However I did restore the deleted branch so I could find the blame for individual commits on that branch: https://github.com/mmkal/jest/blame/uglier-inline-snapshots/packages/jest-snapshot/src/InlineSnapshots.ts#L93 From the blame, it looks like @jeysal committed the line in question so may be able to comment on whether it's safe to remove. |
Pretty sure I didn't write that, but I moved a lot of the code around in a hundred of rebases while the PR was open and I had taken over :P Anyway, yeah it probably doesn't make sense the way it is set up, but I'm also not sure defaulting As an alternative solution: I don't have time to work on Jest at the moment, but either of those could work if someone wants to try it 😅 |
Thanks a lot for such a swift follow up, both of you!
I feel you - I learn much even just from trawling through the code trying to understand issues like this. Being able to tap into such a wealth of collective skill and knowledge is one of the things I truly love about open source. 😌 And hehe, yeah, I thought it might be a bit of a stretch asking anyone to remember such a specific detail from so long ago... especially considering the size of the PR and how many people were involved. I got sweaty just from trying to read through it all. 😅
Perhaps - that was mainly to poke at it and see how the basic reproduction together with the test suite from #7792 would react to it.
That's an interesting idea! 🤔
The thing that comes to mind for me would be angle brackets being ambiguous in certain settings between Flow, TS, and ES. With Flow, the Babel parser by default handles it as an ES-style comparison unless the source starts with a With TS the legacy type assertion could be problematic, but the TS syntax parser supports forcibly enabling JSX parsing. Could we do that and expect people to use the much more compatible |
The file extension-based system we already have should continue to work good enough for TS. If Flow also has these conflicts (I wasn't aware of that then that could make things difficult and maybe the former option of trying to use the existing Babel configs makes more sense.
Haha, you probably didn't even see the preceding PRs I had to build and land first to enable #7792 to go in without causing a massive performance hit and out-of-memory errors on CI 😂 |
Yeah
Haha, I can imagine! Funnily enough I had already stumbled across that in other contexts, and it (along with the related sandbox escape hatch) was interesting reading! Good stuff! 🙌 |
Not wanting to derail this conversation, but I just came across this issue when attempting to write inline snapshots (external snapshots work fine) - however, I didn't have any babel config (using ts-jest), and until I wanted to do inline snapshots all was working as expected. I have now created a babel config with the setup discussed and it works - why does inline snapshotting require a babel config to work? |
Just tried inline snapshot inside a workspace of a monorepo and it looks like I hit this issue. Is there anyone working on it? I can stick to regular snapshots for a moment... |
We use a transform in our jest config and it just isn't working for inline snapshots when they need to be written. Adding a I tried adding sourcemaps per #6744 but that did not help. Here's a repro guide (I'll see if I can put it together as a runnable example later):
|
It's worth noting that this bug prevents you from being able to update inline snapshots in files using flow syntax - locking that entire language out of using v27 if they want to update inline snapshots without resorting to the hack above. |
Requiring one from the other gave me the babel error
but copy pasting all my babel.config.js to .babelrc.js worked :( Are we waiting for a PR to be made to set |
FYI Create React App v5 is affected by this same bug, it's tracked here facebook/create-react-app#11928 |
Are there any updates? |
Will upgrading to jest v28 resolve this problem? |
confirmed this is still an issue with jest 28. the .babelrc.js work around fixed the issue for me |
Missed this one, sorry. Any reason why we cannot pass then at least jest should pick up all the user's config |
No problem. Thanks for helping. |
Is there anyone specific that we need to ping to help move this forward? |
Good catch, thanks! |
Now with jest v29 the workaround stopped working (creating a .babelrc.js) and deleting that file has no effect so any attempt to update a inline snapshot gets this error:
Do I have to do something to take advantage of the bugfix? |
That seems weird, could you open up a separate issue with a reproduction? |
Sorry false alarm, the problem was in our repo, the repro allowed me to track down the issue. |
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. |
💥 Regression Report
Inline snapshots are not written to JavaScript files containing JSX syntax when the file is located in a subfolder. It works fine if the file is at the root of the project. It also works fine for TypeScript files in both root and subfolders.
Last working version
Worked up to version: 26.6.3
Stopped working in version: 27.0.0
To Reproduce
Steps to reproduce the behavior:
./src/inline-snapshot.spec.js
Expected behavior
The inline snapshot should be written to the test file.
Link to repl or repo (highly encouraged)
https://replit.com/@JimMalmborg/inline-snapshot-regression?v=1
https://github.com/JimMalmborg/inline-snapshot.git
Run
yarn
andyarn test
Note! Manually clear any existing snapshots from the test files before running the tests.
Run
npx envinfo --preset jest
Paste the results here:
The text was updated successfully, but these errors were encountered: