我想設置一些變量首先,執行測試之前, ,我發現這個解決方案,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
鉤許多私有變量的原因,我不想重複我的代碼,所有的測試。
非常聰明的解決方案! ;),我確實找到了另一種方式,是注入「摩卡背景」,但我不認爲這是一個好習慣! – Tim