简体   繁体   English

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

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

I want to run mocha tests in a TDD manner (--watch mode), which works fine. 我想以TDD方式(--watch模式)运行摩卡测试,效果很好。 But I have a "global setup.js" file, which mocks part of the application, that is used by most tests. 但是我有一个“ global setup.js”文件,该文件模拟了应用程序的一部分,大多数测试都使用该文件。

If I run the tests normally or in watch mode for the first time everything is fine because the setup script loads. 如果我是正常运行或第一次以监视模式运行测试,那么一切都很好,因为安装脚本已加载。

If a test or source file is changed, however, only the relevant tests run (sounds awesome in theory) but since my global mocking script is not run the tests fail. 但是,如果更改测试或源文件,则仅运行相关的测试(理论上听起来很棒),但是由于我的全局模拟脚本未运行,因此测试失败。

How can I execute a setup script each time (once per overall test run) even in watch mode with mocha? 即使在带有摩卡的观看模式下,如何每次(每次整体测试运行一次)执行设置脚本?

This is the command I use: 这是我使用的命令:

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

I have tried using the --require and --file option, but they are also not rerun on file changes. 我曾尝试使用--require和--file选项,但它们也不会在文件更改时重新运行。

I am using a vue app created with the VUE CLI and this is how my code looks 我正在使用通过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;
  });
});

This isn't a very satisfying answer because it feels like there should be a better way but you can import your setup script into the individual test files. 这不是一个令人满意的答案,因为感觉应该有更好的方法,但是您可以将设置脚本导入到各个测试文件中。

For example: 例如:

// 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;
  });
});

Feels sub optimal, but it is better than replicating logic everywhere. 感觉次优,但是它比到处复制逻辑更好。

Have you tried utilizing .mocharc.js file to setup your mocha configurations before you run a test? 在运行测试之前,您是否尝试过使用.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