You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vue2+Webpack项目Cypress组件测试报process is not defined错误求助

Vue 2 + Webpack PWA项目Cypress组件测试报错:process is not defined

我在基于Vue 2和Webpack构建的PWA项目中执行组件测试时,遇到了process is not defined的错误,导致无法开展所需测试。

项目依赖

"dependencies": {
    "compressorjs": "^1.1.1",
    "dotenv": "^16.0.3",
    "jquery": "^3.4.1",
    "lodash": "^4.17.21",
    "npm": "^6.10.0",
    "register-service-worker": "^1.5.2",
    "vue": "^2.6.14",
    "vue-router": "^3.0.1",
    "vuex": "^3.0.1",
    "vuex-persist": "^3.1.3"
  },
  "devDependencies": {
    "@babel/cli": "^7.2.3",
    "@babel/core": "^7.12.16",
    "@babel/polyfill": "^7.2.5",
    "@babel/preset-env": "^7.21.4",
    "@vue/cli-plugin-babel": "~5.0.0",
    "@vue/cli-plugin-eslint": "~5.0.0",
    "@vue/cli-plugin-pwa": "^3.1.1",
    "@vue/cli-service": "~5.0.0",
    "babel-helper-vue-jsx-merge-props": "^2.0.3",
    "babel-plugin-syntax-jsx": "^6.18.0",
    "babel-plugin-transform-vue-jsx": "^3.7.0",
    "babel-preset-env": "^1.7.0",
    "copyfiles": "^1.2.0",
    "css-loader": "^6.7.3",
    "cypress": "^12.11.0",
    "node-sass": "^4.12.0",
    "sass-loader": "^7.1.0",
    "vue-cli-plugin-vuetify": "^0.1.6",
    "vue-loader": "^15.10.1",
    "vue-template-compiler": "^2.7.14",
    "web-push": "^3.3.3",
    "webpack": "^5.80.0",
    "workbox-cli": "^3.5.0"
  }

Webpack配置文件

const { VueLoaderPlugin } = require('vue-loader')

module.exports = {
  entry: ["@babel/polyfill", "./app.vue"],
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          'css-loader'
        ]
      },
      {
        test: /\.vue$/,
        loader: 'vue-loader'
      }
    ]
  },
  plugins: [
    new VueLoaderPlugin()
  ]
};

Cypress配置文件

const { defineConfig } = require("cypress");
const webpackConfig = require('./src/webpack.config');

module.exports = defineConfig({
  component: {
    devServer: {
      framework: "vue",
      bundler: "webpack",
      webpackConfig
    },
  },
});

测试文件

import Loading from './Loading.vue'

describe('<Loading />', () => {
  it('renders', () => {
    cy.mount(Loading)
  })
})

我推测这是依赖冲突或加载器库相关问题,尝试了论坛上提出的所有解决方案,但均未奏效。新建了一个Vue 2项目,测试可正常运行,但找不到当前项目问题的原因。


内容的提问来源于stack exchange,提问作者Tomás Caro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 07:55:02