嘲笑每个测试套件和跨测试用例一次初始化和共享对象 [英] Jest initialize and shared objects once per test suite and across test cases

查看:76
本文介绍了嘲笑每个测试套件和跨测试用例一次初始化和共享对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Jest测试套件之间使用共享资源.我在互联网上阅读后发现,可能是解决方案.但是每个测试文件都会调用setup.

I want to use shared resources between jest test suites. I read in the internet and found that this could be the solution. But the setup is invoked per each test file.

我有两个测试文件links.test.js和'subscritpions.test.js'.我通常用一个命令jest以及所有这些命令来调用它们.

I have two test files links.test.js and 'subscritpions.test.js'. I usually call them with one command jest and that all.

问题是我的自定义环境custom-environment.jssetup功能:

The problem is that the setup function of my custom environment custom-environment.js:

const NodeEnvironment = require('jest-environment-node');

const MySql = require('../../lib/databases/myslq/db');

class CustomEnvironment extends NodeEnvironment {
    constructor(config) {
        super(config)
    }

    async setup() {
        await super.setup();
        console.log(`Global Setup  !!!!!!!!!`);
        this.global.gObject = "I am global object"
        this.global.liveUsers = await new MySql("Live Users");
        this.global.stageUsers = await new MySql("Stage Users");
    }

    async teardown() {
        console.log(`Global  terdown  !!!!!!!!!`);
        await super.teardown();
        this.global.gObject = "I am destroyed";
        this.global.liveUsers.closeConnection();
        this.global.stageUsers.closeConnection();
    }

    runScript(script) {
        return super.runScript(script)
    }

 }

module.exports = CustomEnvironment;

每次测试

都会被调用两次:

is called twice for each test:

全局设置!!!!!!!!! 全局设置!!!!!!!!! 错误>>>错误:监听EADDRINUSE:地址已在使用中127.0.0.1:3306

Global Setup !!!!!!!!! Global Setup !!!!!!!!! ERROR>>> Error: listen EADDRINUSE: address already in use 127.0.0.1:3306

因此,它尝试建立到同一端口的第二个连接-而我可以简单地使用现有连接.

So it tries to establish second connection to the same port - while I could simply use the existing connection.

在我看来,它的工作方式与定义没有什么区别

The way it works seems to me makes no difference from defining

beforeAll(async () => {
});
afterAll(() => {
});

钩子.

最后,问题是:使用jest命令(从而运行所有测试套件),如何为所有测试调用一次设置函数并在它们之间共享全局对象?

So to wrap up, the question is: Using jest command (thus running all test suits), how can I invoke setup function once for all test and share global objects across them?

推荐答案

setupteardown实际上是针对每个测试套件执行的,类似于顶级beforeAllafterAll.

setup and teardown are indeed executed for each test suite, similarly to top-level beforeAll and afterAll.

测试套件在单独的进程中运行.为每个测试套件初始化测试环境,例如jsdom环境为每个套件提供了伪造的DOM实例,并且不能在它们之间交叉污染.

Test suites run in separate processes. Test environment is initialized for each test suite, e.g. jsdom environment provides fake DOM instance for each suite and cannot be cross-contaminated between them.

文档说明

注意:TestEnvironment已沙盒化.每个测试套件都会在自己的TestEnvironment中触发设置/拆卸.

Note: TestEnvironment is sandboxed. Each test suite will trigger setup/teardown in their own TestEnvironment.

该环境不适用于全局设置和拆卸.为此,应使用 globalSetupglobalTeardown .它们适用于设置和关闭服务器实例,这是文档示例所显示的内容:

The environment isn't suitable for global setup and teardown. globalSetup and globalTeardown should be used for that. They are appropriate for setting up and shutting down server instances, this is what documentation example shows:

// setup.js
module.exports = async () => {
  // ...
  // Set reference to mongod in order to close the server during teardown.
  global.__MONGOD__ = mongod;
};

// teardown.js
module.exports = async function () {
  await global.__MONGOD__.stop();
};

由于这是在父进程中发生的,因此__MONGOD__在测试套件中不可用.

Since this happens in parent process, __MONGOD__ is unavailable in test suites.

这篇关于嘲笑每个测试套件和跨测试用例一次初始化和共享对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