Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vue.js vuejs单元测试中的服务依赖性_Vue.js_Unit Testing_Jestjs_Babel Jest - Fatal编程技术网

Vue.js vuejs单元测试中的服务依赖性

Vue.js vuejs单元测试中的服务依赖性,vue.js,unit-testing,jestjs,babel-jest,Vue.js,Unit Testing,Jestjs,Babel Jest,在我的vue 2.6.9应用程序中,我正在使用vue jest编写一个单元测试用例,我有以下依赖项的组件 import moment from "moment"; import OverviewService from "/src/services/overviewService.js"; 当我在CLI上运行warn unit:test命令时,它给出了一个错误,说明它找不到overviewService.js的模块。 任何人都可以使用vue jest帮助

在我的vue 2.6.9应用程序中,我正在使用vue jest编写一个单元测试用例,我有以下依赖项的组件

import moment from "moment";
import OverviewService from "/src/services/overviewService.js";
当我在CLI上运行
warn unit:tes
t命令时,它给出了一个错误,说明它找不到
overviewService.js的模块。

任何人都可以使用
vue jest
帮助我,我知道有jest.mock,但我不确定如何使用它并删除此依赖项相关错误

//更新:jest.config.js文件

const ignoredModules = ["@coreui/icons", "@coreui/utils"].join("|");

module.exports = {
  moduleFileExtensions: ["js", "jsx", "json", "vue"],
  transform: {
    "^.+\\.vue$": "vue-jest",
    ".+\\.(css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$":
      "jest-transform-stub",
    "^.+\\.(js|jsx)?$": "<rootDir>/node_modules/babel-jest",
  },
  transformIgnorePatterns: [`<rootDir>/node_modules/(?!${ignoredModules})`],
  moduleNameMapper: {
    "^@/(.*)$": "<rootDir>/src/$1",
  },
  snapshotSerializers: ["jest-serializer-vue"],
  testMatch: ["<rootDir>/src/test/unit/**/*.spec.js"],
  verbose: true,
  testURL: "http://localhost/",
  collectCoverage: true,
  collectCoverageFrom: ["src/**/*.{js,vue}", "!**/node_modules/**"],
  coverageReporters: ["html", "text-summary"],
};

谢谢

您使用的是
overviewService.js
的绝对路径。这应该是相对的吗?你能链接到复制品吗?
import reports from "../../views/Reports.vue";
import createWrapper from "./wrapper";

describe("reports.vue", () => {
  it("it should render correctly", () => {
    const wrapper = createWrapper(reports);
    expect(wrapper).toMatchSnapshot();
  });
});