首页 > 解决方案 > 带有外部文件的摩卡测试 - 钩子以不同的顺序运行?

问题描述

我正在尝试组织我的 mocha 测试以通过单独的测试运行器运行。每当我运行测试时,console.log 在阻塞之前在顶层输出正确的连接,但在它阻塞的单独所需文件中输出我为 null 之后。钩子正在执行,它正确设置了连接变量,但不知何故它没有传递给所需的文件。

为什么连接没有正确设置?令人困惑的是,根据我的调试器,它块在 before 钩子之前执行,这与我看到的 console.logs 的顺序相矛盾

describe.only('test-suite', async () => {
    let connection; // undefinded at this point

    before(async () => {
        connection = await getConnection();
        console.log(connection); -> proper connection instance
    });

    after(async () => {
        await closeConnection();
    });

    require('./some/test')(
        connection
    );
});

./some/test.js

module.exports = async (
    connection,
) => {
    describe('my-method', async () => {
        it('does things', async () => {
            console.log(connection); // somehow undefined
        });
    });
};

标签: javascriptnode.jsmocha.js

解决方案


推荐阅读