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

File tree

19 files changed

+240
-0
lines changed

19 files changed

+240
-0
lines changed

circle.yml

Lines changed: 27 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

examples/webpack5-file/README.md

Lines changed: 32 additions & 0 deletions
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'`.
Lines changed: 7 additions & 0 deletions
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+
}
Lines changed: 20 additions & 0 deletions
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+
})
Lines changed: 10 additions & 0 deletions
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
Lines changed: 12 additions & 0 deletions
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+
})
Lines changed: 24 additions & 0 deletions
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+
})
Lines changed: 12 additions & 0 deletions
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

0 commit comments

Comments
 (0)