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

Have separate snapshot files for branching behavior at compile time #1656

Closed
ghost opened this issue Sep 9, 2016 · 2 comments
Closed

Have separate snapshot files for branching behavior at compile time #1656

ghost opened this issue Sep 9, 2016 · 2 comments

Comments

@ghost
Copy link

ghost commented Sep 9, 2016

Do you want to request a feature or report a bug?
Feature Request

What is the current behavior?
Currently, I have Webpack inject some global constants at compile time to produce different builds depending on environment. This affects styling, what components are rendered, etc. If I run snapshot tests with these constants in Jest's globals config "globals": { ... }, previous snapshots are invalidated whenever I re-run the test with different global values, because the output is different. It would be great if separate snapshot files can be created and be used to check against later snapshot tests based on the globals or some other configuration.

@cpojer
Copy link
Member

cpojer commented Sep 9, 2016

We don't really have a way of doing this on the framework level and I don't think we need to. I recommend you to make your globals injectable and then write your tests like so:

it('works with this config', () => {
  MyStyles.inject({stylesA});
  expect(…).toMatchSnapshot();
});
it('works with that config', () => {
  MyStyles.inject({stylesB});
  expect(…).toMatchSnapshot();
});

Alternatively, you can also reset modules and redefine your globals:

it('works with this config', () => {
  jest.resetModules();
  globals.styles = MyStyles;
  const React = require('react');
  const MyComponent = require('MyComponent');
  expect(…).toMatchSnapshot();
});

Both of these options should be sufficient without introducing complexity to Jest.

@github-actions
Copy link

github-actions bot commented Jun 2, 2021

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.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 2, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant