Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
jest-auto-snapshots
Advanced tools
Automatically generate prop fixtures and snapshot tests for your React components
import snap from 'jest-auto-snapshots';
import MyComponent from '../MyComponent';
snap(MyComponent, '../MyComponent.jsx');
↓
PASS examples/MyComponent/__tests__/MyComponent.test.js
jest-auto-snapshots > MyComponent
✓ Matches snapshot when passed only required props (3ms)
✓ Matches snapshot when passed all props (2ms)
✓ Matches snapshot when boolean prop "booleanProp" is set to: "false" (1ms)
Snapshot tests are a very useful tool whenever you want to make sure your UI does not change unexpectedly.
jest
and react-test-renderer
are fantastic tools, but writing tests for all the different possible rendering states and maintaining props is tedious. Really, we just want to know when & where a component snapshot changes and if that change was intentional.
For example, take the following simple component: <MyComponent active />
. In order to make sure MyComponent
is fully covered in snapshot tests, we would have to write (and maintain) two separate tests for the boolean active
state.
jest-auto-snapshots
can detect the different prop types your component uses and create those shapshot tests for you!
If MyComponent
's props ever get changed/added/removed jest-auto-snapshots
will just create and run new snapshot tests.
See the bigger example below for more info.
arrays
or shapes
. This would just create way to many snapshots and should be handled manually (or, better yet, strive to make your component props as flat as possible). I'm open to a settings option to allow for this in future iterations, though..jsx
file with react-docgen.Component.propTypes
tree and checks for different conditions (required vs optional props, boolean props, etc).yarn add jest-auto-snapshots --dev
or
npm i jest-auto-snapshots --save-dev
MyComponent.jsx
const MyComponent = ({ stringProp, booleanProp, nodeProp }) => (
<div>
{booleanProp && <span>Hello</span>}
{stringProp}
{nodeProp}
</div>
);
MyComponent.propTypes = {
booleanProp: PropTypes.bool,
stringProp: PropTypes.string,
nodeProp: PropTypes.node,
};
MyComponent.test.js
import snap from 'jest-auto-snapshots';
import MyComponent from '../MyComponent';
snap(MyComponent, '../MyComponent.jsx');
MyComponent.test.js.snap
(generated from the example code above)// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`when boolean prop "booleanProp" is set to: "false" 1`] = `
<div>
jest-auto-snapshots String Fixture
<NodeFixture />
</div>
`;
exports[`when passed all props 1`] = `
<div>
<span>
Hello
</span>
jest-auto-snapshots String Fixture
<NodeFixture />
</div>
`;
exports[`when passed only required props 1`] = `
<div>
jest-auto-snapshots String Fixture
</div>
`;
For more examples highlighting different use cases, please check out the examples directory
snap(
component: <Component:required>,
componentFilePath: <String:required>, // jest-auto-snapshots needs to parse the component file itself to determine prop fixtures
config: <Object:optional>, // optionally set fixtures for the component tests (see Config section below)
);
There are 2 ways to change configuration for the script. Either at the root level in your jest setup file or in each individual test. The params are the same for both:
Key | Description | Defaults |
---|---|---|
fixturesByPropKey | Inject component prop fixtures bassed on the prop key | None |
fixturesByPropType | Inject component prop fixtures based on the prop type | see src/configure.js. By default it covers all the core propTypes. |
In your jest setup file:
const jestAutoSnapshots = require('jest-auto-snapshots');
jestAutoSnapshots.configure({
fixturesByPropType: {
customPropType: 'custom fixture',
},
fixturesByPropKey: {
user: { name: 'Joe', age: 30 },
},
});
For the above example:
'custom fixture'
will be injected for all components with something: customPropType
in their propTypes
object.{name: 'Joe', age: 30}
will be injected for all components with user: PropTypes.shape({ /* ... */ })
in their propTypes
object.In your test file:
import snap from 'jest-auto-snapshots';
import CustomProps from '../CustomProps';
snap(CustomProps, '../CustomProps.jsx', {
fixturesByPropType: {
customPropType: 'custom fixture',
},
fixturesByPropKey: {
user: { name: 'Joe', age: 30 },
},
});
For the above example, these custom fixtures will only be injected for the current test.
FAQs
Fully automated Jest snapshot tests for React components
The npm package jest-auto-snapshots receives a total of 440 weekly downloads. As such, jest-auto-snapshots popularity was classified as not popular.
We found that jest-auto-snapshots demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.