【发布时间】:2017-02-23 10:06:59
【问题描述】:
我想在执行测试之前先设置一些变量, 我找到了这个解决方案,Running Mocha setup before each suite rather than before each test
但是,我不知道如何将变量传递给我的回调,他们这样做我会得到未定义
makeSuite('hello', (context) => {
it('should return', () => {
assert.strictEqual(1, 1)
})
})
makeSuite('world', (context) => {
it('should return', () => {
console.log(context) // undefined
assert.strictEqual(1, 1)
})
})
function makeSuite(name: string, cb: (context: any) => any) {
let age: string;
describe(name, () => {
beforeEach(() => {
age = '123'
})
cb(age);
})
}
之所以要将变量传递给回调,是因为我将有许多私有变量需要在beforeEach 挂钩处设置,并且我不想为所有测试重复我的代码。
【问题讨论】:
标签: javascript mocha.js