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

Commit 7b4e542

Browse files
Josh Justicebahmutov
Josh Justice
authored andcommitted
Transpiling (#8)
* Add failing test for component that needs to be transpiled * Enable transpiling class properties `npm install --save-dev babel-loader babel-plugin-transform-class-properties` * Add note about example to readme * Add readme instructions about transpiling
1 parent ad89d98 commit 7b4e542

File tree

5 files changed

+147
-14
lines changed

5 files changed

+147
-14
lines changed

README.md

+59
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,64 @@ describe('HelloState component', () => {
4848

4949
![Unit testing React components](images/demo.png)
5050

51+
## Transpilation
52+
53+
How can we use features that require transpilation? Using [@cypress/webpack-preprocessor](https://github.com/cypress-io/cypress-webpack-preprocessor#readme). You can use [cypress/plugins/index.js](cypress/plugins/index.js) to configure any transpilation plugins you need.
54+
55+
For example, to enable class properties:
56+
57+
```js
58+
// cypress/plugins/index.js
59+
const webpack = require('@cypress/webpack-preprocessor')
60+
const webpackOptions = {
61+
module: {
62+
rules: [
63+
{
64+
test: /\.(js|jsx|mjs)$/,
65+
loader: 'babel-loader',
66+
options: {
67+
presets: ['env', 'react'],
68+
plugins: ['transform-class-properties'],
69+
},
70+
}
71+
]
72+
}
73+
}
74+
75+
const options = {
76+
// send in the options from your webpack.config.js, so it works the same
77+
// as your app's code
78+
webpackOptions,
79+
watchOptions: {}
80+
}
81+
82+
module.exports = on => {
83+
on('file:preprocessor', webpack(options))
84+
}
85+
```
86+
87+
Install dev dependencies
88+
89+
```shell
90+
npm i -D @cypress/webpack-preprocessor \
91+
babel-loader babel-preset-es2015 babel-preset-react \
92+
babel-plugin-transform-class-properties
93+
```
94+
95+
And write a component using class properties
96+
97+
```js
98+
import React from 'react'
99+
100+
export class Transpiled extends React.Component {
101+
state = {
102+
count: 0
103+
}
104+
105+
// ...
106+
}
107+
```
108+
51109
## Examples
52110

53111
All components are in [src](src) folder. All tests are in [cypress/integration](cypress/integration) folder.
@@ -56,6 +114,7 @@ All components are in [src](src) folder. All tests are in [cypress/integration](
56114
* [hello-x-spec.js](cypress/integration/hello-x-spec.js) - testing React component with props and state [hello-x.jsx](src/hello-x.jsx)
57115
* [counter-spec.js](cypress/integration/counter-spec.js) clicks on the component and confirms the result
58116
* [stateless-spec.js](cypress/integration/stateless-spec.js) shows testing a stateless component from [stateless.jsx](src/stateless.jsx)
117+
* [transpiled-spec.js](cypress/integration/stateless-spec.js) shows testing a component with class properties syntax from [transpiled.jsx](src/stateless.jsx)
59118

60119
## Large examples
61120

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { Transpiled } from '../../src/transpiled.jsx'
2+
import React from 'react'
3+
import { mount } from '../../lib'
4+
5+
/* eslint-env mocha */
6+
describe('Transpiled', () => {
7+
it('counts clicks', () => {
8+
mount(<Transpiled />)
9+
cy.contains('count: 0')
10+
.click()
11+
.contains('count: 1')
12+
.click()
13+
.contains('count: 2')
14+
})
15+
16+
it('counts clicks 2', () => {
17+
mount(<Transpiled />)
18+
cy.contains('count: 0')
19+
.click()
20+
.contains('count: 1')
21+
.click()
22+
.contains('count: 2')
23+
})
24+
})
25+
26+
describe('Counter mounted before each test', () => {
27+
beforeEach(() => {
28+
mount(<Transpiled />)
29+
})
30+
31+
it('goes to 3', () => {
32+
cy.contains('count: 0')
33+
.click()
34+
.click()
35+
.click()
36+
.contains('count: 3')
37+
})
38+
39+
it('has count in state', () => {
40+
cy.contains('count: 0')
41+
.click()
42+
.click()
43+
.click()
44+
Cypress.component().its('state')
45+
.should('deep.equal', {count: 3})
46+
})
47+
})

cypress/plugins/index.js

+23-14
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
1-
// ***********************************************************
2-
// This example plugins/index.js can be used to load plugins
3-
//
4-
// You can change the location of this file or turn off loading
5-
// the plugins file with the 'pluginsFile' configuration option.
6-
//
7-
// You can read more here:
8-
// https://on.cypress.io/plugins-guide
9-
// ***********************************************************
1+
const webpack = require('@cypress/webpack-preprocessor')
2+
const webpackOptions = {
3+
module: {
4+
rules: [
5+
{
6+
test: /\.(js|jsx|mjs)$/,
7+
loader: 'babel-loader',
8+
options: {
9+
presets: ['es2015', 'react'],
10+
plugins: ['transform-class-properties'],
11+
},
12+
}
13+
]
14+
}
15+
}
1016

11-
// This function is called when a project is opened or re-opened (e.g. due to
12-
// the project's config changing)
17+
const options = {
18+
// send in the options from your webpack.config.js, so it works the same
19+
// as your app's code
20+
webpackOptions,
21+
watchOptions: {}
22+
}
1323

14-
module.exports = (on, config) => {
15-
// `on` is used to hook into various events Cypress emits
16-
// `config` is the resolved Cypress config
24+
module.exports = on => {
25+
on('file:preprocessor', webpack(options))
1726
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"@types/node": "^8.5.2",
3030
"babel-core": "^6.26.0",
3131
"babel-loader": "^7.1.2",
32+
"babel-plugin-transform-class-properties": "^6.24.1",
3233
"babel-preset-es2015": "^6.24.1",
3334
"babel-preset-react": "^6.24.1",
3435
"cypress": "^1.4.1",

src/transpiled.jsx

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React from 'react'
2+
3+
export class Transpiled extends React.Component {
4+
state = {
5+
count: 0
6+
}
7+
8+
click () {
9+
this.setState({
10+
count: this.state.count + 1
11+
})
12+
}
13+
14+
render () {
15+
return <p onClick={this.click.bind(this)}>count: {this.state.count}</p>
16+
}
17+
}

0 commit comments

Comments
 (0)