jest-community / vscode-jest

The optimal flow for Jest based testing in VS Code
MIT License
2.84k stars 293 forks source link

vscode-jest not finding snapshot when using custom jest snapshotResolver #587

Open tostringtheory opened 4 years ago

tostringtheory commented 4 years ago

Environment

  1. node -v: v12.14.0
  2. npm -v: 6.13.4
  3. npm ls jest or npm ls react-scripts (if you haven’t ejected): -- jest@26.0.1
  4. your vscode-jest settings if customized:
    • jest.pathToJest? n/a
    • jest.pathToConfig? n/a
    • anything else that you think might be relevant?

in jest.config.js:

snapshotResolver: "<rootDir>/scripts/testing/snapshot-resolver.js"

in scripts/testing/snapshot-resolver.js:

module.exports = {
    testPathForConsistencyCheck: "some/example.spec.js",

    resolveSnapshotPath: (testPath, snapshotExtension) => testPath.replace(/\.spec\.([tj]sx?)/, `${snapshotExtension}.$1`),

    resolveTestPath: (snapshotFilePath, snapshotExtension) => snapshotFilePath.replace(snapshotExtension, ".spec")
};
  1. Operating system: Windows 10 v2004 build 19041.264

Prerequisite

Steps to Reproduce

Open repository: https://github.com/tostringtheory/vscode-jest-snapshot-bug Run yarn install | npm install

If you run yarn test / npm run test, then the test succeeds, and does not fail to resolve the snapshot. However, while the vscode-jest extension sees the test was successful, it still reports that the snapshot is missing:

image

Relevant Debug Info

Enabled the "jest.debugMode", yet there were no errors/issues. I tried to determine which repository would be the owner, but couldn't exactly see where the issue would lie. I see that the jest-community/jest-editor-support seems to reference the necessary property (snapshotResolver) in the project config. However, the vscode extension doesn't appear that it even looks for snapshots (at least when I searched the repository), so I'm not even sure that the issue belongs here. Instead of spinning my wheels hunting, I figured I'd put in an issue and see if someone with more experience/knowledge on this structure could identify the culprit.

Expected Behavior

The same functionality with default snapshot resolution in vscode-jest works with a custom snapshot-resolver. IE, the snapshot would not indicate that it is missing, and CodeLens would be able to peek to the definition.

Actual Behavior

The method call is decorated with a "snapshot missing" statement, even though the test is succeeding.

tostringtheory commented 4 years ago

Just checking in on this, could someone at least point me to whether this is an issue with this extension or the tools it builds upon? As mentioned above I tried to hunt it down but couldn't find it in my initial searches if this was relevant to this repository or the jest IDE integrations repository. I'm happy to create a PR myself in a repo resolving this.

connectdotz commented 4 years ago

@tostringtheory it does look like it is a problem on our end, a quick investigate led me to believe that the SnapshotCodeLensProvider didn't pass jest config to the jest-editor-support that could find snapshot via snapshotResolver...

We have stopped parsing jest config a while back thinking there is really no need for it. But looks like you just found one use case! Maybe we need to revisit this decision 🤔 ...

tostringtheory commented 4 years ago

@connectdotz - ahh, that's why I was confused. I thought from what I was looking at in the jest-editor-support package that it should handle it, and didn't see anything specific to vscode-jest that needed it (looked like it was just consuming jest-editor-support). Didn't dawn on me that the issue would be not passing configuration. You appear to be correct too, the config looks to be the 3rd argument to the ctor.

I just checked in the SnapshotCodeLensProvider commit history, and actually don't see that the jest.pathToConfig was ever even passed to the Snapshot object.

Would you want me to try and take a crack at a PR for this? I've not done vscode extensions work so I might have some slight environment work to prep, but it looks trivial, unless there's caveat's i'm not aware of (dragons?).

connectdotz commented 4 years ago

@tostringtheory sorry for the late reply, great spirit 👍 , yes I am open for discussing this fix ... just vaguely remembered that there were quite a few issues surrounding getting config from jest before, but maybe something has changed and it won't hurt to have a fresh perspective to reexamine this again.

luca-borrione commented 3 years ago

Any progress on this? I am facing the very same issue

Yimiprod commented 1 year ago

Having the same issue without a custom snapshotResolver here. Debug mode is not much help, only get errors like this ones:

image image