访问常量在之前每个在茉莉花测试中定义



对于给定的代码,如何在测试myTest中访问常量myConst

describe('HeaderCustomerDetailsComponent child components',() => {
beforeEach(() => {
...
const myConst = "Something";
});
it('myTest', () => {
expect(myConst).toBeTruthy();
});
});

因为您要在beforeEach(...)方法中定义myConst,所以它仅限于该范围。最好的方法是可能将myConst移到类级别,并使用let定义它。

试试这个:

describe('HeaderCustomerDetailsComponent child components',() => {
let myConst;
beforeEach(() => {
...
this.myConst = "Something";
});
it('myTest', () => {
expect(this.myConst).toBeTruthy();
});
});

由于您仍在beforeEach(...)方法中设置myConst,因此您可以避免测试之间的污染,但您仍然应该小心。

最新更新