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

Commit 05b5da1

Browse files
committed
trying to work around exports problems
1 parent 3a56a40 commit 05b5da1

File tree

10 files changed

+286
-16
lines changed

10 files changed

+286
-16
lines changed

examples/using-babel/.babelrc

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
{
2+
"sourceType": "unambiguous",
23
"presets": [
34
"@babel/preset-env",
45
"@babel/preset-react",
5-
{
6-
"plugins": ["@babel/plugin-proposal-class-properties"]
7-
},
6+
// {
7+
// "plugins": ["@babel/plugin-proposal-class-properties"]
8+
// },
89
"@emotion/babel-preset-css-prop"
910
],
11+
"plugins": ["@babel/plugin-proposal-class-properties"],
1012
"env": {
1113
// TODO switch this to "development" name
12-
"test": {
14+
"development": {
1315
"plugins": [
1416
// during Cypress tests we want to instrument source code
1517
// to get code coverage from tests
16-
"babel-plugin-istanbul",
18+
// "babel-plugin-istanbul"
1719
// we also want to export ES6 modules as objects
1820
// to allow mocking named imports
1921
[
2022
"@babel/plugin-transform-modules-commonjs",
2123
{
24+
"allowCommonJSExports": true,
2225
"loose": true
2326
}
2427
]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/// <reference types="cypress" />
2+
import React from 'react'
3+
import { getRandomNumber } from './calc'
4+
5+
const Component = () => {
6+
const n = getRandomNumber()
7+
return <div className="random">{n}</div>
8+
}
9+
10+
export default Component
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference types="cypress" />
2+
import React from 'react'
3+
import { mount } from 'cypress-react-unit-test'
4+
import Component from './Component.jsx'
5+
import * as calc from './calc'
6+
7+
describe('Component', () => {
8+
it('mocks call from the component', () => {
9+
cy.stub(calc, 'getRandomNumber')
10+
.as('lucky')
11+
.returns(777)
12+
mount(<Component />)
13+
})
14+
})
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/// <reference types="cypress" />
2+
const React = require('react')
3+
const { getRandomNumber } = require('./calc')
4+
5+
const Component = () => {
6+
const n = getRandomNumber()
7+
return <div className="random">{n}</div>
8+
}
9+
10+
module.exports = Component
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference types="cypress" />
2+
const React = require('react')
3+
const { mount } = require('cypress-react-unit-test')
4+
const Component = require('./ComponentReq.jsx')
5+
const calc = require('./calc')
6+
7+
describe('Component', () => {
8+
it('mocks call from the component', () => {
9+
cy.stub(calc, 'getRandomNumber')
10+
.as('lucky')
11+
.returns(777)
12+
mount(<Component />)
13+
})
14+
})

examples/using-babel/src/Mock.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as calc from './calc'
66

77
describe('Mocking', () => {
88
// https://github.com/bahmutov/cypress-react-unit-test/issues/266
9-
it.skip('mocks import used by the Post', () => {
9+
it('mocks import used by the Post', () => {
1010
cy.stub(calc, 'getRandomNumber')
1111
.as('lucky')
1212
.returns(777)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as calc from './calc'
2+
3+
// checking mocked value if there is no React involved
4+
describe('plain', () => {
5+
it('mocks es6 import', () => {
6+
cy.stub(calc, 'getRandomNumber')
7+
.as('lucky')
8+
.returns(777)
9+
10+
cy.then(() => {
11+
return calc.getRandomNumber()
12+
}).should('equal', 777)
13+
})
14+
})

package-lock.json

Lines changed: 213 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
"url": "https://github.com/bahmutov/cypress-react-unit-test.git"
122122
},
123123
"dependencies": {
124-
"@babel/plugin-transform-modules-commonjs": "7.7.5",
124+
"@babel/plugin-transform-modules-commonjs": "7.10.1",
125125
"@cypress/code-coverage": "3.8.1",
126126
"@cypress/webpack-preprocessor": "5.4.1",
127127
"babel-plugin-istanbul": "6.0.0",

plugins/babelrc/file-preprocessor.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ module.exports = config => {
5959

6060
const wpPreprocessorOptions = webpackPreprocessor.defaultOptions
6161
enableBabelrc(wpPreprocessorOptions.webpackOptions)
62+
debug('webpack options %o', wpPreprocessorOptions.webpackOptions)
6263

6364
addImageRedirect(wpPreprocessorOptions.webpackOptions)
6465

0 commit comments

Comments
 (0)