Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
jest-styled-components
Advanced tools
jest-styled-components is a Jest utility for testing styled-components. It provides custom matchers to test the styles applied to your components, making it easier to ensure that your styled-components are rendered correctly.
toHaveStyleRule
The `toHaveStyleRule` matcher allows you to check if a specific style rule is applied to a component. In this example, it verifies that the `Button` component has a `color` style rule set to `red`.
const styled = require('styled-components').default;
const { toHaveStyleRule } = require('jest-styled-components');
expect.extend({ toHaveStyleRule });
const Button = styled.button`
color: red;
`;
// Test
it('should have red color', () => {
const wrapper = mount(<Button />);
expect(wrapper).toHaveStyleRule('color', 'red');
});
toMatchSnapshot
The `toMatchSnapshot` matcher allows you to take a snapshot of the rendered component and compare it to a previously saved snapshot. This is useful for ensuring that the component's styles have not changed unexpectedly.
const styled = require('styled-components').default;
const { toMatchSnapshot } = require('jest-styled-components');
expect.extend({ toMatchSnapshot });
const Button = styled.button`
color: red;
`;
// Test
it('should match the snapshot', () => {
const wrapper = mount(<Button />);
expect(wrapper).toMatchSnapshot();
});
jest-emotion is a Jest utility for testing Emotion styled components. It provides custom matchers to test the styles applied to your components, similar to jest-styled-components. However, it is specifically designed for Emotion, another popular CSS-in-JS library.
enzyme-to-json is a utility for converting Enzyme wrappers to a format compatible with Jest snapshots. While it does not provide style-specific matchers, it can be used in conjunction with Enzyme to test the rendered output of styled-components.
react-testing-library is a lightweight solution for testing React components. It focuses on testing components from the user's perspective and can be used with jest-styled-components to test styled-components. It does not provide style-specific matchers but can be used to test the overall behavior and appearance of components.
Jest utilities for Styled Components.
yarn add --dev jest-styled-components
// *.spec.js
import 'jest-styled-components'
// ...
expect(tree).toMatchStyledComponentsSnapshot()
// *.spec.js
import 'jest-styled-components'
// ...
expect(tree).toHaveStyleRule('property', value)
// *.spec.js
import 'jest-styled-components/native'
// ...
expect(tree).toHaveStyleRule('property', value)
FAQs
Jest utilities for Styled Components
The npm package jest-styled-components receives a total of 619,663 weekly downloads. As such, jest-styled-components popularity was classified as popular.
We found that jest-styled-components demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.