Cypress React,找不到根元素来安装组件

Byt*_*tor 4 javascript testing reactjs webpack cypress

我正在尝试测试我的 React 应用程序,该应用程序是使用 cypress (create-react-app) 创建的。

由于某种原因,我收到此错误:

在此输入图像描述

正如你所看到的,它尝试通过 ROOT_ID 获取元素,ROOT_ID 它是一个 const:

在此输入图像描述

我发现解决这个问题的方法就是在 cypress/support/index.js 上导入

导入“cypress-react-unit-test/support”;

但正如你在这里看到的:

https://github.com/cypress-io/cypress-react-unit-test

该存储库已移至官方 cypress,我找不到等效的导入。

这是我的plugins/index.js

const findWebpack = require('find-webpack')
const webpackPreprocessor = require('@cypress/webpack-preprocessor')

/**
 * @type {Cypress.PluginConfig}
 */
module.exports = (on) => {
  // find the Webpack config used by react-scripts
  const webpackOptions = findWebpack.getWebpackOptions()

  if (!webpackOptions) {
    throw new Error('Could not find Webpack in this project ')
  }

  const cleanOptions = {
    reactScripts: true,
  }

  findWebpack.cleanForCypress(cleanOptions, webpackOptions)

  const options = {
    webpackOptions,
    watchOptions: {},
  }

  on('file:preprocessor', webpackPreprocessor(options))
}
Run Code Online (Sandbox Code Playgroud)

Byt*_*tor 7

显然我使用了错误的 cypress 模块。

这是用于反应测试

npx 赛普拉斯 open-ct

或者

npx 赛普拉斯 run-ct

赛普拉斯/插件/index.js:

const injectDevServer = require("@cypress/react/plugins/react-scripts")

module.exports = (on, config) => {
  injectDevServer(on, config)
  return config
}
Run Code Online (Sandbox Code Playgroud)