【问题标题】:How to write simple Jest preset如何编写简单的 Jest 预设
【发布时间】:2020-12-11 11:50:19
【问题描述】:

如何用常用的beforeAllafterAll编写自己的Jest预设?

我很困惑,好像没有相关文档。

【问题讨论】:

标签: javascript jestjs preset


【解决方案1】:

您可以通过这种方式扩展环境:

JEST 配置:

module.exports = {
    testEnvironment: './suites/future/jest.myEnv.js',
};

jest.myEnv.js:

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

const start = async () => {
    console.log('START');
};

const stop = async () => {
    console.log('STOP');
};

class TestEnvironment extends NodeEnvironment {
    constructor(config) {
        super(config);
    }

    async setup() {
        await super.setup();
        await start();
    }

    async teardown() {
        await stop();
        await super.teardown();
    }

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

module.exports = TestEnvironment;

【讨论】:

    猜你喜欢
    • 2019-08-05
    • 1970-01-01
    • 2019-03-21
    • 1970-01-01
    • 2019-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-21
    相关资源
    最近更新 更多