繁体   English   中英

在观察模式下进行任何摩卡测试之前,如何运行全局设置脚本

[英]How can I run a global setup script before any mocha test in watch mode

我想以TDD方式(--watch模式)运行摩卡测试,效果很好。 但是我有一个“ global setup.js”文件,该文件模拟了应用程序的一部分,大多数测试都使用该文件。

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

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

即使在带有摩卡的观看模式下,如何每次(每次整体测试运行一次)执行设置脚本?

这是我使用的命令:

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文件设置摩卡配置?


    'use strict';

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM