6.8 Organizing Test With describe()

describe('Utils', ()=>{
it('should add two numbers', ()=> {
var res = utils.add(33, 11);
expect(res).toBe(44).toBeA('number');
// if (res != 44)
// throw new Error(`Expected 44, but got ${res}.`);
});
it('should sqare a number', ()=> {
var res = utils.square(50);
expect(res).toBe(2500).toBeA('number');
expect({name: 'Jude'}).toEqual({name: 'Jude'});
// if (res != 2500)
// throw new Error(`Expected 250, but got ${res}.`);
});
it('should verify first and last names are set', ()=> {
var obj = {
age: 19,
location: 'London'
};
var res = utils.setName(obj, 'Jude Gao');
expect(res)
.toInclude({firstName: "Jude"})
.toInclude({lastName: "Gao"})
.toBeAn("object");
})
it('should async add two numbers', (done) => {
utils.asyncAdd(4, 3, (sum) => {
expect(sum).toBe(7).toBeA('number');
done();
});
});
it('should async square a number', (done) => {
utils.asyncSquare(10, (res) => {
expect(res).toBe(100);
done();
});
});
})

Using describe to organize tests allows a group effect of the output messages:

Utils
√ should add two numbers
√ should sqare a number
√ should verify first and last names are set
√ should async add two numbers (1001ms)
√ should async square a number (1001ms)

Even further

describe('Utils', ()=>{
describe('#add', ()=>{
it('should add two numbers', ()=> {
var res = utils.add(33, 11);
expect(res).toBe(44).toBeA('number');
// if (res != 44)
// throw new Error(`Expected 44, but got ${res}.`);
});
it('should async add two numbers', (done) => {
utils.asyncAdd(4, 3, (sum) => {
expect(sum).toBe(7).toBeA('number');
done();
});
});
})
√ should return hello world response
√ should return my user objects
Utils
√ should sqare a number
√ should verify first and last names are set
√ should async square a number (1002ms)
#add
√ should add two numbers
√ should async add two numbers (1001ms)

Exercise: making this structure in testing

// Server
// GET /
// some test case
// GET /users
// some test case