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

Commit b315795

Browse files
authored
feat: Use custom Babel module to enable named imports mocking (#265)
1 parent de07da9 commit b315795

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+358
-14
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ Folder Name | Description
174174
[visual-testing-with-percy](examples/visual-testing-with-percy) | [Visual testing](#visual-testing) for components using 3rd party service [Percy.io](https://percy.io/)
175175
[visual-testing-with-happo](examples/visual-testing-with-happo) | [Visual testing](#visual-testing) for components using 3rd party service [Happo](https://happo.io/)
176176
[using-babel](examples/using-babel) | Bundling specs and loaded source files using project's existing `.babelrc` file
177+
[webpack-file](examples/webpack-file) | Load existing `webpack.config.js` file
177178
[webpack-options](examples/webpack-options) | Using the default Webpack options from `@cypress/webpack-preprocessor` to transpile JSX specs
178179
<!-- prettier-ignore-end -->
179180

circle.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ executors:
66
base-12-16-2:
77
docker:
88
- image: 'cypress/base:12.16.2'
9+
environment:
10+
DEBUG: find-webpack,cypress-react-unit-test
911

1012
workflows:
1113
build:
@@ -117,6 +119,26 @@ workflows:
117119
npm run only-covered
118120
working_directory: examples/tailwind
119121

122+
- cypress/run:
123+
name: Example Webpack file
124+
executor: base-12-16-2
125+
requires:
126+
- Install
127+
# each example installs "cypress-react-unit-test" as a local dependency (symlink)
128+
install-command: npm install
129+
verify-command: echo 'Already verified'
130+
no-workspace: true
131+
working_directory: examples/webpack-file
132+
command: npm test
133+
store_artifacts: true
134+
post-steps:
135+
- run:
136+
name: Check coverage 📈
137+
command: |
138+
npm run check-coverage
139+
npm run only-covered
140+
working_directory: examples/webpack-file
141+
120142
- cypress/run:
121143
name: Example Webpack options
122144
executor: cypress/base-12
@@ -271,6 +293,7 @@ workflows:
271293
- Example Sass
272294
- Example Snapshots
273295
- Example Tailwind
296+
- Example Webpack file
274297
- Example Webpack options
275298
- Visual Sudoku
276299
- Visual with Percy

examples/react-scripts-folder/cypress.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"fixturesFolder": false,
33
"testFiles": "**/*cy-spec.js",
44
"viewportWidth": 500,
5-
"viewportHeight": 500,
5+
"viewportHeight": 800,
66
"experimentalComponentTesting": true,
77
"componentFolder": "cypress/component"
88
}

examples/react-scripts-folder/cypress/component/App.cy-spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import React from 'react'
44
import App from '../../src/App'
55
import { mount } from 'cypress-react-unit-test'
6+
import * as calc from '../../src/calc'
7+
import * as child from '../../src/Child'
68

79
describe('App', () => {
810
it('renders learn react link', () => {
@@ -15,4 +17,26 @@ describe('App', () => {
1517
mount(<div>JSX</div>)
1618
cy.contains('JSX')
1719
})
20+
21+
it('controls the random number by stubbing named import', () => {
22+
// we are stubbing "getRandomNumber" exported by "calc.js"
23+
// and imported into "App.js" and called.
24+
cy.stub(calc, 'getRandomNumber')
25+
.as('lucky')
26+
.returns(777)
27+
mount(<App />)
28+
cy.contains('.random', '777')
29+
cy.get('@lucky').should('be.calledOnce')
30+
})
31+
32+
it('stubs an imported child component', () => {
33+
cy.stub(child, 'Child')
34+
.as('child')
35+
.returns(<div className="mock-child">Mock child component</div>)
36+
mount(<App />)
37+
// App component rendered our mock child component!
38+
cy.contains('Mock child component')
39+
cy.get('@child').should('have.been.calledTwice')
40+
cy.get('.mock-child').should('have.length', 2)
41+
})
1842
})

examples/react-scripts-folder/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"scripts": {
66
"test": "../../node_modules/.bin/cypress run",
77
"cy:open": "../../node_modules/.bin/cypress open",
8-
"check-coverage": "../../node_modules/.bin/check-coverage src/App.js",
9-
"only-covered": "../../node_modules/.bin/only-covered src/App.js"
8+
"check-coverage": "../../node_modules/.bin/check-coverage src/App.js src/calc.js src/Child.js",
9+
"only-covered": "../../node_modules/.bin/only-covered src/App.js src/calc.js src/Child.js"
1010
},
1111
"devDependencies": {
1212
"cypress-react-unit-test": "file:../.."

examples/react-scripts-folder/src/App.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import React from 'react'
33
import './App.css'
44
import logo from './logo.svg' // => "/__root/src/logo.svg"
55
import cypressLogo from './cypress-logo-dark.png' // => "/__root/src/cypress-logo-dark.png"
6+
import { getRandomNumber } from './calc'
7+
import { Child } from './Child'
68

79
// large image will be transformed into
810
// a different url static/media/vans.25e5784d.jpg
@@ -22,6 +24,7 @@ function App() {
2224
<p>
2325
Edit <code>src/App.js</code> and save to reload.
2426
</p>
27+
<p className="random">This is a random number {getRandomNumber()}</p>
2528
<a
2629
className="App-link"
2730
href="https://reactjs.org"
@@ -31,6 +34,8 @@ function App() {
3134
Learn React
3235
</a>
3336
</header>
37+
<Child />
38+
<Child />
3439
</div>
3540
)
3641
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import React from 'react'
2+
3+
export const Child = () => <div>Real child component</div>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const getRandomNumber = () => Math.round(Math.random() * 1000)

examples/react-scripts/cypress.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"fixturesFolder": false,
33
"testFiles": "**/*cy-spec.js",
44
"viewportWidth": 500,
5-
"viewportHeight": 500,
5+
"viewportHeight": 800,
66
"experimentalComponentTesting": true,
77
"componentFolder": "src"
88
}

examples/react-scripts/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"scripts": {
66
"test": "../../node_modules/.bin/cypress run",
77
"cy:open": "../../node_modules/.bin/cypress open",
8-
"check-coverage": "../../node_modules/.bin/check-coverage src/App.js",
9-
"only-covered": "../../node_modules/.bin/only-covered src/App.js"
8+
"check-coverage": "../../node_modules/.bin/check-coverage src/App.js src/calc.js src/Child.js",
9+
"only-covered": "../../node_modules/.bin/only-covered src/App.js src/calc.js src/Child.js"
1010
},
1111
"devDependencies": {
1212
"cypress-react-unit-test": "file:../.."

0 commit comments

Comments
 (0)