powerfull testing for javascript framework
===
- make new folder to store our project, give folder name JestJS
- open terminal on that directory
- then do npm init -y to initialize node.js project
- then we can open the code in vscode by type code .
- to install jestjs we can use: npm install --save-dev jest
- so we successfully install jest on our node.js, we can see on package.json
- next we will configure npm script
- on package.json, on scripts there is test
===
- unit test is process of checking small piece of code to deliver information early
- we need unit test to easly identify error or bug, instead of check the whole code
- now we gonna make a unit test with jestjs
- we start by make new file name sum.js, we gonna type our code in this file
- then also create new file name sum.test.js. this file is the test file
- back to sum.js. we make new function name sum that take 2 parameter a and b. and return a + b. then we export the sum function with module like:
- then we need to use sum funcion on sum.test.js
- once we make name.test.js, javascript automaticly know that this file is test file
- now open sum.test.js, we need import the sum function from sum.js with:
- now we can run the test, with test() method, the first parameter is description and followed by testFunction as callback
- we gonna make desription that adds 1 + 2 to equal 3
- with callback second parameter, that use expect(function).toBe(expect output) like:
- then open package.json, change jest to test, and the value to jest like:
- now run the test, we can open terminal run npm test
- then we can see information about the test is pass
===
- use to test different type of data. on last code, the matcher is toBe method
- there is various type of matcher, like toBe toEqual toTruthy toFalsy toThrow
- first we well learn about toBe
- to is use to primitive value (number, string, boolean)
- start with make test with description two plus two is four
- and with call back function with expect 2 + 2 toBe 4 like:
- so we can try npm test to run the test
- next is about toEqual, is use when comparing object or array
- we can make test with description object assignment
- with callback function that have const variable of data with key and value of one: 1
- follow with assign data on that object with key and value two and 2
- then add expect data toEqual({one: 1, two:2}), the code will be like:
- and we run npm test to run the test
- so next matcher is toTruthy and toFalsy matcher, we use
- start by create a test with description null is falsy
- with callback function that have const variable name n with value null
- the add expect n toBeFalsy()
- lets run with npm test
- because falsy not only null, but also all falsy state, so we can use it like:
- next we gonna try toBeTruthy, the value of truthy is the exception of falsy state
- we can change value of n to 1, and toBeFalsy to toBeTruthy
- and also change the description to one is truthy like:
- and run the test with npm run
- the last thing is toThrow to error handling
- we use it for detailing when the function is throw error, we expect the function throw the error when its executed. useful to validate input and throw the error
- first create new test with description throws on invalid input
- with callback function that expect with function myFunction(invalidInput) and toThrow()
- the back to sum.js, and add mynFunction with input as parameter
- and create a conditional that if typeof input is not a number we gonna throw new Error('invalid input')
- the we need to export the function like:
- now back to sum.test.js, and create const variable name myFunction with value require of the file of myFunction that sum
- so on expect callback function we change the argument to some string like:
- now try the test with npm test
===
- test asynchronus code is quite complex, not such straighforward
- asyncronus code allow to execute some code on background, not blocking the main execution
- web need asynchronus because keep ui to responsive while do request or timer
- there is some method of async such as callback, promise, and async function
- first the callback, lets start by write new function called fetchData with parameter callback
- and do setTimeout with callback function callback('peanut butter') and time 1000
- new we gonna test this function, be sure to export the function, the code be like:
- create test with description the data is peanut butter
- then for second parameter make done => {}
- done contain function callback(data)
- and do try with expect(data) toBe('peanut butter') and add done()
- on catch error, add done(error)
- then after the callback function, add fetchData(callback). the code gonna be like:
- and we can try the test with npm test
- next we gonna try promise
- first we need to create a function with promise
- create function name fetchPromise with return new Promise
- with parameter resolve and reject, then use setTimeout to resolve('peanut butter') in 1000
- now make sure to export this function
- and create test for the promise, we need to import the function with require method
- then write test with description the data is peanut butter
- with second parameter a callback that return expect the function
- follow by resolves.toBe('peanut butter')
- then we can do npm test to run the test
- now also create the test on reject
- with desctipiton the fetch fails with an error
- then callback function that return expect(fetchPromise()).rejects.toThrow('error')
- now we talk about async await function
- now to do async await test, we create new test with description the data is peanut butter
- follow by async function, that have const variable call data with value await fetchPromise()
- and expect data toBe('peanut butter')
- the async will significe the callback funtion will be an asynchronus
- the await is keyword of async, this wait the function to return the result
- now we gonna run the test with npm test
===
- mock is fake implementation of real function
- and spies are tools that use to track the behavior of those function
- such the function is called or how many it has called, and with what argument of the function
- mocking used to isolate unit of code being tested, this prevent test from being affected by external factors
- now lets try mock function in jest
- we gonna use jest.fn the way to create mock function
- first we create new const variable name mockCallback with value jest.fn
- and have argument of a function x => 42 + x
- and we can call mockCallback(0) or mockCallback(1)
- that gonna make jest.fn contain 43
- now to test the mock function with jest
- write test with descripiton mock implementation of basic function
- with function the before function of mockCallback
- now expect mock(1) toBe(43)
- now try the test with npm test
- then add expect mock toHaveBeenCalledWith(1)
- this statement check if the mock call by correct argument
- then run test too
- next is spies, we gonna make new test with desctiption spying on a method of an object
- with function that have variable called video
- video have value of object then a method play() { return true }
- then add const spy with value jest.spyOn(video, 'play')
- then add video.play()
- and expect spy. toHaveBeenCalled()
- and do sply.mockRestore()
- so we can run the test