3

我在我的项目 vuejs 中使用 test jest 对登录页面进行了测试:

import Vuex from "vuex";
import { shallowMount, createLocalVue } from "@vue/test-utils";
import LoginForm from "@/components/LoginForm.vue";
import store from "@/store";

const localVue = createLocalVue();
localVue.use(Vuex);

describe("LoginForm.vue", () => {
    it("input id and password exist", () => {
        const wrapper = shallowMount(LoginForm, {
            mocks: {
                $t: () => {}
            },
            store,
            localVue
        });
        //const div = wrapper.find("el-input");

        expect(wrapper.find("#inputId").exists()).toBeTruthy();
        expect(wrapper.find("#inputPassword").exists()).toBeTruthy();
    });
});

并通过run test:unit-coverage(在包 json 中"test:unit-coverage": "jest --clearCache && vue-cli-service test:unit --coverage":)

在项目中添加 ag-grid-vue 之前很好...

现在我有一个错误

 FAIL  tests/unit/specs/login.spec.js
  ● Test suite failed to run

    C:\DevSources\node_modules\ag-grid-vue\lib\AgGridVue.js:20
    import { Component, Prop, Vue } from 'vue-property-decorator';
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
      at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)
      at Object.<anonymous> (node_modules/ag-grid-vue/main.js:6:10)

我试图更改配置,但已经出现错误

jest.config.js :

module.exports = {
    moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "vue"],
    transform: {
        "^.+\\.vue$": "vue-jest",
        ".+\\.(css|styl|less|sass|scss|svg|png|jpg|ttf|woff|woff2)$":
            "jest-transform-stub",
        "^.+\\.jsx?$": "babel-jest"
    },
    moduleNameMapper: {
        "^@/(.*)$": "<rootDir>/src/$1"
    },
    snapshotSerializers: ["jest-serializer-vue"],
    testMatch: [
        "**/tests/unit/**/*.spec.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx)"
    ],
    testURL: "http://localhost/"
};

babel.config.js :

module.exports = {
    presets: ["@vue/cli-plugin-babel/preset"]
};

谢谢您的帮助。

4

1 回答 1

0

您可以尝试transformIgnorePatternsjest.config.js文件中使用

transformIgnorePatterns: [
    "node_modules/(?!(ag-grid-vue)/)"
]
于 2020-05-17T15:01:07.127 回答