Security News
NIST Misses 2024 Deadline to Clear NVD Backlog
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
cypress-react-unit-test
Advanced tools
A little helper to unit test React components in the open source Cypress.io E2E test runner ALPHA
What is this? This package allows you to use Cypress test runner to unit test your React components with zero effort.
How is this different from Enzyme? It is similar in functionality BUT runs the component in the real browser with full power of Cypress E2E test runner: live GUI, full API, screen recording, CI support, cross-platform.
window.alert
#6Requires Node version 6 or above.
npm install --save-dev cypress cypress-react-unit-test
// import the component you want to test
import { HelloState } from '../../src/hello-x.jsx'
import React from 'react'
describe('HelloState component', () => {
it('works', () => {
// mount the component under test
cy.mount(<HelloState />)
// start testing!
cy.contains('Hello Spider-man!')
// mounted component is aliased as @Component
cy.get('@Component')
.invoke('setState', { name: 'React' })
cy.get('@Component')
.its('state')
.should('deep.equal', { name: 'React' })
// check if GUI has rerendered
cy.contains('Hello React!')
})
})
How can we use features that require transpilation? Using @cypress/webpack-preprocessor. You can use cypress/plugins/index.js to configure any transpilation plugins you need.
For example, to enable class properties:
// cypress/plugins/index.js
const webpack = require('@cypress/webpack-preprocessor')
const webpackOptions = {
module: {
rules: [
{
test: /\.(js|jsx|mjs)$/,
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env', '@babel/preset-react'],
plugins: ['@babel/plugin-proposal-class-properties'],
},
}
]
}
}
const options = {
// send in the options from your webpack.config.js, so it works the same
// as your app's code
webpackOptions,
watchOptions: {}
}
module.exports = on => {
on('file:preprocessor', webpack(options))
}
Install dev dependencies
npm i -D @cypress/webpack-preprocessor \
babel-loader @babel/preset-env @babel/preset-react \
@babel/plugin-proposal-class-properties
And write a component using class properties
import React from 'react'
export class Transpiled extends React.Component {
state = {
count: 0
}
// ...
}
All components are in src folder. All tests are in cypress/integration folder.
window.alert
calls from your component stateless-alert.jsxSame feature for unit testing components from other framesworks using Cypress
FAQs
Unit test React components using Cypress
The npm package cypress-react-unit-test receives a total of 1,586 weekly downloads. As such, cypress-react-unit-test popularity was classified as popular.
We found that cypress-react-unit-test 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
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.