JEST-EACH: Writing maintainable tests

Anil Namde
Sep 6, 2018 · 3 min read

General guidelines to write maintainable tests

1. Understanding of code under test

function add(one, two){ return one + two; }

2. Write a test that really matters not everything

// example for addition considered tests for integers only
expect(add(2, 3)).toBe(5)
expect(add(-2, 3)).toBe(1)
expect(add(-2, 0)).toBe(1)

3. Isolation of the tests

4. When around some code keep visiting its tests

5. Avoid deep nesting

describe('scenario 1 ', ()=>{
before(()=>{
...
});
after(()=>{
...
});
describe('sub scenario', ()=>{
before(()=>{
...
});
after(()=>{
...
});
describe('deep sub scenario', ()=>{
....
});
});
});
describe('scenario 1', ()=>{
before(()=>{...})
after(()=>{...})
it('test scenario', ()=>{})
})
describe('scenario n', ()=>{
before(()=>{...})
after(()=>{...})
it('test scenario', ()=>{})
}

6. Test bucketing

component.snapshot.test.js
component.unit.test.js
component.integration.test.js

Test refactoring example


JEST-EACH to rescue

Frontend Weekly

It's really hard to keep up with all the front-end development news out there. Let us help you. We hand-pick interesting articles related to front-end development. You can also subscribe to our weekly newsletter at http://frontendweekly.co

Anil Namde

Written by

UI Developer | React & Redux enthusiast

Frontend Weekly

It's really hard to keep up with all the front-end development news out there. Let us help you. We hand-pick interesting articles related to front-end development. You can also subscribe to our weekly newsletter at http://frontendweekly.co