admin管理员组文章数量:1357595
I would like to setup some variables first, before executing the test, and I found this solution, Running Mocha setup before each suite rather than before each test
But, I dont know how can I pass the variable into my callback, they way I did I will get undefined
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);
})
}
The reason why I want to pass the variable into the callback because, I will have many private variables that requires to setup at beforeEach
hook, and I dont want to repeat my code for all the tests.
I would like to setup some variables first, before executing the test, and I found this solution, Running Mocha setup before each suite rather than before each test
But, I dont know how can I pass the variable into my callback, they way I did I will get undefined
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);
})
}
The reason why I want to pass the variable into the callback because, I will have many private variables that requires to setup at beforeEach
hook, and I dont want to repeat my code for all the tests.
3 Answers
Reset to default 8The callbacks passed to describe
are called immediately but your beforeEach
hook is called later when the test executes. So when cb(age)
is called, age
has for value undefined
. age
is later set to "123"
but cb
has already gotten its copy of the value earlier so it has no effect. In order for cb
to see the change you'd have to pass a reference to an object that you then mutate. Something like this:
makeSuite('world', (context) => {
it('should return', () => {
console.log(context.age)
})
})
function makeSuite(name, cb) {
describe(name, () => {
let context = {
age: undefined,
};
beforeEach(() => {
context.age = '123';
});
cb(context);
});
}
(I've removed the TypeScript type annotations so that it runs as pure JavaScript. The annotations are not crucial to solving the problem anyway.)
There is another way, which is to inject the variable into mocha context
makeSuite('world', function() {
it('should return', function() {
// read the variable as shown below
console.log((this as any).age); // 123
})
})
function makeSuite(name: string, cb: () => any) {
describe(name, function() {
beforeEach(function() {
// inject the desired variable into the mocha context
(this as any).age = 123;
})
cb();
})
}
But, I dont think is a good practise of injecting the variable into the mocha context, the answer provided by @Louis would be better solution
I started with what @Louis was doing but then found it was better (in my situation) to make the function around it. That way, I could toggle the after and before actions. I also needed the before, after, and it to be asynchronous.
function itWithSetup(name, callback, doBefore = true, doAfter = true) {
let context = null;
if (doBefore) {
before(async () => {
context = await getContext();
});
}
it(name, async () => {
await callback(context);
});
if (doAfter) {
after(async () => {
await clearContext();
});
}
}
describe("test with context", () => {
itWithSetup("unit test", async (message) => {
await testWithContext(context, otherConditions);
}, true, true);
});
本文标签: javascriptSetup variable in mocha suite before each testStack Overflow
版权声明:本文标题:javascript - Setup variable in mocha suite before each test? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744069919a2585696.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论