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

Commit bf07210

Browse files
bahmutovlarrifax
andauthored
fix: set Next.js pages to load from /pages folder (#518)
Co-authored-by: Kristian Tryggestad <kristian.tryggestad@gmail.com>
1 parent f7c36a1 commit bf07210

File tree

6 files changed

+46
-12
lines changed

6 files changed

+46
-12
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import * as React from 'react'
2+
3+
import { hello } from './helpers';
4+
5+
export function UsingHelper() {
6+
return (
7+
<>{hello()}</>
8+
)
9+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const hello = () => 'Hello World!'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './hello-world'
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference types="cypress" />
2+
import * as React from 'react'
3+
import { mount } from 'cypress-react-unit-test'
4+
import { UsingHelper } from '../../components/UsingHelper'
5+
6+
describe('Component using helper with * export', () => {
7+
it('Renders component', () => {
8+
mount(<UsingHelper />)
9+
10+
cy.contains('Hello World!')
11+
})
12+
})

examples/nextjs/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
"dev": "next",
88
"build": "next build",
99
"start": "next start",
10-
"test": "cypress-expect run --min-passing 10",
10+
"test": "cypress-expect run --min-passing 11",
1111
"cy:open": "cypress open",
1212
"build:static": "next build && next out",
13-
"check-coverage": "check-coverage components/Search.jsx pages/index.js pages/router.js",
14-
"only-covered": "only-covered components/Search.jsx pages/index.js pages/router.js"
13+
"check-coverage": "check-coverage components/Search.jsx pages/index.js pages/router.js components/UsingHelper.jsx",
14+
"only-covered": "only-covered components/Search.jsx components/UsingHelper.jsx components/helpers/hello-world.js components/helpers/index.js pages/index.js pages/router.js"
1515
},
1616
"devDependencies": {
1717
"@mdx-js/loader": "1.6.18",

plugins/next/file-preprocessor.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// @ts-check
2+
const path = require('path')
23
const debug = require('debug')('cypress-react-unit-test')
34
const loadConfig = require('next/dist/next-server/server/config').default
45
const getNextJsBaseWebpackConfig = require('next/dist/build/webpack-config')
@@ -11,19 +12,29 @@ async function getNextWebpackConfig(config) {
1112
config && config.env && config.env.coverage === false
1213

1314
debug('coverage is disabled? %o', { coverageIsDisabled })
15+
debug('Cypress project %o', {
16+
projectRoot: config.projectRoot,
17+
componentFolder: config.componentFolder,
18+
})
1419

1520
const nextConfig = await loadConfig('development', config.projectRoot)
21+
22+
const configOptions = {
23+
buildId: `cypress-react-unit-test-${Math.random().toString()}`,
24+
config: nextConfig,
25+
dev: false,
26+
isServer: false,
27+
// assuming the Next.js project has the entire pages in "/pages" subfolder
28+
// https://github.com/bahmutov/cypress-react-unit-test/pull/517
29+
pagesDir: path.join(config.projectRoot, 'pages'),
30+
entrypoints: {},
31+
rewrites: [],
32+
}
33+
debug('Next config options %o', configOptions)
34+
1635
const nextWebpackConfig = await getNextJsBaseWebpackConfig(
1736
config.projectRoot,
18-
{
19-
buildId: `cypress-react-unit-test-${Math.random().toString()}`,
20-
config: nextConfig,
21-
dev: false,
22-
isServer: false,
23-
pagesDir: config.projectRoot,
24-
entrypoints: {},
25-
rewrites: [],
26-
},
37+
configOptions,
2738
)
2839

2940
debug('resolved next.js webpack options: %o', nextWebpackConfig)

0 commit comments

Comments
 (0)