Skip to content
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

Jest detects linux .snap package file as obsolete snapshot #8922

Open
nahuelarjonadev opened this issue Sep 6, 2019 · 10 comments
Open

Jest detects linux .snap package file as obsolete snapshot #8922

nahuelarjonadev opened this issue Sep 6, 2019 · 10 comments

Comments

@nahuelarjonadev
Copy link

nahuelarjonadev commented Sep 6, 2019

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
@thymikee
Copy link
Collaborator

thymikee commented Sep 8, 2019

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

@SimenB
Copy link
Member

SimenB commented Sep 8, 2019

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?

@nahuelarjonadev
Copy link
Author

nahuelarjonadev commented Sep 9, 2019

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;
   }
   ...
}

@juancarlosfarah
Copy link

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?

@murrayju
Copy link

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?

@StephanBijzitter
Copy link
Contributor

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.

@chrisj-back2work
Copy link

chrisj-back2work commented Jan 11, 2022

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

@davispuh
Copy link

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.

Copy link

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 github-actions bot added the Stale label Nov 18, 2023
@Jelmerro
Copy link

still an issue

@github-actions github-actions bot removed the Stale label Nov 24, 2023
naporin0624 added a commit to pixiv/charcoal that referenced this issue Feb 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants