You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Mar 5, 2022. It is now read-only.
feat: mocking ES6 imports when using Babelrc (#278)
During test runs, there is a Babel plugin that transforms ES6 imports into plain objects that can be stubbed using [cy.stub](https://on.cypress.io/stub). In essence
```js
// component imports named ES6 import from "calc.js
import { getRandomNumber } from './calc'
const Component = () => {
// then calls it
const n = getRandomNumber()
return <div className="random">{n}</div>
}
```
The test can mock that import before mounting the component
```js
import Component from './Component.jsx'
import * as calc from './calc'
describe('Component', () => {
it('mocks call from the component', () => {
cy.stub(calc, 'getRandomNumber')
.as('lucky')
.returns(777)
mount(<Component />)
})
})
```
**Bonus:** in order to enable code instrumentation, add the `babel-plugin-istanbul` (included in this plugin) to your `.babelrc` setup. You can place it under `test` environment to avoid instrumenting production code. Example `.babelrc` config file that you can execute with `BABEL_ENV=test npx cypress open`
65
+
### Add Babel plugins
66
+
67
+
If you want to use code instrumentation, add the [babel-plugin-istanbul](https://github.com/istanbuljs/babel-plugin-istanbul) to your `.babelrc` setup. You do not even need to install it separately, as it is already included in `cypress-react-unit-test` as a dependency.
68
+
69
+
If you want to use ES6 import mocking, add the [@babel/plugin-transform-modules-commonjs](https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-modules-commonjs) to the list of plugins. This module is also included in `cypress-react-unit-test` as a dependency.
When loading your `.babelrc` settings, `cypress-react-unit-test` sets `BABEL_ENV` and `NODE_ENV` to `test` if they are not set already. Thus you can move the above plugins into the `test` environment to exclude them from being used in production bundle.
Copy file name to clipboardExpand all lines: examples/using-babel/README.md
+30-1Lines changed: 30 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,7 @@ npm test
14
14
15
15
## Specs
16
16
17
-
See specs [src/Post.spec.js](src/Post.spec.js) and[src/Skeleton.spec.js](src/Skeleton.spec.js). The specs are bundled using [.babelrc](.babelrc) settings via [cypress/plugins/index.js](cypress/plugins/index.js) file that includes file preprocessor
17
+
See spec files[src/\*.spec.js](src). The specs are bundled using [.babelrc](.babelrc) settings via [cypress/plugins/index.js](cypress/plugins/index.js) file that includes file preprocessor
18
18
19
19
```js
20
20
// let's bundle spec files and the components they include using
During test runs, there is a Babel plugin that transforms ES6 imports into plain objects that can be stubbed using [cy.stub](https://on.cypress.io/stub). In essence
34
+
35
+
```js
36
+
// component imports named ES6 import from "calc.js
37
+
import { getRandomNumber } from'./calc'
38
+
constComponent= () => {
39
+
// then calls it
40
+
constn=getRandomNumber()
41
+
return<div className="random">{n}</div>
42
+
}
43
+
```
44
+
45
+
The test can mock that import before mounting the component
0 commit comments