0

我正在 nuxt 上测试 jest,这是我的第一次。但我正面临这个错误,我不知道会发生什么,我尝试在其他包中它可以工作,但不是这个。我在 nuxt 的新环境上进行了测试,它可以工作,但这个不工作。

包.json

{
    "name": "frontend",
    "version": "1.0.2",
    "private": true,
    "scripts": {
        "dev": "nuxt",
        "build": "nuxt build",
        "start": "nuxt start",
        "analyze": "nuxt build --analyze",
        "generate": "nuxt generate",
        "test": "jest --no-cache",
        "test:unit": "vue-cli-service test:unit",
        "cy:open": "cypress open --browser chromium",
        "cy:run": "cypress run",
        "test:e2e:run": "start-server-and-test start http://localhost:3000 cy:run",
        "test:e2e:dev": "start-server-and-test dev http://localhost:3000 cy:open"
    },
    "dependencies": {
        "@nuxtjs/auth-next": "5.0.0-1637745161.ea53f98",
        "@nuxtjs/axios": "^5.13.6",
        "@nuxtjs/bootstrap-vue": "^2.0.4",
        "@woocommerce/woocommerce-rest-api": "^1.0.1",
        "bootstrap": "^5.1.3",
        "core-js": "^3.8.3",
        "font-awesome": "^4.7.0",
        "jquery": "^1.12.4",
        "nuxt": "^2.14.12",
        "popper.js": "^1.16.1"
    },
    "devDependencies": {
        "@babel/core": "^7.17.2",
        "@babel/plugin-proposal-object-rest-spread": "^7.16.7",
        "@babel/preset-env": "^7.16.11",
        "@testing-library/jest-dom": "^5.16.2",
        "@vue/cli-plugin-unit-jest": "^4.5.15",
        "@vue/test-utils": "^2.0.0-rc.17",
        "@vue/vue3-jest": "^27.0.0-alpha.4",
        "axios-mock-adapter": "^1.20.0",
        "babel-core": "^7.0.0-bridge.0",
        "babel-jest": "^27.5.1",
        "cypress": "^9.4.1",
        "eslint-plugin-cypress": "^2.12.1",
        "jest": "^27.5.1",
        "jest-serializer-vue": "^2.0.2",
        "start-server-and-test": "^1.14.0"
    }
}

.babelrc

{
    "env": {
        "test": {
            "presets": [
                [
                    "@babel/preset-env",
                    {
                        "targets": {
                            "node": "current"
                        }
                    }
                ]
            ],
            "plugins": [
                "@babel/plugin-proposal-object-rest-spread"
            ]
        }
    }
}

jest.config.js

module.exports = {
    // tell Jest to handle `*.vue` files
    moduleFileExtensions: ["js", "json", "vue"],
    watchman: false,
    moduleNameMapper: {
        "^~/(.*)$": "<rootDir>/$1",
        "^~~/(.*)$": "<rootDir>/$1",
        "^@/(.*)$": "<rootDir>/$1"
    },
    transform: {
        // process js with `babel-jest`
        "^.+\\.js$": "<rootDir>/node_modules/babel-jest",
        // process `*.vue` files with `vue-jest`
        ".*\\.(vue)$": "@vue/vue3-jest"
    },
    // set jsdom
    testEnvironment: "jsdom",
    // ingore cypress tests
    testPathIgnorePatterns: ["<rootDir>/cypress/"],
    snapshotSerializers: ["<rootDir>/node_modules/jest-serializer-vue"],
    collectCoverage: true,
    collectCoverageFrom: [
        "<rootDir>/components/**/*.vue",
        "<rootDir>/pages/*.vue"
    ]
};

MyAccountLogin.spec.js

import {
    shallowMount,
    createLocalVue
} from '@vue/test-utils';
import Vuex from 'vuex';

错误

modules/my-account/__tests__/MyAccountLogin.spec.js
  ● Test suite failed to run

    Cannot find module 'vuex' from 'modules/my-account/__tests__/MyAccountLogin.spec.js'

      35 |     createLocalVue
      36 | } from '@vue/test-utils';
    > 37 | import Vuex from 'vuex';
         | ^

所以我错过了什么?

4

0 回答 0