From e8c8671a5b648fffde6db216b9af72427d4010a5 Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Tue, 27 Oct 2020 11:27:58 -0400 Subject: [PATCH] fix: use pages or src/pages folder for Next.js (#519) --- circle.yml | 19 ------------------- .../advanced/mocking-axios/1-users.spec.js | 3 ++- plugins/next/file-preprocessor.js | 14 +++++++++++--- 3 files changed, 13 insertions(+), 23 deletions(-) diff --git a/circle.yml b/circle.yml index 5590ff79..904d07f3 100644 --- a/circle.yml +++ b/circle.yml @@ -47,7 +47,6 @@ workflows: npm install echo ***rename root node_modules to avoid accidental dependencies*** mv ../../node_modules ../../no_modules - verify-command: echo 'Already verified' no-workspace: true working_directory: examples/a11y command: npm test @@ -66,7 +65,6 @@ workflows: npm install echo ***rename root node_modules to avoid accidental dependencies*** mv ../../node_modules ../../no_modules - verify-command: echo 'Already verified' no-workspace: false working_directory: examples/using-babel command: npm test @@ -85,7 +83,6 @@ workflows: npm install echo ***rename root node_modules to avoid accidental dependencies*** mv ../../node_modules ../../no_modules - verify-command: echo 'Already verified' no-workspace: true working_directory: examples/using-babel-typescript command: npm test @@ -106,7 +103,6 @@ workflows: mv ../../node_modules ../../no_modules echo ***React version*** npm ls react react-dom - verify-command: echo 'Already verified' no-workspace: true working_directory: examples/react-scripts-cucumber command: npm test @@ -127,7 +123,6 @@ workflows: mv ../../node_modules ../../no_modules echo ***React version*** npm ls react react-dom - verify-command: echo 'Already verified' no-workspace: true working_directory: examples/react-scripts command: npm test @@ -155,7 +150,6 @@ workflows: mv ../../node_modules ../../no_modules echo ***React version*** npm ls react react-dom - verify-command: echo 'Already verified' no-workspace: true working_directory: examples/rewired command: npm test @@ -174,7 +168,6 @@ workflows: npm install echo ***rename root node_modules to avoid accidental dependencies*** mv ../../node_modules ../../no_modules - verify-command: echo 'Already verified' no-workspace: true working_directory: examples/nextjs command: npm test @@ -202,7 +195,6 @@ workflows: npm install echo ***rename root node_modules to avoid accidental dependencies*** mv ../../node_modules ../../no_modules - verify-command: echo 'Already verified' no-workspace: true working_directory: examples/react-scripts-folder command: npm test @@ -229,7 +221,6 @@ workflows: npm install echo ***rename root node_modules to avoid accidental dependencies*** mv ../../node_modules ../../no_modules - verify-command: echo 'Already verified' no-workspace: true working_directory: examples/tailwind command: | @@ -257,7 +248,6 @@ workflows: npm install echo ***rename root node_modules to avoid accidental dependencies*** mv ../../node_modules ../../no_modules - verify-command: echo 'Already verified' no-workspace: true working_directory: examples/webpack-file command: npm test @@ -283,7 +273,6 @@ workflows: npm install echo ***rename root node_modules to avoid accidental dependencies*** mv ../../node_modules ../../no_modules - verify-command: echo 'Already verified' no-workspace: true working_directory: examples/webpack5-file command: npm test @@ -310,7 +299,6 @@ workflows: npm install echo ***rename root node_modules to avoid accidental dependencies*** mv ../../node_modules ../../no_modules - verify-command: echo 'Already verified' no-workspace: true working_directory: examples/rollup command: npm test @@ -329,7 +317,6 @@ workflows: npm install echo ***rename root node_modules to avoid accidental dependencies*** mv ../../node_modules ../../no_modules - verify-command: echo 'Already verified' no-workspace: true working_directory: examples/webpack-options command: npm test @@ -357,7 +344,6 @@ workflows: npm install echo ***rename root node_modules to avoid accidental dependencies*** mv ../../node_modules ../../no_modules - verify-command: echo 'Already verified' no-workspace: true working_directory: examples/sass-and-ts command: npm test @@ -383,7 +369,6 @@ workflows: npm install echo ***rename root node_modules to avoid accidental dependencies*** mv ../../node_modules ../../no_modules - verify-command: echo 'Already verified' no-workspace: true working_directory: examples/snapshots command: npm test @@ -409,7 +394,6 @@ workflows: npm install echo ***rename root node_modules to avoid accidental dependencies*** mv ../../node_modules ../../no_modules - verify-command: echo 'Already verified' no-workspace: true working_directory: examples/visual-sudoku command: npm test @@ -431,7 +415,6 @@ workflows: npm install echo ***rename root node_modules to avoid accidental dependencies*** mv ../../node_modules ../../no_modules - verify-command: echo 'Already verified' no-workspace: true working_directory: examples/visual-testing-with-applitools # to correctly run this job, we need Applitools token @@ -459,7 +442,6 @@ workflows: npm install echo ***rename root node_modules to avoid accidental dependencies*** mv ../../node_modules ../../no_modules - verify-command: echo 'Already verified' no-workspace: true working_directory: examples/visual-testing-with-percy # run Percy agent and then run Cypress component tests @@ -480,7 +462,6 @@ workflows: npm install echo ***rename root node_modules to avoid accidental dependencies*** mv ../../node_modules ../../no_modules - verify-command: echo 'Already verified' no-workspace: true working_directory: examples/visual-testing-with-happo command: npm run test:happo diff --git a/cypress/component/advanced/mocking-axios/1-users.spec.js b/cypress/component/advanced/mocking-axios/1-users.spec.js index 82f4a097..1e15514e 100644 --- a/cypress/component/advanced/mocking-axios/1-users.spec.js +++ b/cypress/component/advanced/mocking-axios/1-users.spec.js @@ -8,7 +8,8 @@ const Axios = require('axios') describe('Mocking Axios', () => { it('shows real users', () => { mount() - cy.get('li').should('have.length', 3) + // the first Ajax request could be slow + cy.get('li', { timeout: 15000 }).should('have.length', 3) }) // https://github.com/bahmutov/cypress-react-unit-test/issues/338 diff --git a/plugins/next/file-preprocessor.js b/plugins/next/file-preprocessor.js index f8cba139..b6077767 100644 --- a/plugins/next/file-preprocessor.js +++ b/plugins/next/file-preprocessor.js @@ -1,5 +1,6 @@ // @ts-check const path = require('path') +const fs = require('fs') 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') @@ -19,14 +20,21 @@ async function getNextWebpackConfig(config) { const nextConfig = await loadConfig('development', config.projectRoot) + // determine where the Next.js keeps its pages in this project + // https://nextjs.org/docs/advanced-features/src-directory + const rootPages = path.join(config.projectRoot, 'pages') + const srcPages = path.join(config.projectRoot, 'src', 'pages') + const pagesDir = fs.existsSync(rootPages) ? rootPages : srcPages + if (!fs.existsSync(pagesDir)) { + throw new Error(`Cannot pages folder ${pagesDir}`) + } + 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'), + pagesDir, entrypoints: {}, rewrites: [], }