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

Commit

Permalink
fix: set Next.js pages to load from /pages folder (#518)
Browse files Browse the repository at this point in the history
Co-authored-by: Kristian Tryggestad <[email protected]>
  • Loading branch information
bahmutov and larrifax authored Oct 27, 2020
1 parent f7c36a1 commit bf07210
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 12 deletions.
9 changes: 9 additions & 0 deletions examples/nextjs/components/UsingHelper.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as React from 'react'

import { hello } from './helpers';

export function UsingHelper() {
return (
<>{hello()}</>
)
}
1 change: 1 addition & 0 deletions examples/nextjs/components/helpers/hello-world.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const hello = () => 'Hello World!'
1 change: 1 addition & 0 deletions examples/nextjs/components/helpers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './hello-world'
12 changes: 12 additions & 0 deletions examples/nextjs/cypress/components/UsingHelper.spec.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/// <reference types="cypress" />
import * as React from 'react'
import { mount } from 'cypress-react-unit-test'
import { UsingHelper } from '../../components/UsingHelper'

describe('Component using helper with * export', () => {
it('Renders component', () => {
mount(<UsingHelper />)

cy.contains('Hello World!')
})
})
6 changes: 3 additions & 3 deletions examples/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
"dev": "next",
"build": "next build",
"start": "next start",
"test": "cypress-expect run --min-passing 10",
"test": "cypress-expect run --min-passing 11",
"cy:open": "cypress open",
"build:static": "next build && next out",
"check-coverage": "check-coverage components/Search.jsx pages/index.js pages/router.js",
"only-covered": "only-covered components/Search.jsx pages/index.js pages/router.js"
"check-coverage": "check-coverage components/Search.jsx pages/index.js pages/router.js components/UsingHelper.jsx",
"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"
},
"devDependencies": {
"@mdx-js/loader": "1.6.18",
Expand Down
29 changes: 20 additions & 9 deletions plugins/next/file-preprocessor.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @ts-check
const path = require('path')
const debug = require('debug')('cypress-react-unit-test')
const loadConfig = require('next/dist/next-server/server/config').default
const getNextJsBaseWebpackConfig = require('next/dist/build/webpack-config')
Expand All @@ -11,19 +12,29 @@ async function getNextWebpackConfig(config) {
config && config.env && config.env.coverage === false

debug('coverage is disabled? %o', { coverageIsDisabled })
debug('Cypress project %o', {
projectRoot: config.projectRoot,
componentFolder: config.componentFolder,
})

const nextConfig = await loadConfig('development', config.projectRoot)

const configOptions = {
buildId: `cypress-react-unit-test-${Math.random().toString()}`,
config: nextConfig,
dev: false,
isServer: false,
// assuming the Next.js project has the entire pages in "/pages" subfolder
// https://github.com/bahmutov/cypress-react-unit-test/pull/517
pagesDir: path.join(config.projectRoot, 'pages'),
entrypoints: {},
rewrites: [],
}
debug('Next config options %o', configOptions)

const nextWebpackConfig = await getNextJsBaseWebpackConfig(
config.projectRoot,
{
buildId: `cypress-react-unit-test-${Math.random().toString()}`,
config: nextConfig,
dev: false,
isServer: false,
pagesDir: config.projectRoot,
entrypoints: {},
rewrites: [],
},
configOptions,
)

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

0 comments on commit bf07210

Please sign in to comment.