【发布时间】:2018-08-17 16:12:21
【问题描述】:
我正在尝试组织我的 mocha 测试以通过单独的测试运行器运行。每当我运行测试时,console.log 在顶层before 块中输出正确的连接,但在单独的所需文件it 块中输出我为空。钩子正在执行,它正确设置了connection 变量,但不知何故它没有传递给所需的文件。
为什么没有正确设置连接?令人困惑的是,根据我的调试器,it 块在 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
});
});
};
【问题讨论】: