Skip to content
This repository was archived by the owner on Oct 1, 2020. It is now read-only.

Commit 9e80faf

Browse files
authored
describe how to use your own babelrc (#78)
1 parent 2d29c87 commit 9e80faf

File tree

11 files changed

+99
-2
lines changed

11 files changed

+99
-2
lines changed

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ examples/react-app/node_modules
66
examples/react-app/build
77
examples/react-app/package.json
88
examples/react-app/cypress/integration/dynamic-import-spec.js
9+
# contains nullish operator
10+
examples/use-babelrc/cypress/integration/spec.js
911
*.ts

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ module.exports = (on) => {
5151
Pass in options as the second argument to `webpack`:
5252

5353
```javascript
54-
const webpack = require('@cypress/webpack-preprocessor')
54+
const webpackPreprocessor = require('@cypress/webpack-preprocessor')
5555

5656
module.exports = (on) => {
5757
const options = {
@@ -61,7 +61,7 @@ module.exports = (on) => {
6161
watchOptions: {},
6262
}
6363

64-
on('file:preprocessor', webpack(options))
64+
on('file:preprocessor', webpackPreprocessor(options))
6565
}
6666
```
6767

@@ -92,6 +92,19 @@ Object of webpack options. Just `require` in the options from your `webpack.conf
9292

9393
Source maps are always enabled unless explicitly disabled by specifying `devtool: false`.
9494

95+
### use babelrc
96+
97+
If you have a `.babelrc` file and would like to use it, then you must delete `options.presets` from the default Webpack options
98+
99+
```js
100+
const webpackPreprocessor = require('@cypress/webpack-preprocessor')
101+
const defaults = webpackPreprocessor.defaultOptions
102+
module.exports = (on) => {
103+
delete defaults.webpackOptions.module.rules[0].use[0].options.presets
104+
on('file:preprocessor', webpackPreprocessor(defaults))
105+
}
106+
```
107+
95108
### watchOptions
96109

97110
Object of options for watching. See [webpack's docs](https://webpack.js.org/configuration/watch).

circle.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,24 @@ workflows:
4646
no-workspace: true
4747
working_directory: examples/react-app
4848

49+
- cypress/run:
50+
name: Test babelrc
51+
requires:
52+
- Install
53+
executor: cypress/base-12-14-0
54+
install-command: echo 'Nothing else to install in this job'
55+
timeout: '1m'
56+
no-workspace: true
57+
working_directory: examples/use-babelrc
58+
4959
# wait for all jobs to finish and possible run NPM release
5060
- cypress/run:
5161
name: NPM release
5262
requires:
5363
- Install
5464
- Tests
5565
- Test React App
66+
- Test babelrc
5667
executor: cypress/base-12-14-0
5768
# nothing to install - cypress/install job does it
5869
# and nothing to pass to the next job

examples/use-babelrc/.babelrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"plugins": [
3+
"@babel/plugin-proposal-nullish-coalescing-operator"
4+
]
5+
}

examples/use-babelrc/.eslintrc.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"env": {
3+
"cypress/globals": true
4+
},
5+
"extends": [
6+
"eslint:recommended",
7+
"plugin:cypress/recommended"
8+
],
9+
"plugins": ["cypress"]
10+
}

examples/use-babelrc/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# use-babelrc
2+
3+
> Transpiling specs using local .babelrc file
4+
5+
The [cypress/integration/spec.js](cypress/integration/spec.js) uses null coalescing operator `??`, which needs a [separate plugin](https://babeljs.io/docs/en/next/babel-plugin-proposal-nullish-coalescing-operator) for Babel to transpile it.
6+
7+
```js
8+
it('handles nullish operator', () => {
9+
const data = {
10+
person: {
11+
firstName: 'Joe'
12+
}
13+
}
14+
const name = data.person.firstName ?? 'Anonymous'
15+
expect(name).to.equal('Joe')
16+
})
17+
```
18+
19+
There is a local [.babelrc](.babelrc) file, and to make Cypress preprocessor use it, the [cypress/plugins/index.js](cypress/plugins/index.js) removes Babel presets from the default options.

examples/use-babelrc/cypress.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"fixturesFolder": false,
3+
"supportFile": false
4+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference types="cypress" />
2+
describe('Use .babelrc', () => {
3+
it('handles nullish operator', () => {
4+
const data = {
5+
person: {
6+
firstName: 'Joe'
7+
}
8+
}
9+
const name = data.person.firstName ?? 'Anonymous'
10+
expect(name).to.equal('Joe')
11+
})
12+
})
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const webpackPreprocessor = require('../../../..')
2+
const defaults = webpackPreprocessor.defaultOptions
3+
4+
module.exports = (on) => {
5+
delete defaults.webpackOptions.module.rules[0].use[0].options.presets
6+
on('file:preprocessor', webpackPreprocessor(defaults))
7+
}

examples/use-babelrc/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "use-babelrc",
3+
"version": "1.0.0",
4+
"description": "Transpiling specs using local .babelrc file",
5+
"private": true,
6+
"scripts": {
7+
"cy:open": "../../node_modules/.bin/cypress open",
8+
"test": "../../node_modules/.bin/cypress run"
9+
},
10+
"license": "ISC",
11+
"author": "",
12+
"keywords": []
13+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
},
2626
"devDependencies": {
2727
"@babel/core": "^7.0.1",
28+
"@babel/plugin-proposal-nullish-coalescing-operator": "7.8.3",
2829
"@babel/preset-env": "^7.0.0",
2930
"@cypress/eslint-plugin-dev": "5.0.0",
3031
"@typescript-eslint/eslint-plugin": "2.27.0",

0 commit comments

Comments
 (0)