ref – https://medium.com/piecesofcode/testing-javascript-code-with-jest-18a398888838
1 2 3 4 |
> mkdir jestTest > cd jestTest > npm init > npm install —-save-dev jest |
then in package.json:
make sure the “scripts” key, and its values are there.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
{ "name": "jesttest", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "jest" }, "author": "", "license": "ISC", "devDependencies": { "jest": "^24.1.0" } } |
Test Custom function
Let’s define a custom function.
concat.js
1 2 3 4 5 |
function concat(x, y) { return x + y; } module.exports = concat; |
Then we test that custom function
Make sure you name it yourclass.test.js
This lets Jest know which files to test.
So if in our case, if we’re gunna test for concat.js, we’ll name it concat.test.js
concat.test.js
1 2 3 4 5 |
//concat.test.js const concat = require(‘./concat’); test(‘join Orin and ami to be Orinami’, () => { expect(concat(‘Orin’, ‘ami’)).toBe(‘Orinami’); }); |
Open a terminal, and in your folder directory, type: npm test
You’ll then see the result of your tests.
Basically, we use expect(…).toBe(…)
to make sure the returned result from our custom function matches up with what’s in toBe(…).
.toBe() basically just checks that value is what you expect it to be.
1 2 3 4 5 6 7 8 9 10 11 |
test('join shooo ryu-ken to be shooooo ryu ken!', () => { expect(concat('Shooooo', '-Ryu-Ken')).toBe('Shooooo-Ryu-Ken'); }); test('join hadooo ken!', () => { expect(concat('hadooo', '-ken')).toBe('hadooo-ken'); }); test('what about numbers?', () => { expect(concat('3', '4')).toBe('34'); }); |
Testing custom objects
We can also declare a custom object, and then expect(…).toBe(…) an object.
1 2 3 4 5 6 7 8 9 |
const profile = { name: 'Orinami', githubUrl: 'github.com/orinami', twitterUrl: 'twitter.com/orinami' }; test('expects profile to be an object', () => { expect(typeof profile).toBe('object'); }); |
matchers.test.js
Is the object in question equals given object?
1 2 3 4 5 6 7 8 9 |
//toEqual const data = { name: 'Orinami', lastname: 'Olatunji' }; test('Object Assignment', () => { expect(data).toEqual({name: 'Orinami', lastname: 'Olatunji'}); }); |
Declare a null. Test if its a null. Test if its defined.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
test('null', () => { const n = null; expect(n).toBeNull(); expect(n).toBeDefined(); expect(n).not.toBeUndefined(); }); test('zero', () => { const zero = 0; expect(zero).not.toBeNull(); expect(zero).toBeLessThan(1); expect(zero).toBeLessThanOrEqual(0); expect(zero).toBeDefined(); expect(zero).not.toBeUndefined(); }); |
doing addition/subtraction with numerics
1 2 3 4 5 6 7 8 |
// For Numbers const sum = 5 + 6; test(' 5 + 6 ', () => { expect(sum).toBeGreaterThan(10); expect(sum).toBeGreaterThanOrEqual(11); expect(sum).toBeLessThan(12); expect(sum).toBeLessThanOrEqual(11); }); |
Check for elements in an array
1 2 3 4 5 6 7 8 9 |
const languages = [ 'JavaScript', 'PHP', 'Ruby', ]; test('languages contains JavaScript', () => { expect(languages).toContain('JavaScript'); }); |
Match a character in a string
1 2 3 |
test('g in programmer', () => { expect('Programmer').toMatch(/g/); }); |
callback
First we define the function getMessage, where we take a callback object parameter.
In the function getMessage, we pass in a string “go Javascript!”.
Then in our test, we define the callback function. In turn we pass in this callback function into the getMessage.
The way we test for the callback is to expect the value inside the callback function to be the string “go Javascript!”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// We pass done as an argument so that Jest will wait // until the done callback is called before finishing the test. function getMessage(callback) { callback('go Javascript!'); } test('data: go JavaScript!', done => { function callback(value) { console.log('callback - value received: ' + value); expect(value).toBe('go Javascript!'); done(); } getMessage(callback); }); |