Me: So Guys! Today we will learn JEST. Excited? ๐
You: Yes!๐
Me: Let's start, then...๐คญ
WHAT IS JEST? ๐ง
Jest is a delightful JavaScript Testing Framework with a focus on simplicity. It works with projects using: Babel, TypeScript, Node, React, Angular, Vue and more!
ADVANTAGES OF JEST ๐
- Fast and safe
- Code Coverage
- Easy Mocking
- Great Exceptions (if tests fail, it provides rich context why)
- Zero Configuration
INSTALLATION ๐
If you use yarn...
yarn add -D jest
npm fan...
npm install -D jest
Before examples, we will understand some basics global methods or objects of jest.
GLOBALS ๐ค
1. describe(name, fn)
This global method of jest creates a block that group together various tests!
For Example,
describe('arithmetic', () => {
test('sum', () => {
expect(2+2).toBe(4);
});
test('substract', () => {
expect(5-2).toBe(3);
});
} )
Note: describe()
is not compulsory, but helps to organise the group of tests together.
Note: there is describe.only()
if you want to run only one describe block
2. test(name, fn, timeout)
This jest method takes 3 arguments i.e The first argument is the test name; the second argument is a function that contains the expectations to test. The third argument (optional) is a timeout (in milliseconds).
Note: The default timeout is 5 seconds.
test(' Harry can talk to snakes ? ', () => {
expect( canHarryTalkToSnakes() ).toBeTruthy()
} )
Please, assume that canHarryTalkToSnakes()
is a function defined in some other file that returns true!
Note: If a promise is returned from test()
, Jest will wait for the promise to resolve before letting the test complete.
Note: test.only(name, fn, timeout)
this will run only a subset of tests which have .only()
Expect ๐ฌ
Expect gives us various matchers
to test values against certain conditions!
You: Payal, What are matchers?
Me:
1. expect(value)
Expect, is used every time you need to test a value. And expect
expects ๐ a "matcher" function with itself always to assert something about a value. ๐
For Example,
test('Help will always be given at Hogwarts to those who ask for it', () => {
expect( voldemortHasHorcruxes() ).toBe(7)
} )
So here, .toBe()
is a "matcher" function
2. expect.extend(matchers)
We can use expect.extend
to write our own matcher function
checkout example here
Matchers
Some common matchers are as follows
.toBe()
- tests against equality.toEqual()
- recursively checks every field of an object or array.not().toBe()
- opposite of equality.toBeNull()
- matches only null.toBeUndefined()
- matches only undefined.toBeTruthy()
- if(true).toBeFalsy()
- if(false).toContain()
- if an array or iterable contains a particular item.toMatch()
- check strings against regular expressions.toBeGreaterThan()
- greater than value.toBeGreaterThanOrEqual()
- greater than and equal to equal
Note: same goes with toBeLessThan
and toBeLessThanOrEqual
You: ๐ฎ
CONGRATULATIONS ๐
Here we are done, I will cover asynchronous tests in my next blog!
Thank you guys for reading up till here, I hope you enjoyed and learnt something new!
See you! ๐