如何在手表模式下进行任何摩卡测试之前运行全局安装脚本



我想以TDD方式(--watch模式(运行摩卡测试,这工作正常。但是我有一个"全局设置.js"文件,它模拟了应用程序的一部分,大多数测试都使用它。

如果我第一次正常运行测试或以监视模式运行测试,一切都很好,因为安装脚本加载了。

但是,如果更改了测试或源文件,则仅运行相关的测试(理论上听起来很棒(,但由于我的全局模拟脚本未运行,因此测试失败。

即使在使用 mocha 的手表模式下,我如何每次(每次整体测试运行一次(执行设置脚本?

这是我使用的命令:

vue-cli-service test:unit --watch
# pure mocha would be (I assume)
mocha 'tests/**/*.spec.js' --watch

我尝试使用 --require 和 --file 选项,但它们也不会在文件更改时重新运行。

我正在使用使用 VUE CLI 创建的 vue 应用程序,这就是我的代码的外观

// setup.spec.js
import { config } from "@vue/test-utils";
before(() => {
  config.mocks["$t"] = () => {};
});
// some_test.spec.js
import { expect } from "chai";
import { shallowMount } from "@vue/test-utils";
import MyComp from "@/components/MyComp.vue";
describe("MyComp", () => {
  it("renders sth", () => {
    const wrapper = shallowMount(MyComp);
    expect(wrapper.find(".sth").exists()).to.be.true;
  });
});

这不是一个非常令人满意的答案,因为感觉应该有更好的方法,但您可以将安装脚本导入到各个测试文件中。

例如:

// some_test.spec.js
import 'setup.spec.js' //<-- this guy right here
import { expect } from "chai";
import { shallowMount } from "@vue/test-utils";
import MyComp from "@/components/MyComp.vue";
describe("MyComp", () => {
  it("renders sth", () => {
    const wrapper = shallowMount(MyComp);
    expect(wrapper.find(".sth").exists()).to.be.true;
  });
});

感觉次优,但比到处复制逻辑要好。

您是否尝试过在运行测试之前使用 .mocharc.js 文件来设置 mocha 配置?


    'use strict';
    module.exports = {
        package: './package.json',
        watch: true,
        timeout: 100000
};

最新更新