jest icon indicating copy to clipboard operation
jest copied to clipboard

Jest detects linux .snap package file as obsolete snapshot

Open nahuelarjonadev opened this issue 6 years ago • 13 comments

Current behavior

when running Jest, it's detecting a .snap package I built for Linux as an obsolete snapshot, and it's causing my tests to fail. I tried ignoring the folder with no success.

Part of the test output:

[1] Snapshot Summary
[1]  › 1 snapshot file obsolete from 1 test suite. To remove it, run `yarn run test:main -u`.
[1]    ↳   • release/kafka-lens_2.0.0_amd64.snap
[1] 
[1] Test Suites: 1 skipped, 2 passed, 2 of 3 total
[1] Tests:       2 skipped, 6 passed, 8 total
[1] Snapshots:   1 file obsolete, 1 passed, 1 total
[1] Time:        4.173s
[1] Ran all test suites matching /main/i.
[0] Snapshot Summary
[0]  › 1 snapshot file obsolete from 1 test suite. To remove it, run `yarn run test:renderer -u`.
[0]    ↳   • release/kafka-lens_2.0.0_amd64.snap
[0] 
[0] Test Suites: 10 passed, 10 total
[0] Tests:       37 passed, 37 total
[0] Snapshots:   1 file obsolete, 10 passed, 10 total
[0] Time:        4.171s
[0] Ran all test suites matching /client/i.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
[0] yarn test:renderer exited with code 1
[1] yarn test:main exited with code 1
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

package.json scripts:

"test": "concurrently \"yarn test:renderer\" \"yarn test:main\"",
"test:renderer": "jest client",
"test:main": "jest main"

Folder Structure

client
|--src
   |--components
   |--__tests__
      |--__snapshots__
         |--component.jsx.snap
|--...
main
|--...
release
|--kafka-lens_2.0.0_amd64.snap
|--...
jest.config.js
package.json

jest.config.js

module.exports = {
  verbose: true,
  snapshotSerializers: ['enzyme-to-json/serializer'],
  setupFiles: ['./setupTests.js'],
  moduleNameMapper: {
    '\\.(css|less|scss)$': '<rootDir>/client/src/app/__mocks__/styleMock.js',
    '\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
      '<rootDir>/client/src/app/__mocks__/assetsTransformer.js',
  },
  moduleDirectories: ['node_modules'],
  modulePaths: ['<rootDir>'],
  testPathIgnorePatterns: ['<rootDir>/release/', '<rootDir>/node_modules/'],
};

Expected behavior

Being able to ignore the path to the release folder, so Jest doesn't try to parse any files inside it as a snapshot

envinfo

System:
    OS: Linux 5.0 Ubuntu 18.04.3 LTS (Bionic Beaver)
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
Binaries:
    Node: 10.16.2 - ~/.nvm/versions/node/v10.16.2/bin/node
    Yarn: 1.17.3 - ~/WebstormProjects/kafka-lens/node_modules/.bin/yarn
    npm: 6.9.0 - ~/.nvm/versions/node/v10.16.2/bin/npm
npmPackages:
    jest: ^24.1.0 => 24.9.0

nahuelarjonadev avatar Sep 06 '19 16:09 nahuelarjonadev

Could you provide a minimal repro, so it's easier to investigate?

thymikee avatar Sep 08 '19 09:09 thymikee

The logic lives here: https://github.com/facebook/jest/blob/0935f7c2cd7780e47365511e35efc829ba359df4/packages/jest-snapshot/src/index.ts#L132-L133

Not sure how to handle it... #8665 added the logic that if the resolved test file is ignored, then we ignore the snap file. Maybe we can tweak it to cover your use-case as well? Thoughts?

SimenB avatar Sep 08 '19 15:09 SimenB

Thanks to both! I was trying to find the code which looked for the snap files, but I couldn't find it on my own. I'll give it a look and maybe I can think of a solution.

Why don't just ignore snap files inside testPathIgnorePatterns?

const list = files.filter(snapshotFile => {
   const testPath = snapshotResolver.resolveTestPath(snapshotFile);

   // ignore snapshots of ignored tests
   // here, instead of evaluating if the 'associated test file' is ignored (which makes no sense for a .snap file that's not a real snapshot) also ignore the snapshot if it falls under the testIgnorePatterns defined by the user.
   if (testIgnorePatternsRegex && testIgnorePatternsRegex.test(testPath)) {
     return false;
   }
   ...
}

nahuelarjonadev avatar Sep 09 '19 05:09 nahuelarjonadev

I'm also running into this issue. Since I'm using CRA, I cannot add testPathIgnorePatterns without ejecting. However, I see that the Jest config in CRA has a testMatch configuration as follows:

    testMatch: [
      '<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}',
      '<rootDir>/src/**/*.{spec,test}.{js,jsx,ts,tsx}',
    ],

The file that is causing me headaches is in a dist folder and it's clearly not associated with a test with a name like dist/graasp-desktop_0.14.0_amd64.snap.

Perhaps it can be ignored if not within the testMatch pattern?

juancarlosfarah avatar Oct 26 '19 10:10 juancarlosfarah

I just ran into this as well. I like @nahuelarjonadev's suggestion of ignoring snap files in testPathIgnorePatterns. This is actually the first thing that I tried as a workaround, and was sad that it didn't work.

Any chance that this can be implemented?

murrayju avatar Jun 30 '20 07:06 murrayju

Similar issue here, I got two types of test files (basically one unit, one much slower using storyshots) and as such I have a variable testMatch config.

For example: TestA has a snapshot, TestB has a snapshot

When running TestA and TestB at the same time, no issue. But if I change the testMatch to only include TestA, Jest will complain that TestB's snapshot is obsolete as it's no longer used.

What would fix it for me: when a .snap file is found, resolve it to the test file and only report it as obsolete if that test file has been run. If the test file hasn't been run, it's either not a Jest snapshot file or it's not supposed to be included in this test run.

StephanBijzitter avatar Sep 10 '20 21:09 StephanBijzitter

Similar issue -- Postgres creates .snap files, and so far I've found no combination of roots, testPathIgnorePatterns or other settings that will make jest ignore these files if they exist below the project root. These files are reported as obsolete snapshots and fail the jest test. Innocently running jest -u deletes the DB files. Bad surprise.

What's needed:

1 -- ignore files named *.snap unless they are Jest snapshots; don't show as obsolete; don't delete via -u; etc.

2 -- respect the various path parameters (testPathIgnorePatterns, modulePathIgnorePatterns, roots, probably others I'm not aware of.

3 -- provide a big-hammer config option disableSnapshots that if set, doesn't look for or operate on snapshots at all

chrisj-back2work avatar Jan 11 '22 14:01 chrisj-back2work

I found a workaround that works fine for my case by using custom snapshotResolver. Basically you need that your resolveTestPath method returns a valid test file for given snapshotFile, then snapshot won't be considered obsolete.

davispuh avatar Nov 18 '22 15:11 davispuh

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Nov 18 '23 16:11 github-actions[bot]

still an issue

Jelmerro avatar Nov 24 '23 22:11 Jelmerro

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Nov 23 '24 23:11 github-actions[bot]

still an issue. Custom snapshot resolver doesn't help even though it logs the paths correctly

Sahabial avatar Nov 28 '24 13:11 Sahabial

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Nov 28 '25 14:11 github-actions[bot]