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

Commit 6599b21

Browse files
chore: Babel + Typescript example (#408)
* Add babel-typescipt example * Run new example on CI * chore: run markdown links checks only on main (#404) * chore: Improve typings (#405) * check out files before checking links * just use normal cypress run job * Improve Readme Co-authored-by: Gleb Bahmutov <gleb.bahmutov@gmail.com>
1 parent 5525a05 commit 6599b21

File tree

15 files changed

+209
-1
lines changed

15 files changed

+209
-1
lines changed

circle.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,19 @@ workflows:
6969
command: npm test
7070
store_artifacts: true
7171

72+
- cypress/run:
73+
name: Example Babel + Typescript
74+
requires:
75+
- Install
76+
executor: cypress/base-12
77+
# each example installs "cypress-react-unit-test" as a local dependency (symlink)
78+
install-command: npm install
79+
verify-command: echo 'Already verified'
80+
no-workspace: true
81+
working_directory: examples/using-babel-typesceript
82+
command: npm test
83+
store_artifacts: true
84+
7285
- cypress/run:
7386
name: Example React Scripts
7487
requires:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# example: using-babel
2+
3+
> Component testing for typescript projects using Babel config with `@babel/preset-typescript`
4+
5+
![Example component test](images/dynamic.gif)
6+
7+
## Usage
8+
9+
1. Make sure the root project has been built .
10+
11+
```bash
12+
# in the root of the project
13+
npm install
14+
npm run build
15+
```
16+
17+
2. Run `npm install` in this folder to symlink the `cypress-react-unit-test` dependency.
18+
19+
```bash
20+
# in this folder
21+
npm install
22+
```
23+
24+
3. Start Cypress
25+
26+
```bash
27+
npm run cy:open
28+
# or just run headless tests
29+
npm test
30+
```
31+
32+
## Specs
33+
34+
See spec files [src/\*.spec.js](src). The specs are bundled using [babel.config.js](babel.config.js) settings via [cypress/plugins/index.js](cypress/plugins/index.js) file that includes file preprocessor.
35+
36+
```js
37+
// let's bundle spec files and the components they include using
38+
// the same bundling settings as the project by loading .babelrc
39+
const preprocessor = require('cypress-react-unit-test/plugins/babelrc')
40+
module.exports = (on, config) => {
41+
preprocessor(on, config)
42+
// IMPORTANT to return the config object
43+
// with the any changed environment variables
44+
return config
45+
}
46+
```
47+
48+
## Mocking
49+
50+
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
51+
52+
```ts
53+
// component imports named ES6 import from "calc.js
54+
import { getRandomNumber } from './calc'
55+
const Component = () => {
56+
// then calls it
57+
const n = getRandomNumber()
58+
return <div className="random">{n}</div>
59+
}
60+
```
61+
62+
The test can mock that import before mounting the component
63+
64+
```js
65+
import Component from './Component'
66+
import * as calc from './calc'
67+
describe('Component', () => {
68+
it('mocks call from the component', () => {
69+
cy.stub(calc, 'getRandomNumber')
70+
.returns(777)
71+
mount(<Component />)
72+
})
73+
})
74+
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module.exports = {
2+
presets: [
3+
'@babel/preset-env',
4+
'@babel/preset-typescript',
5+
'@babel/preset-react',
6+
],
7+
plugins: ['@babel/plugin-proposal-class-properties'],
8+
env: {
9+
// place plugins for Cypress tests into "test" environment
10+
// so that production bundle is not instrumented
11+
test: {
12+
plugins: [
13+
// during Cypress tests we want to instrument source code
14+
// to get code coverage from tests
15+
'babel-plugin-istanbul',
16+
// we also want to export ES6 modules as objects
17+
// to allow mocking named imports
18+
[
19+
'@babel/plugin-transform-modules-commonjs',
20+
{
21+
loose: true,
22+
},
23+
],
24+
],
25+
},
26+
},
27+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"fixturesFolder": false,
3+
"testFiles": "**/*spec.tsx",
4+
"viewportWidth": 500,
5+
"viewportHeight": 500,
6+
"experimentalComponentTesting": true,
7+
"componentFolder": "src"
8+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/// <reference types="cypress" />
2+
describe('integration spec', () => {
3+
it('works', () => {
4+
expect(1).to.equal(1)
5+
})
6+
})
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// let's bundle spec files and the components they include using
2+
// the same bundling settings as the project by loading .babelrc
3+
// https://github.com/bahmutov/cypress-react-unit-test#install
4+
const preprocessor = require('cypress-react-unit-test/plugins/babelrc')
5+
module.exports = (on, config) => {
6+
preprocessor(on, config)
7+
// IMPORTANT to return the config object
8+
// with the any changed environment variables
9+
return config
10+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('cypress-react-unit-test/dist/hooks')
753 KB
Loading
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "example-using-babel",
3+
"description": "Component testing for projects using Babel config",
4+
"private": true,
5+
"scripts": {
6+
"test": "node ../../scripts/cypress-expect run --passing 17",
7+
"cy:open": "../../node_modules/.bin/cypress open"
8+
},
9+
"devDependencies": {
10+
"cypress-react-unit-test": "file:../.."
11+
}
12+
}

0 commit comments

Comments
 (0)