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

Commit 271235e

Browse files
authored
feat: allow importing fixture files in react-scripts tests (#290)
For example, from `src/App.spec.js` you can now do ```js // we can directly import JSON fixture files // from the local JSON file import names from './names.json' // try importing JavaScript import { add } from '../cypress/fixtures/add' // try importing JSON from fixtures folder import fixtureNames from '../cypress/fixtures/names.json' ```
1 parent aac981e commit 271235e

File tree

11 files changed

+39
-10
lines changed

11 files changed

+39
-10
lines changed

examples/react-scripts/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
A typical project using `react-scripts` with components and matching component tests residing in the [src](src) folder.
44

5-
Note: run `npm install` in this folder to symlink `cypress-react-unit-test` dependency.
5+
Note: run `npm install` in this folder to symlink the `cypress-react-unit-test` dependency.
66

77
```shell
8+
npm install
89
npm run cy:open
910
# or just run headless tests
1011
npm test

examples/react-scripts/cypress.json

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{
2-
"fixturesFolder": false,
32
"testFiles": "**/*cy-spec.js",
43
"viewportWidth": 500,
54
"viewportHeight": 800,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const add = (a, b) => a + b
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
["joe", "mary"]

examples/react-scripts/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"scripts": {
66
"test": "../../node_modules/.bin/cypress run",
77
"cy:open": "../../node_modules/.bin/cypress open",
8-
"check-coverage": "../../node_modules/.bin/check-coverage src/App.js src/calc.js src/Child.js",
9-
"only-covered": "../../node_modules/.bin/only-covered src/App.js src/calc.js src/Child.js"
8+
"check-coverage": "../../node_modules/.bin/check-coverage src/App.js src/calc.js src/Child.js cypress/fixtures/add.js",
9+
"only-covered": "../../node_modules/.bin/only-covered src/App.js src/calc.js src/Child.js cypress/fixtures/add.js"
1010
},
1111
"devDependencies": {
1212
"cypress-react-unit-test": "file:../.."
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// <reference types="cypress" />
2+
// we can directly import JSON fixture files
3+
// from the local JSON file
4+
import names from './names.json'
5+
// try importing JavaScript
6+
import { add } from '../cypress/fixtures/add'
7+
// try importing JSON from fixtures folder
8+
import fixtureNames from '../cypress/fixtures/names.json'
9+
10+
describe('fixtures', () => {
11+
it('imports the array', () => {
12+
expect(names).to.deep.equal(['joe', 'mary'])
13+
expect(fixtureNames).to.deep.equal(['joe', 'mary'])
14+
expect(add(2, 4)).to.equal(6)
15+
})
16+
})

examples/react-scripts/src/names.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
["joe", "mary"]

examples/sass-and-ts/README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
55
![Sass test](images/sass.png)
66

7-
To run, need to install link first
7+
Note: run `npm install` in this folder to symlink the `cypress-react-unit-test` dependency.
88

99
```
1010
npm install
11+
npm run cy:open
12+
# or just run headless tests
13+
npm test
1114
```
1215

1316
Note that Node Sass is a binary dependency, thus we need to run it using the same system version of Node as we installed. See [cypress.json](cypress.json) file.

package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126
"@cypress/webpack-preprocessor": "5.4.1",
127127
"babel-plugin-istanbul": "6.0.0",
128128
"debug": "4.1.1",
129-
"find-webpack": "1.13.0",
129+
"find-webpack": "1.14.0",
130130
"mime-types": "2.1.26"
131131
},
132132
"release": {

plugins/cra-v3/file-preprocessor.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,17 @@ module.exports = config => {
3333
config && config.env && config.env.coverage === false
3434
debug('coverage is disabled? %o', { coverageIsDisabled })
3535
debug('component test folder: %s', config.componentFolder)
36+
debug('fixtures folder', config.fixturesFolder)
37+
38+
const additionalFolders = [config.componentFolder]
39+
// user can disable fixtures folder, so check first
40+
if (config.fixturesFolder) {
41+
additionalFolders.push(config.fixturesFolder)
42+
}
3643

3744
const opts = {
3845
reactScripts: true,
39-
addFolderToTranspile: config.componentFolder,
46+
addFolderToTranspile: additionalFolders,
4047
coverage: !coverageIsDisabled,
4148
// insert Babel plugin to mock named imports
4249
looseModules: true,

0 commit comments

Comments
 (0)