reactjs - 在 NextJS 应用程序中使用 Cypress 组件测试

标签 reactjs typescript next.js cypress

我想我需要一些帮助来了解如何使用 nextJS 的 cypress 插件来运行 Cypress Components Test

package.json


  "devDependencies": {
    "@cypress/react": "^5.3.4",
    "@cypress/webpack-dev-server": "^1.1.4",
    "@cypress/webpack-preprocessor": "^5.7.0",
    "@testing-library/cypress": "^7.0.6",
    "@types/lodash": "^4.14.168",
    "@types/node": "^14.14.36",
    "@types/react": "^17.0.3",
    "cypress": "^7.1.0",
    "sass": "^1.32.8",
    "typescript": "^4.2.3",
    "webpack-dev-server": "^3.11.2"
  }

cypress/plugins/index.ts

module.exports = (on, config) => {
  require('@cypress/react/plugins/next')(on, config)

  return config
}
尝试开始测试抛出 options.rewrites.map is not a function 错误
$ yarn cypress open-ct
yarn run v1.22.5
$ /Users/norfeldt/Repos/LoCali/locali-web/node_modules/.bin/cypress open-ct
info  - Loaded env from /Users/norfeldt/Repos/LoCali/locali-web/.env.local
Error [TypeError]: options.rewrites.map is not a function
    at new BuildManifestPlugin (/Users/norfeldt/Repos/LoCali/locali-web/node_modules/next/build/webpack/plugins/build-manifest-plugin.ts:94:38)
    at getBaseWebpackConfig (/Users/norfeldt/Repos/LoCali/locali-web/node_modules/next/build/webpack-config.ts:1133:9)
    at getNextWebpackConfig (/Users/norfeldt/Repos/LoCali/locali-web/node_modules/@cypress/react/plugins/next/findNextWebpackConfig.js:9:29)
    at findNextWebpackConfig (/Users/norfeldt/Repos/LoCali/locali-web/node_modules/@cypress/react/plugins/next/findNextWebpackConfig.js:36:24)
    at Object.handler (/Users/norfeldt/Repos/LoCali/locali-web/node_modules/@cypress/react/plugins/next/index.js:5:27)
TypeError: options.rewrites.map is not a function
    at new BuildManifestPlugin (/Users/norfeldt/Repos/LoCali/locali-web/node_modules/next/build/webpack/plugins/build-manifest-plugin.ts:94:38)
    at getBaseWebpackConfig (/Users/norfeldt/Repos/LoCali/locali-web/node_modules/next/build/webpack-config.ts:1133:9)
    at getNextWebpackConfig (/Users/norfeldt/Repos/LoCali/locali-web/node_modules/@cypress/react/plugins/next/findNextWebpackConfig.js:9:29)
    at findNextWebpackConfig (/Users/norfeldt/Repos/LoCali/locali-web/node_modules/@cypress/react/plugins/next/findNextWebpackConfig.js:36:24)
    at Object.handler (/Users/norfeldt/Repos/LoCali/locali-web/node_modules/@cypress/react/plugins/next/index.js:5:27)
我错过了什么吗?

最佳答案

我在 cypress discussion 上找到了一个可行的解决方案
只是要抛弃它对我有用的东西
将 NextJS 更新到最新版本。
然后我也做了

yarn add -D cypress @cypress/webpack-dev-server @cypress/react html-webpack-plugin@4 webpack@4 webpack-dev-serv

./tsconfig.json


{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": false,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "baseUrl": ".",
    "paths": {
      "~/*": ["*"]
    },
    "types": ["cypress", "@testing-library/cypress"]
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
  "exclude": ["node_modules"]
}

./cypress/tsconfig.json


{
  "extends": "../tsconfig.json",
  "include": ["../**/*.ts*"]
}
// ./cypress/plugins/index.ts
const injectDevServer = require('@cypress/react/plugins/next')

module.exports = (on, config) => {
  injectDevServer(on, config)
  return config
}
(获得 测试库 命令烘焙到 cypress 中的奖励)
yarn add -D @testing-library/cypress
// ./cypress/support/index.ts
/// <reference types="cypress" />

import '@testing-library/cypress/add-commands'
// ./components/Dishcard/index.spec.tsx
import React from 'react'
import { mount } from '@cypress/react'

import DishCard from '.'
import { DishesProvider } from '~/context/dishes'
import { CartProvider } from '~/context/cart'

describe('DishCard', () => {
  it('looks great', () => {
    mount(
      <CartProvider>
        <DishesProvider>
          <DishCard id="dish02" />
        </DishesProvider>
      </CartProvider>
    )
    cy.findByText('CHICKEN', { exact: false })
  })
})

关于reactjs - 在 NextJS 应用程序中使用 Cypress 组件测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67212766/

相关文章:

typescript - Nextjs 构建失败,错误为 'workerError'

css - 如何在不缓存 CSS 样式的情况下有条件地在 Next.js 中渲染组件?

javascript - 如何从项目中提取react和react-dom

javascript - 按钮删除列表项并更改其排序 | react

reactjs - 路径上的 Eslint 导入/无无关依赖错误

css - next.js 使用现在缺少的样式部署,styles.css 404 响应

reactjs - Next.js登录后如何重定向回私有(private)路由?

reactjs - MUI Select 组件中的自定义文本

javascript - 如何编写 Typescript 插件?

javascript - 如何更改输入文本并随后存储它?