Skip to content
This repository was archived by the owner on Mar 5, 2022. It is now read-only.

Commit d65d2fb

Browse files
authored
add example with Webpack5 (#511)
1 parent c551c96 commit d65d2fb

19 files changed

+240
-0
lines changed

circle.yml

+27
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,32 @@ workflows:
249249
npm run only-covered
250250
working_directory: examples/webpack-file
251251

252+
- cypress/run:
253+
name: Example Webpack5 file
254+
requires:
255+
- Component Tests
256+
executor: cypress/base-12
257+
install-command: |
258+
ls -la ../..
259+
echo ***Installing cypress-react-unit-test from root TGZ archive***
260+
npm install -D ../../cypress-react-unit-test-0.0.0-development.tgz
261+
echo ***Installing other dependencies***
262+
npm install
263+
echo ***rename root node_modules to avoid accidental dependencies***
264+
mv ../../node_modules ../../no_modules
265+
verify-command: echo 'Already verified'
266+
no-workspace: true
267+
working_directory: examples/webpack5-file
268+
command: npm test
269+
store_artifacts: true
270+
post-steps:
271+
- run:
272+
name: Check coverage 📈
273+
command: |
274+
npm run check-coverage
275+
npm run only-covered
276+
working_directory: examples/webpack5-file
277+
252278
- cypress/run:
253279
name: Example Rollup
254280
requires:
@@ -484,6 +510,7 @@ workflows:
484510
- Example Snapshots
485511
- Example Tailwind
486512
- Example Webpack file
513+
- Example Webpack5 file
487514
- Example Webpack options
488515
- Example Rollup
489516
- Visual Sudoku

examples/webpack5-file/.babelrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["@babel/preset-env", "@babel/preset-react"]
3+
}

examples/webpack5-file/.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

