Alohomora, JEST! ๐Ÿ”‘

Here, opens the learning door of Jest!

ยท

3 min read

Alohomora, JEST! ๐Ÿ”‘

Me: So Guys! Today we will learn JEST. Excited? ๐Ÿ˜‰
You: Yes!๐Ÿ˜…
exicted Me: Let's start, then...๐Ÿคญ

WHAT IS JEST? ๐Ÿง

jest-logo

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 ๐Ÿ˜Ž

  1. Fast and safe
  2. Code Coverage
  3. Easy Mocking
  4. Great Exceptions (if tests fail, it provides rich context why)
  5. 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: hold-on

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

  1. .toBe() - tests against equality
  2. .toEqual() - recursively checks every field of an object or array
  3. .not().toBe() - opposite of equality
  4. .toBeNull() - matches only null
  5. .toBeUndefined() - matches only undefined
  6. .toBeTruthy() - if(true)
  7. .toBeFalsy() - if(false)
  8. .toContain() - if an array or iterable contains a particular item
  9. .toMatch() - check strings against regular expressions
  10. .toBeGreaterThan() - greater than value
  11. .toBeGreaterThanOrEqual() - greater than and equal to equal

Note: same goes with toBeLessThan and toBeLessThanOrEqual

You: ๐Ÿ˜ฎ wow

CONGRATULATIONS ๐Ÿ˜

thanks 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! ๐Ÿ‘‹ bye

ย