You can test styles, although snapshot tests, but Jest does not support evaluating component styles through statements, that is, through expect .
To do this, you need to combine Jest with the enzyme , chai and chai-farme .
This combination will allow you to write tests, like this simple example:
it('should render style', () => { chai.expect(shallow( <div style={{ left: '4rem' }} /> )).to.have.style('left', '4rem'); });
First create the installation file and add it to the jest.setupFiles array in package.json. For an overview of this option, see the Jest documentation .
This is my settings file:
// test/setup.js import React from 'react'; import chai from 'chai'; import chaiEnzyme from 'chai-enzyme'; import { shallow, render, mount } from 'enzyme'; // Add commonly used methods and objects as globals global.chai = chai; global.mount = mount; global.React = React; global.render = render; global.shallow = shallow; chai.use(chaiEnzyme());
This is my .json package:
{ "jest": { "setupFiles": [ "./test/setup.js" ] } }
Now that you need it, you can access the Chai chai.expect API through chai.expect and the Jest chai.expect API through expect .
source share