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

Commit

Permalink
feat: allow importing fixture files in react-scripts tests (#290)
Browse files Browse the repository at this point in the history
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'
```
  • Loading branch information
bahmutov authored Jun 13, 2020
1 parent aac981e commit 271235e
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 10 deletions.
3 changes: 2 additions & 1 deletion examples/react-scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

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

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

```shell
npm install
npm run cy:open
# or just run headless tests
npm test
Expand Down
1 change: 0 additions & 1 deletion examples/react-scripts/cypress.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"fixturesFolder": false,
"testFiles": "**/*cy-spec.js",
"viewportWidth": 500,
"viewportHeight": 800,
Expand Down
1 change: 1 addition & 0 deletions examples/react-scripts/cypress/fixtures/add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const add = (a, b) => a + b
1 change: 1 addition & 0 deletions examples/react-scripts/cypress/fixtures/names.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["joe", "mary"]
4 changes: 2 additions & 2 deletions examples/react-scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"scripts": {
"test": "../../node_modules/.bin/cypress run",
"cy:open": "../../node_modules/.bin/cypress open",
"check-coverage": "../../node_modules/.bin/check-coverage src/App.js src/calc.js src/Child.js",
"only-covered": "../../node_modules/.bin/only-covered src/App.js src/calc.js src/Child.js"
"check-coverage": "../../node_modules/.bin/check-coverage src/App.js src/calc.js src/Child.js cypress/fixtures/add.js",
"only-covered": "../../node_modules/.bin/only-covered src/App.js src/calc.js src/Child.js cypress/fixtures/add.js"
},
"devDependencies": {
"cypress-react-unit-test": "file:../.."
Expand Down
16 changes: 16 additions & 0 deletions examples/react-scripts/src/fixture.cy-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// <reference types="cypress" />
// 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'

describe('fixtures', () => {
it('imports the array', () => {
expect(names).to.deep.equal(['joe', 'mary'])
expect(fixtureNames).to.deep.equal(['joe', 'mary'])
expect(add(2, 4)).to.equal(6)
})
})
1 change: 1 addition & 0 deletions examples/react-scripts/src/names.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["joe", "mary"]
5 changes: 4 additions & 1 deletion examples/sass-and-ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
![Sass test](images/sass.png)

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

```
npm install
npm run cy:open
# or just run headless tests
npm test
```

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.
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
"@cypress/webpack-preprocessor": "5.4.1",
"babel-plugin-istanbul": "6.0.0",
"debug": "4.1.1",
"find-webpack": "1.13.0",
"find-webpack": "1.14.0",
"mime-types": "2.1.26"
},
"release": {
Expand Down
9 changes: 8 additions & 1 deletion plugins/cra-v3/file-preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,17 @@ module.exports = config => {
config && config.env && config.env.coverage === false
debug('coverage is disabled? %o', { coverageIsDisabled })
debug('component test folder: %s', config.componentFolder)
debug('fixtures folder', config.fixturesFolder)

const additionalFolders = [config.componentFolder]
// user can disable fixtures folder, so check first
if (config.fixturesFolder) {
additionalFolders.push(config.fixturesFolder)
}

const opts = {
reactScripts: true,
addFolderToTranspile: config.componentFolder,
addFolderToTranspile: additionalFolders,
coverage: !coverageIsDisabled,
// insert Babel plugin to mock named imports
looseModules: true,
Expand Down

0 comments on commit 271235e

Please sign in to comment.