Jest
Delightful javascript testing
Most Basic
sum.test.js
package.json
npm run test
Will find any tests in subdirectories
Matchers
expect(2 + 2)
- produces expectation object you call matchers on
.toBe(4)
=> Object.is.toEqual(4)
=> recursively checks fields of object or array,toBe
for numbers.toMatchObject(obj)
=> Check all fields in obj are in.not.toBe(0)
=> negate matcher.toBeNull
=> null.toBeUndefined
=> undefined.toBeDefined
=> opp oftoBeUndefined
.toBeTruthy
=>if
statement treats as true.toBeFalsy
=>if
statement treats as false.toBeGreaterThan(3)
.toBeGreaterThanOrEqual(3.5)
.toBeLessThan(5)
.toBeLessThanOrEqual(4.5)
.toBeCloseTo
=> use instead of equals for jank floating pt.toMatch(/I/)
=> String regular expressions.toContain('beer')
=> check array for in.toThrow()
=> throws errors
Blocks
toplevel before each done first and after each done last compared to blocks
Execution Order
1) Runs down page running code not in test and in describe blocks
2) runs down page again running tests in order found
Async
Can't just expect in callback because test will finish, instead add done argument and test will wait until called or timeout and fail
The ways for using async also work for setup and teardown
Callback
Promises
Just return a promise
Async/Await
Setup
CLI Options
files matching my-test
, native OS notification on completion, and config.json as config
Other
To only run one test
change test(
to test.only(
Mock
Pretty cool, allows you to specify different returns per time called, check arguments and call count
More Plugins
API Routes
Last updated