You can simply move the shared tests into a function that does the it()
calls.(您可以简单地将共享测试移动到执行it()
调用的函数中。)
class Octocat {
get length() {
return 3;
}
get canSmile() {
return true;
}
}
class GrumpyCat {
get length() {
return 1;
}
get canSmile() {
return false;
}
}
const behavesLikeAPet = (pet) => {
it('is small', () => expect(pet.length).toBeLessThan(5));
it('can smile', () => expect(pet.canSmile).toEqual(true));
};
describe('Famous animals', () => {
describe('Octocat', () => {
behavesLikeAPet(new Octocat());
});
describe('GrumpyCat', () => {
behavesLikeAPet(new GrumpyCat());
});
});
You will get detailed output for every it test:(您将对其进行的所有测试得到详细的输出:)
Famous animals
Octocat
? is small (2ms)
? can smile (1ms)
GrumpyCat
? is small
? can smile (2ms)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…