jest ReferenceError:Vue未定义

oogrdqng  于 2023-04-12  发布在  Vue.js
关注(0)|答案(2)|浏览(227)

我们正在将Jest引入一个现有的项目。
然而,我写了一个示例测试代码。发生了上述错误。

ReferenceError: Vue is not defined

      1 | import User from "../components/modal/ad/AdAdd";
    > 2 | import { mount } from "@vue/test-utils";
        | ^
      3 |
      4 | describe("user component", () => {
      5 |   let wrapper;

我该怎么解决这个问题?

//User.test.js

import User from "../components/modal/ad/AdAdd";
import { mount } from "@vue/test-utils";

describe("user component", () => {
  let wrapper;
  beforeEach(() => {
    wrapper = mount(User);
  });
  test("render", () => {
    expect(wrapper.vm.oSid).toBe(0);
  });
});
export default {
  data() {
    return {
      Sid: 0,
      deleteList: [],
    };
  },
//package.json
"dependencies": {
    "eslint-plugin-jest": "^26.2.2",
    "vue": "^2.6.11",
    "vuetify": "^2.4.0",
    "vuex": "^3.4.0",
  },
  "devDependencies": {
    "@babel/core": "^7.18.0",
    "@babel/preset-env": "^7.18.0",
    "@types/jest": "^27.5.1",
    "@vue/cli-plugin-babel": "~4.5.0",
    "@vue/cli-service": "^3.0.5",
    "@vue/eslint-config-prettier": "^6.0.0",
    "@vue/test-utils": "^2.0.0",
    "babel-core": "^7.0.0-bridge.0",
    "babel-eslint": "^10.1.0",
    "babel-jest": "^28.1.0",
    "eslint": "^6.7.2",
    "eslint-plugin-prettier": "^3.3.1",
    "eslint-plugin-vue": "^6.2.2",
    "jest": "^28.1.0",
    "jest-environment-jsdom": "^28.1.0",
    "speed-measure-webpack-plugin": "^1.5.0",
    "uglifyjs-webpack-plugin": "^2.2.0",
    "vue-cli-plugin-vuetify": "~2.4.0",
    "vue-jest": "^3.0.7",
    "vue-template-compiler": "^2.6.11",
    "vuetify-loader": "^1.7.0"
  }

我刚挂载了,但显示视图未定义,不知道怎么解决。
怎么了?
我已经好几天没能解决上面的错误了。
我想解决它。任何帮助都将不胜感激。

jjhzyzn0

jjhzyzn01#

我在使用vue3和jest v.28时也遇到了同样的错误,解决的方法是添加

testEnvironmentOptions: {
       customExportConditions: ["node", "node-addons"],
    },

jest.config.js中。这将覆盖从exports加载到package.json中的库版本,如jest配置页面中所解释的那样。因为它默认为jest-environment-jsdom['browser'],并且我失败的测试都是针对UI的,大概没有为jsdom环境导入正确的库。

tyg4sfes

tyg4sfes2#

您正在使用Vue版本2和@vue/test-utils 2.0.0,这是Vue版本3。
您可以找到Vue 2 here的正确@vue/test-utils版本

相关问题