Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Jest Snapshot testing with dynamic Id

I am facing attached error while performing snapshot testing. The error is basically for dropdown and datepicker of kendo controls.

Please help.

c

like image 581
Parag Pathari Avatar asked Nov 17 '18 18:11

Parag Pathari


People also ask

How do you write snapshot tests for React components with Jest?

This is the structure for the test: import React from "react"; import renderer from "react-test-renderer"; import App from "./App"; describe("Jest Snapshot testing suite", () => { it("Matches DOM Snapshot", () => { const domTree = renderer. create(<App />). toJSON(); expect(tree).

Should I commit Jest snapshots?

Yes, all snapshot files should be committed alongside the modules they are covering and their tests. They should be considered part of a test, similar to the value of any other assertion in Jest. In fact, snapshots represent the state of the source modules at any given point in time.

Is snapshot testing worth it?

Snapshot tests are easy to create and maintain, and they are a great way to check that your application's behavior isn't changing unexpectedly during development.

Does React testing library have snapshot?

Snapshot testing It works well with React components because when you render a component you can view the DOM output and create a “snapshot” at the time of run.


1 Answers

The way I'd approach this problem is with Jest Property Matchers, so you can expect any string. That way, dynamic strings will still pass.

Without a code sample from the OP it's not really possible to suggest a concrete implementation, but from a testing perspective this will be much easier than trying to inject a UUID into every component that requires it.

like image 156
Eric Dauenhauer Avatar answered Nov 15 '22 04:11

Eric Dauenhauer