Jest: Bypass check for obsolete snapshots

Created on 15 Nov 2017  Â·  4Comments  Â·  Source: facebook/jest

Hi,

I use Jest in the context of Storyshots/Storybook.

I use Storyshots to run tests against my components, and I actually do so for many languages in a row. I want to ensure all components render fine, for all languages.

I don't want to generate Jest snapshots for all languages, because it would not be very useful + I would need to store snapshots for all languages, so I decided to run my tests with snapshots for english languages only.

So to sum up: run same test twice for EN and FR, and only use snapshots for EN language.

What is the current behavior?

When I test with english language it works fine and generate snapshots, and match against snapshots as expected

When I test afterward with french language and I don't call expect().toMatchSnapshot(), it seems Jest does not say there is a snapshot mismatch (as expected), but it reports that the snapshots previously generated for french language are obsolete:

› 2 obsolete snapshots found, run withnpm run storyshots-fr -- -uto remove them.

For info I use the renderOnly feature of storyshot which code is basically very simple and just does the rendering:

export function renderOnly({ story, context }) {
  const storyElement = story.render(context);
  renderer.create(storyElement);
}

What is the expected behavior?

I don't want to remove these snapshots, I want them to be kept for the next time I run the english test, and ignore this error for french language. I think there should be an option to ignore obsolete snapshots or completely disable snapshots from jest CLI to handle such usecase.

Most helpful comment

But why close it? this is still an issue

All 4 comments

I don't think we'll be able to make this obsolete. Jest provides guarantees that your snapshots in the repo always stay consistent. I'm wondering if there is a way you can change your approach to make this work properly. What is the value of running the tests twice but not storing snapshots for one of the test runs? That means you aren't really testing anything.

@cpojer during both FR/EN tests, the components render the same tree, the only thing that changes is the translated texts (we don't have specific render path per locale). I could use snapshot testing for both locales but don't really see the value.

As I said, I already have snapshots for EN and it's good enough to prevent refactoring errors. I want to also render all components in FR (without trying to match snapshot) and the only purpose is to validate that all my React-intl / ICU keys are valid syntaxically for the FR locale, ensuring at build time there is no translation typo. A typo in one language bundle can be left unnoticed and would fallback to english / translation key displayed (or worse, render errors) in production, while simply doing a render of all languages can prevent that.

Anyway, I just found a workaround that is good for my usecase: instead of running the same test twice with different env variable, I just duplicate it on 2 tests, one for each locale hardcoded with different settings:

image

But why close it? this is still an issue

Was this page helpful?
0 / 5 - 0 ratings