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

Commit 7d733c1

Browse files
committed
feat: add babelrc preprocessor, closes #149
1 parent 79f2130 commit 7d733c1

File tree

4 files changed

+99
-2
lines changed

4 files changed

+99
-2
lines changed

docs/recipes.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ If you are using Create-React-App v3 or `react-scripts`, and want to reuse the b
1717
```json
1818
{
1919
"pluginsFile": "node_modules/cypress-react-unit-test/plugins/cra-v3",
20-
"supportFile": "node_modules/cypress-react-unit-test/support"
20+
"supportFile": "node_modules/cypress-react-unit-test/support",
21+
"experimentalComponentTesting": true
2122
}
2223
```
2324

@@ -53,8 +54,43 @@ If you have your own webpack config, you can use included plugins file to load i
5354
```json
5455
{
5556
"pluginsFile": "node_modules/cypress-react-unit-test/plugins/load-webpack",
57+
"experimentalComponentTesting": true,
5658
"env": {
5759
"webpackFilename": "demo/config/webpack.dev.js"
5860
}
5961
}
6062
```
63+
64+
## Your `.babelrc` file
65+
66+
If you are using Babel without Webpack to transpile, you can use the plugin that tells Babel loader to use your configuration file. In `cypress.json` file set:
67+
68+
```json
69+
{
70+
"pluginsFile": "node_modules/cypress-react-unit-test/plugins/babelrc",
71+
"supportFile": "node_modules/cypress-react-unit-test/support",
72+
"experimentalComponentTesting": true
73+
}
74+
```
75+
76+
**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`
77+
78+
```json
79+
{
80+
"presets": [
81+
"@babel/preset-env",
82+
"@babel/preset-react",
83+
{
84+
"plugins": ["@babel/plugin-proposal-class-properties"]
85+
},
86+
"@emotion/babel-preset-css-prop"
87+
],
88+
"env": {
89+
"test": {
90+
"plugins": ["babel-plugin-istanbul"]
91+
}
92+
}
93+
}
94+
```
95+
96+
See [bahmutov/react-loading-skeleton](https://github.com/bahmutov/react-loading-skeleton) example
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// @ts-check
2+
// uses webpack to load your .babelrc file
3+
const debug = require('debug')('cypress-react-unit-test')
4+
const webpackPreprocessor = require('@cypress/webpack-preprocessor')
5+
const { addImageRedirect } = require('../utils/add-image-redirect')
6+
7+
// note: modifies the input object
8+
function enableBabelrc(webpackOptions) {
9+
if (!Array.isArray(webpackOptions.module.rules)) {
10+
debug('could not find webpack module rules %o', webpackOptions.module)
11+
return
12+
}
13+
14+
const jsCodeRule = webpackOptions.module.rules[0]
15+
debug('js code rule %o', jsCodeRule)
16+
if (!jsCodeRule) {
17+
debug('could not get first rule %o', webpackOptions.module)
18+
return
19+
}
20+
21+
const jsCodeRuleUses = jsCodeRule.use
22+
if (!Array.isArray(jsCodeRuleUses)) {
23+
debug('js code rule use is not an array %o', jsCodeRuleUses)
24+
return
25+
}
26+
const babelLoaderOptions = jsCodeRuleUses[0].options
27+
debug('Babel options %o', babelLoaderOptions)
28+
if (!babelLoaderOptions) {
29+
debug('Hmm, no babel loader options %o', jsCodeRuleUses)
30+
return
31+
}
32+
33+
// by deleting our default presets list
34+
// we allow Babel loader to load the presets and plugins
35+
// from the project's .babelrc file
36+
delete babelLoaderOptions.presets
37+
}
38+
39+
module.exports = config => {
40+
debug('env object %o', config.env)
41+
42+
const coverageIsDisabled =
43+
config && config.env && config.env.coverage === false
44+
debug('coverage is disabled? %o', { coverageIsDisabled })
45+
46+
const wpPreprocessorOptions = webpackPreprocessor.defaultOptions
47+
enableBabelrc(wpPreprocessorOptions.webpackOptions)
48+
49+
addImageRedirect(wpPreprocessorOptions.webpackOptions)
50+
51+
return webpackPreprocessor(wpPreprocessorOptions)
52+
}

plugins/babelrc/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const filePreprocessor = require('./file-preprocessor')
2+
3+
module.exports = (on, config) => {
4+
require('@cypress/code-coverage/task')(on, config)
5+
on('file:preprocessor', filePreprocessor(config))
6+
// IMPORTANT to return the config object
7+
// with the any changed environment variables
8+
return config
9+
}

plugins/cra-v3/file-preprocessor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const getWebpackOptions = opts => {
1717
}
1818
debug('webpack options: %o', webpackOptions)
1919
findWebpack.cleanForCypress(opts, webpackOptions)
20-
debug('claned webpack options: %o', webpackOptions)
20+
debug('cleaned webpack options: %o', webpackOptions)
2121

2222
addImageRedirect(webpackOptions)
2323

0 commit comments

Comments
 (0)