Skip to content

Commit

Permalink
fix: configure proper pages directory for next application (#18009)
Browse files Browse the repository at this point in the history
* fix: find pages directory for next applications

* convert new files to typescript and add transpile step

* add tsconfig for plugins
  • Loading branch information
ZachJW34 committed Sep 14, 2021
1 parent 0284b9d commit 70c7c36
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 3 deletions.
7 changes: 5 additions & 2 deletions npm/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@
"description": "Test React components using Cypress",
"main": "dist/cypress-react.cjs.js",
"scripts": {
"build": "rimraf dist && yarn rollup -c rollup.config.js",
"build": "rimraf dist && yarn transpile-plugins && yarn rollup -c rollup.config.js",
"build-prod": "yarn build",
"cy:open": "node ../../scripts/cypress.js open-ct",
"cy:open:debug": "node --inspect-brk ../../scripts/start.js --component-testing --run-project ${PWD}",
"cy:run": "node ../../scripts/cypress.js run-ct",
"cy:run:debug": "node --inspect-brk ../../scripts/start.js --component-testing --run-project ${PWD}",
"pretest": "yarn transpile",
"test": "yarn cy:run",
"test": "yarn test-unit && yarn test-component",
"test-unit": "mocha -r @packages/ts/register test/**/*.spec.*",
"test-component": "yarn cy:run",
"test-ci": "node ../../scripts/run-ct-examples.js --examplesList=./examples.env",
"transpile": "tsc",
"transpile-plugins": "yarn transpile --project ./plugins",
"watch": "yarn build --watch --watch.exclude ./dist/**/*"
},
"dependencies": {
Expand Down
3 changes: 2 additions & 1 deletion npm/react/plugins/next/findNextWebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/// <reference types="next" />
const debug = require('debug')('@cypress/react')
const getNextJsBaseWebpackConfig = require('next/dist/build/webpack-config').default
const { findPagesDir } = require('../../dist/next/findPagesDir')

async function getNextWebpackConfig (config) {
let loadConfig
Expand All @@ -26,7 +27,7 @@ async function getNextWebpackConfig (config) {
config: nextConfig,
dev: true,
isServer: false,
pagesDir: config.projectRoot,
pagesDir: findPagesDir(config.projectRoot),
entrypoints: {},
rewrites: { fallback: [], afterFiles: [], beforeFiles: [] },
},
Expand Down
33 changes: 33 additions & 0 deletions npm/react/plugins/next/findPagesDir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as fs from 'fs'
import * as path from 'path'

const existsSync = (file: string) => {
try {
fs.accessSync(file, fs.constants.F_OK)

return true
} catch (_) {
return false
}
}

/**
* Next allows the `pages` directory to be located at either
* `${projectRoot}/pages` or `${projectRoot}/src/pages`.
* If neither is found, return projectRoot
*/
export function findPagesDir (projectRoot: string) {
// prioritize ./pages over ./src/pages
let pagesDir = path.join(projectRoot, 'pages')

if (existsSync(pagesDir)) {
return pagesDir
}

pagesDir = path.join(projectRoot, 'src/pages')
if (existsSync(pagesDir)) {
return pagesDir
}

return projectRoot
}
11 changes: 11 additions & 0 deletions npm/react/plugins/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"rootDir": "./",
"types": [
"cypress",
"node"
]
},
"include": ["./**/*.ts"]
}
Empty file.
Empty file.
19 changes: 19 additions & 0 deletions npm/react/test/unit/findPagesDir.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { expect } from 'chai'
import * as path from 'path'
import { findPagesDir } from '../../plugins/next/findPagesDir'

describe('Next.js findPagesDir', () => {
it('should find the correct pagesDir', () => {
const nextPluginFixturePath = path.join(__dirname, '../fixtures/next-plugin')

let projectRoot = nextPluginFixturePath

expect(findPagesDir(projectRoot)).to.equal(projectRoot)

projectRoot = path.join(nextPluginFixturePath, 'next-project-one')
expect(findPagesDir(projectRoot)).to.equal(path.join(projectRoot, 'pages'))

projectRoot = path.join(nextPluginFixturePath, 'next-project-two')
expect(findPagesDir(projectRoot)).to.equal(path.join(projectRoot, 'src/pages'))
})
})

4 comments on commit 70c7c36

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 70c7c36 Sep 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/8.4.1/circle-develop-70c7c3678180d5408c144fa37f94ba5f5f8ceeb8/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 70c7c36 Sep 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AppVeyor has built the win32 ia32 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/8.4.1/appveyor-develop-70c7c3678180d5408c144fa37f94ba5f5f8ceeb8/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 70c7c36 Sep 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AppVeyor has built the win32 x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/8.4.1/appveyor-develop-70c7c3678180d5408c144fa37f94ba5f5f8ceeb8/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 70c7c36 Sep 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/8.4.1/circle-develop-70c7c3678180d5408c144fa37f94ba5f5f8ceeb8/cypress.tgz

Please sign in to comment.