examples/webpack5-file/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# example: webpack5-file
2+
3+
> Component tests for projects using existing [webpack.config.js](webpack.config.js) file and [Webpack 5](https://webpack.js.org/blog/2020-10-10-webpack-5-release/)
4+
5+
## Usage
6+
7+
1. Make sure the root project has been built .
8+
9+
```bash
10+
# in the root of the project
11+
npm install
12+
npm run build
13+
```
14+
15+
2. Run `npm install` in this folder to symlink the `cypress-react-unit-test` dependency.
16+
17+
```bash
18+
# in this folder
19+
npm install
20+
```
21+
22+
3. Start Cypress
23+
24+
```bash
25+
npm run cy:open
26+
# or just run headless tests
27+
npm test
28+
```
29+
30+
## Notes
31+
32+
See tests in the [cypress/component](cypress/component) folder. We also allow tests to load components using a [webpack alias](webpack.config.js) from `more-components/src` folder using `import Hello from '@components/Hello'`.

examples/webpack5-file/cypress.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"fixturesFolder": false,
3+
"testFiles": "**/*cy-spec.js",
4+
"viewportWidth": 500,
5+
"viewportHeight": 500,
6+
"experimentalComponentTesting": true
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/// <reference types="cypress" />
2+
import React from 'react'
3+
import { mount } from 'cypress-react-unit-test'
4+
import ChildComponent from './ChildComponent'
5+
import * as calc from './calc'
6+
7+
describe('ChildComponent unstubbed', () => {
8+
it('works', () => {
9+
cy.spy(calc, 'getRandomNumber').as('getRandomNumber')
10+
mount(<ChildComponent />)
11+
// make sure the component shows the random value
12+
// returned by the calc.getRandomNumber function
13+
cy.get('@getRandomNumber')
14+
.should('have.been.called')
15+
.its('returnValues.0')
16+
.then(n => {
17+
cy.contains('.random', n)
18+
})
19+
})
20+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import React from 'react'
2+
import { getRandomNumber } from './calc'
3+
4+
const ChildComponent = () => (
5+
<div>
6+
Child component <p className="random">Random number {getRandomNumber()}</p>
7+
</div>
8+
)
9+
10+
export default ChildComponent
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference types="cypress" />
2+
import React from 'react'
3+
import { mount } from 'cypress-react-unit-test'
4+
// use path alias defined in webpack config
5+
import Hello from '@components/Hello'
6+
7+
describe('Hello using path alias', () => {
8+
it('works', () => {
9+
mount(<Hello />)
10+
cy.contains('Hello!').should('be.visible')
11+
})
12+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/// <reference types="cypress" />
2+
import React from 'react'
3+
import { mount } from 'cypress-react-unit-test'
4+
import ParentComponent from './ParentComponent'
5+
import * as calc from './calc'
6+
import * as ChildComponent from './ChildComponent'
7+
8+
describe('Mocking', () => {
9+
it('named getRandomNumber imported in the child component', () => {
10+
cy.stub(calc, 'getRandomNumber')
11+
.as('lucky')
12+
.returns(777)
13+
mount(<ParentComponent />)
14+
cy.contains('.random', '777')
15+
})
16+
17+
it('entire child component exported as default', () => {
18+
cy.stub(ChildComponent, 'default')
19+
.as('child')
20+
.returns(<div className="mock-child">Mock child component</div>)
21+
mount(<ParentComponent />)
22+
cy.contains('.mock-child', 'Mock child component')
23+
})
24+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import React from 'react'
2+
import ChildComponent from './ChildComponent'
3+
4+
const ParentComponent = () => (
5+
<div>
6+
Parent component, child component below
7+
<br />
8+
<ChildComponent />
9+
</div>
10+
)
11+
12+
export default ParentComponent
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// <reference types="cypress" />
2+
import React from 'react'
3+
import { mount } from 'cypress-react-unit-test'
4+
import Test from './Test'
5+
6+
describe('components', () => {
7+
it('works', () => {
8+
mount(<Test />)
9+
cy.contains('Text')
10+
})
11+
12+
it('works with plain div', () => {
13+
mount(<div>Text</div>)
14+
cy.contains('Text')
15+
})
16+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React from 'react'
2+
3+
const Test = () => <div>Text</div>
4+
5+
export default Test
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const getRandomNumber = () => Math.round(Math.random() * 1000)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/// <reference types="cypress" />
2+
describe('integration spec', () => {
3+
it('works', () => {
4+
expect(1).to.equal(1)
5+
})
6+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// load file preprocessor that comes with this plugin
2+
// https://github.com/bahmutov/cypress-react-unit-test#install
3+
module.exports = (on, config) => {
4+
// from the root of the project (folder with cypress.json file)
5+
config.env.webpackFilename = 'webpack.config.js'
6+
require('cypress-react-unit-test/plugins/load-webpack')(on, config)
7+
// IMPORTANT to return the config object
8+
// with the any changed environment variables
9+
return config
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require('cypress-react-unit-test/dist/hooks')
2+
// if we need code coverage, need to include its custom support hook
3+
require('@cypress/code-coverage/support')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React from 'react'
2+
3+
const Hello = () => <div>Hello!</div>
4+
5+
export default Hello

examples/webpack5-file/package.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "example-webpack-options",
3+
"description": "Using default Webpack options to transpile simple tests",
4+
"private": true,
5+
"scripts": {
6+
"test": "cypress-expect run --passing 7",
7+
"cy:open": "cypress open",
8+
"check-coverage": "check-coverage Test.js calc.js ParentComponent.js ChildComponent.js",
9+
"only-covered": "only-covered Test.js calc.js ParentComponent.js ChildComponent.js more-components/src/Hello.js"
10+
},
11+
"devDependencies": {
12+
"babel-loader": "8.1.0",
13+
"check-code-coverage": "1.10.0",
14+
"cypress": "5.3.0",
15+
"cypress-expect": "2.1.0",
16+
"cypress-react-unit-test": "file:../..",
17+
"react": "^16.13.1",
18+
"react-dom": "^16.13.1",
19+
"webpack": "5.2.0"
20+
}
21+
}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const path = require('path')
2+
3+
module.exports = {
4+
module: {
5+
rules: [
6+
{
7+
test: /\.(js|jsx)$/,
8+
exclude: /node_modules/,
9+
use: {
10+
loader: 'babel-loader',
11+
},
12+
},
13+
],
14+
},
15+
// add alias to load "@components" from another folder
16+
// so that tests in cypress/component can load components using
17+
// import X from '@components/X'
18+
// see https://glebbahmutov.com/blog/using-ts-aliases-in-cypress-tests/
19+
resolve: {
20+
extensions: ['.js'],
21+
alias: {
22+
'@components': path.resolve(__dirname, 'more-components', 'src'),
23+
},
24+
},
25+
}

0 commit comments

Comments
 (0)