gpt4 book ai didi

node.js - Mocha 中的多个 before() 或 beforEeach()

转载 作者:搜寻专家 更新时间:2023-11-01 00:12:32 32 4
gpt4 key购买 nike

我需要在 mocha 的 before() Hook 中执行多个异步调用。我需要删除一个用户,然后注册,验证电子邮件,最后登录以获取 token 来验证所有其他测试用例。这是我的代码片段:

    const userInfo = {
"password": "pass123",
"email": "test@email.com",
};

var token = '' , userId = '';

before((done) => {
// Delete the user if already exists
User.remove({
email: userInfo.email
}).then((res) => {
// console.log(res.result);
})
.end(done);
done();
});


before((done) => {
request(app)
.post('/api/users')
.send(userInfo)
.expect(200)
.expect((res) => {
})
.end(done);
});

before((done) => {
User.findOne({
email: userInfo.email
}).then((res) => {
userId = res._id;
request(app)
.post('/api/users/verify-email')
.send({'token' : userId})
.expect(200)
.expect((res) => {
})
.end(done);
})
.end(done);
done();
});

这里这些调用不是按顺序执行的。我需要在验证电子邮件之前获取 userId,但出现以下错误:

POST /api/users/verify-email 401 4.082 ms - 45
1) "before all" hook

最佳答案

首先,是的,mocha 允许多个 before hooks 并保证它们以正确的顺序被调用。为确保这一点,您可以运行此代码段。

'use strict';

const results = [];
const assert = require('assert');

before(done => {
setTimeout(() => {
console.log(`First 'before'`);
results.push(1);
done(); //Will be called last
}, 1000)
});

before(done => {
setTimeout(() => {
console.log(`Second 'before'`);
results.push(2); //Will be called second
done();
}, 300)
});

before(done => {
setTimeout(() => {
console.log(`Third 'before'`);
results.push(3); //Will be called first
done();
}, 100)
});

describe('Before hooks order', () => {
it('should before hooks sequentially', () => {
//Check if the hooks were called in the right order anyway
assert.deepEqual(results, [1, 2, 3]);
});
});
//Output is:
// First 'before'
// Second 'before'
// Third 'before'

但要做到这一点,您需要仅在所有异步操作完成后调用 done() 以让 mocha 知道 Hook 已完成并且它应该运行下一个。

还有一个规则,任何 Node.js 回调必须只调用一次。所以这里有几个修复:

before((done) => {
// Delete the user if already exists
User
.remove({
email: userInfo.email
})
.then((res) => {
// console.log(res.result);
})
.end(done);
//Do not call done here() because User.remove have only started
//We need to wait until it finishes. Done will be called in .end method
// done();
});

before((done) => {
//All good here
request(app)
.post('/api/users')
.send(userInfo)
.expect(200)
.expect((res) => {
})
.end(done);
});

before((done) => {
User.findOne({
email: userInfo.email
}).then((res) => {
userId = res._id;
//You need a return statement here so the outer promise waits
//Until the internal call finishes
return request(app)
.post('/api/users/verify-email')
.send({'token': userId})
.expect(200)
.expect((res) => {
});
//We must not call callback multiple times, so remove this.
// .end(done);
})
//Now this 'end' will happen only after both calls finish
.end(done);

//Remove this please, for the same reason as with the first 'before'
// done();
});

请检查一下。我无法运行您的代码(没有您的 API),所以请让我知道任何问题。

关于node.js - Mocha 中的多个 before() 或 beforEeach(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42500154/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com