Shallow rendering renders only component itself without its children. So if you change something in a child component it won’t change shallow output of your component. Or a bug, introduced to a child component, won’t break your component’s test. It also doesn’t require DOM.
Jest snapshots are like those old text UIs with windows and buttons made of text characters: it’s a rendered output of your component stored in a text file.
You tell Jest that you want to be sure that output of this component should never change accidentally and Jest saves it to a file that looks like this:
Every time you change your markup Jest will show you a diff and ask you to update a snapshot if the change was intended.
Jest stores snapshots besides your tests in files like __snapshots__/Label.spec.js.snap and you need to commit them with your code.
Awesome interactive watch mode that reruns only tests that are relevant to your changes.
Helpful fail messages.
Simple configuration.
Mocks and spies.
Coverage report with a single command-line switch.
Active development.
Impossible to write silently wrong asserts like expect(foo).to.be.a.function instead of expect(foo).to.be.a('function') in Chai because it’s the only natural thing to write after (correct) expect(foo).to.be.true.
snapshotSerializers allows you to pass Enzyme wrappers directly to Jest’s snapshot matcher, without converting them manually by calling enzyme-to-json’s toJson function.
Create a test/jestsetup.js file to customize Jest environment (see setupFiles above):
For CSS Modules also add to jest section in your package.json:
And run npm install --save-dev identity-obj-proxy.
Note that identity-obj-proxy requires node --harmony-proxies flag for Node 4 and 5.
You can simulate an event like click or change and then compare component to a snapshot:
Sometimes you want to interact with an element in a child component to test effect in your component. For that you need a proper DOM rendering with Enzyme’s mount method:
You may see an error like this when you run your tests:
console.error node_modules/fbjs/lib/warning.js:42
Warning: React depends on requestAnimationFrame. Make sure that you load a polyfill in older browsers. http://fb.me/react-polyfills