Jest - 在一个描述块中导入多个测试,重用 beforeEach() 中定义的变量



我熟悉RSpec,通过编写共享示例很容易重用测试用例

shared_example_for 'a cute pet' do 
it 'tests that the pet is a small' { expect(pet.size).to be_lesser_than(10) }
it 'tests that the pet can smile' { expect(pet.can_smile?).to be }
end
describe 'The Octocat' do
let(:pet) { Octocat.new }
it_behaves_like 'a cute pet'
end
...
describe 'The Doge' do 
let(:pet) { Doge.new }
it_behaves_like 'a cute pet'
end

开玩笑中有等价物吗?可以让我重用在 Each(( 块之前设置的变量的东西?我正在尝试使用以下方法找到一种方法:

# __tests__/cuteness.js
export const cutenessTests = function() {
test('it is small', () => {
expect(petSetInBefore.length).toBeLesserThan(5)
})
test('it can smile', () => {
expect(petSetInBefore.canSmile).toBe(true)
})
}
# __tests__/famous_animals.test.js
import { cutenessTests } from './cuteness'
describe('Famous animals', () => {
let petSetInBefore;
describe('Octocat', () => {
beforeEach(() => {
petSetInBefore = new Octocat();
})
cutenessTests.bind(this)()
})
})

这里重要的是,我正在尝试共享多个test定义,而不仅仅是一个,否则我本可以将petSetInBefore传递给共享函数。

编辑:我的每个测试和嵌套描述都可能会改变我的测试环境和对象,因此 beforeEach 用于恢复适当的测试环境。这是一个更好的例子

class Octocat {
get strokeFor(time) {
this.strokeTime = this.strokeTime + time
if (this.strokeTime <= 10) {
this.mood = 'happy'
} else {
this.mood = 'bored'
}
}
}
class Doge {
get strokeFor(time) {
this.strokeTime = this.strokeTime + time
if (this.strokeTime <= 5) {
this.mood = 'happy'
} else {
this.mood = 'bored'
}
}
}
const cutenessTests = function() {
describe('when stroked for a short while', () => {
beforeEach(() => {
petSetInBefore.strokeFor(1);
})
test('it is happy', () => { expect(petSetInBefore.mood).to(eq('happy')) }
describe('when stroked too much', () => {
beforeEach(() => {
petSetInBefore.stroke(1000);
})
test('it gets bored', () => { expect(petSetInBefore.mood).to(eq('bored')) }
})
describe('when stroked a little longer', () => {
beforeEach(() => {
petSetInBefore.strokeFor(4);
})
test('it is still happy', () => { expect(petSetInBefore.mood).to(eq('happy')) }
})
})
}

编辑2:这是基于Gui3答案的 repl.it

EDIT3:对象可以在可重用测试之前或期间更改

describe('Famous animals', () => {
let petSetInBefore;
describe('Octocat', () => {
beforeEach(() => {
petSetInBefore = new Octocat();
})
describe('when it is not well rested', () => { 
beforeEach(() => { petSetInBefore.wellRested() } // Extra object preparation / context before calling reusable examples
cutenessTests()
}),
describe('when it is not well rested', () => { 
// Calling reusable examples without extra context
cutenessTests()
})
})
})

Jest 有describe.each(table)我没有看到被大量使用,但它对于重用具有共同/相同结果的测试非常有帮助。

如果对两个测试对象的期望相同,您可以这样做:

const aCutePet = pet => {
it("should be small", () => {
expect(pet.size).toBeLessThan(10);
});
it(`should be able to smile`, () => {
expect(pet).toHaveProperty('can_smile', true)
});
}
describe.each([
[new Doge],
[new Octocat]
])("The %O", aCutePet);

输出:

The Doge { size: 3, can_smile: true }
✓ should be small (1ms)
✓ should be able to smile (1ms)
The Octocat { size: 5, can_smile: true }
✓ should be small
✓ should be able to smile (1ms)

如果你仍然想要beforeEach

出于原因...如果您在全局范围内声明变量,它有效

let petSetInBefore; // here it works
describe('Famous animals', () => {
//let petSetInBefore; // here it's undefined
describe('Octocat', ()  => {
//let petSetInBefore; // undefined too
beforeAll(() => {
petSetInBefore = new Octocat();
})
cutenessTests() // .bind(this) results the same
});
describe('Doge', () => {
beforeEach(() => {
petSetInBefore = new Doge();
})
cutenessTests.bind(this)()
});
})

https://repl.it/@gui3/jestSharedTests

似乎共享函数内的测试无法共享以前的变量每个否则......

您只需将共享测试移动到执行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());
});
});

您将获得每个测试的详细输出:

Famous animals
Octocat
✓ is small (2ms)
✓ can smile (1ms)
GrumpyCat
✓ is small
✕ can smile (2ms)

相关内容

  • 没有找到相关文章

最新更新