我正在尝试使用sequelize编写测试,并且我想在每次测试之前截断所有表。如果可能的话,我不想在每个测试文件中编写或运行它。有谁知道有什么方法可以做到这一点吗?
看来 Jest 应该支持类似的东西。
我目前正在尝试使用 globalSetup,但那是 beforeAll。
我还有另一个问题,当我运行 globalSetup 时,我会执行如下操作:
module.exports = () => {
sequelize.db.sync({ force: true })
}
二分之一有效。所以我猜测当我需要它同步运行时它正在异步运行。
最佳答案
一种方法:
sequelize.db.sync({ force: true }).then(() => {
// write you test code here
// Or
// or call the function from here to perform your test
})
或者你也可以这样做:
// trunk.js
module.exports = () => {
sequelize.db.sync({ force: true })
}
// test.js
const trunk = require('./trunk');
module.exports = () => {
trunk.then(() => {
// write you test code here
})
}
关于node.js - 有没有办法在全局范围内编写 beforeEach ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48391903/