Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
412 views
in Technique[技术] by (71.8m points)

javascript - 开玩笑-在一个describe块中导入多个测试,重用beforeEach()中定义的变量(Jest - import multiple tests in a describe block, reusing variables defined in beforeEach())

I am familiar with RSpec where it is very easy to reuse test cases by writing shared examples(我对RSpec很熟悉,在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 Is there an equivalent in Jest ?(笑话中是否有类似的东西?) Something that would let me reuse variables set in beforeEach() blocks ?(是什么让我可以重用beforeEach()块中设置的变量?) I am trying to find a way using something like the following :(我正在尝试使用以下方法找到一种方法:) # __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)() }) }) The important here is that I am trying to share multiple test definitions and not just one, otherwise I could have passed the petSetInBefore to the shared function.(这里重要的是我要共享多个test定义,而不仅仅是一个,否则我可以将petSetInBefore传递给共享函数。) EDIT : each of my tests and nested describe are likely to alter my test environment and objects, so the beforeEach is used to restore a proper test environment.(编辑:我的每个测试和嵌套描述都可能会更改我的测试环境和对象,因此,beforeEach用于还原适当的测试环境。) Here is a better example(这是一个更好的例子) 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')) } }) }) } EDIT2: Here is a repl.it based on Gui3's answer(EDIT2:这里是一个repl.it基于鬲的答案) EDIT3 : the object can be altered before or during the reusable tests(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() }) }) })   ask by Cyril Duchon-Doris translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...