javascript - 带有外部文件的摩卡测试 - 钩子以不同的顺序运行?
问题描述
我正在尝试组织我的 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
});
});
};
解决方案
推荐阅读
- c# - Automapper C# Net Core - 如何映射两个不同结构的列表来更改一个字段的值?
- html - 将鼠标悬停在另一个div中的元素上时如何在一个div中显示元素
- javascript - 访问对象内数组中的数据
- python - 改进表格中的 Pytesseract 结果(表格 OCR)
- c++ - 附加到向量时如何避免调用 memcpy()
>? - javascript - Magento 2.4 - 快速结帐 - 送货地址错误
- azure-resource-manager - 如何在 ARM 模板的参数部分引用密钥库中的机密作为默认值
- opengl - 如何使OpenGL光定向?
- php - 如何在 PHP 8.0 中使用 ssh2?
- r - 使用 R 计算字符串中的单句点和双句点