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

Commit e693b50

Browse files
authored
fix: collect code coverage for Next.js components (#440)
1 parent f535292 commit e693b50

File tree

8 files changed

+40
-34
lines changed

8 files changed

+40
-34
lines changed

examples/nextjs/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,6 @@ cy.contains(
5555

5656
Find more examples in [Page.spec.jsx](./cypress/components/Page.spec.jsx).
5757

58-
## Coverage
58+
## Mocking imports
5959

60-
Code coverage is not working yet, see issue [431](https://github.com/bahmutov/cypress-react-unit-test/issues/431). It was disabled in [cypress.json](cypress.json)
60+
Mocking imports is not working yet, seems the plugin we are inserting for loose mode causes problems, see issue [439](https://github.com/bahmutov/cypress-react-unit-test/issues/439).

examples/nextjs/cypress.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
"experimentalFetchPolyfill": true,
77
"componentFolder": "cypress/components",
88
"env": {
9-
"coverage": false
9+
"coverage": true
1010
}
1111
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference types="cypress" />
2+
import { mount } from 'cypress-react-unit-test'
3+
import HelloWorld from '../../components/HelloWorld.mdx'
4+
5+
describe('MDX component', () => {
6+
it('Renders using custom next.config.js', () => {
7+
mount(<HelloWorld />)
8+
9+
cy.contains('Hello').should('have.css', 'fontWeight', '700')
10+
cy.contains('This is JSX')
11+
.parent()
12+
.should('have.css', 'background-color', 'rgb(255, 99, 71)')
13+
})
14+
})
Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/// <reference types="cypress" />
2-
import * as React from 'react'
2+
// import * as React from 'react'
33
import { mount } from 'cypress-react-unit-test'
44
import { Search } from '../../components/Search'
5-
import HelloWorld from '../../components/HelloWorld.mdx'
65

76
describe('<Search /> NextJS component', () => {
87
it('Renders component', () => {
@@ -11,13 +10,4 @@ describe('<Search /> NextJS component', () => {
1110
cy.get('input').type('124152')
1211
cy.contains('.search-text', '124152').should('be.visible')
1312
})
14-
15-
it('Renders mdx component using custom next.config.js', () => {
16-
mount(<HelloWorld />)
17-
18-
cy.contains('Hello').should('have.css', 'fontWeight', '700')
19-
cy.contains('This is JSX')
20-
.parent()
21-
.should('have.css', 'background-color', 'rgb(255, 99, 71)')
22-
})
2313
})

examples/nextjs/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
"test": "node ../../scripts/cypress-expect run --passing 7",
1111
"cy:open": "../../node_modules/.bin/cypress open",
1212
"build:static": "next build && next out",
13-
"check-coverage": "echo no code coverage yet",
14-
"only-covered": "echo no code coverage yet"
13+
"check-coverage": "../../node_modules/.bin/check-coverage components/Search.jsx && ../../node_modules/.bin/check-coverage pages/index.js",
14+
"only-covered": "../../node_modules/.bin/only-covered components/Search.jsx pages/index.js"
1515
},
1616
"devDependencies": {
1717
"cypress-react-unit-test": "file:../.."

package-lock.json

Lines changed: 16 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@
136136
"@zeit/next-sass": "^1.0.1",
137137
"babel-plugin-istanbul": "6.0.0",
138138
"debug": "4.1.1",
139-
"find-webpack": "2.0.0",
139+
"find-webpack": "2.1.0",
140140
"framer-motion": "2.6.13",
141141
"i18next": "19.7.0",
142142
"mime-types": "2.1.26",

plugins/next/file-preprocessor.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ async function getNextWebpackConfig(config) {
3535
{
3636
coverage: !coverageIsDisabled,
3737
// insert Babel plugin to mock named imports
38-
looseModules: true,
38+
// disable because causes an error with double definitions
39+
// https://github.com/bahmutov/cypress-react-unit-test/issues/439
40+
looseModules: false,
3941
},
4042
nextWebpackConfig,
4143
)

0 commit comments

Comments
 (0)