Introduction to Jest testing framework
Painless JavaScript Testing platform
Introduction to Jest testing framework Painless JavaScript Testing - - PowerPoint PPT Presentation
Introduction to Jest testing framework Painless JavaScript Testing platform Vasyl Boroviak Transform your inspection process with smart, mobile forms. checklists into digital forms and access them on site, in the field, where Typical set of
Painless JavaScript Testing platform
Transform your inspection process with smart, mobile forms. checklists into digital forms and access them on site, in the field, where
reload, JS file mocks)
src/ converters/ api-to-db.js db-to-api.js server.js routes.js model.js db.js node_modules/ test/ converters/ api-to-db.js db-to-api.js server.js routes.js model.js db.js package.json
I usually copy the src/ directory structure inside the test/ directory.
Do you?
I find it hard to maintain and navigate especially in large projects (say, more than 20 source files).
Do you?
src/ converters/ __tests__/ api-to-db.js db-to-api.js api-to-db.js db-to-api.js __tests__/ server.js routes.js model.js db.js server.js routes.js model.js db.js node_modules/ package.json
Facebook promote the idea that tests should be kept closer to the code it’s testing.
By default Jest searches all the directories named __tests__ and runs all JS files inside.
Jest will search all the file/module mocks in __mocks__. Mocked:
src/ __mocks__/ db.js __tests__/ server.js routes.js model.js server.js routes.js model.js db.js __mocks__/ express.js fs.js node_modules/ express/ package.json
execute in your project.
mockFn = jest.fn(?implementation) Returns a new, unused mock/spy function. Optionally takes a mock implementation. API:
Setup steps:
Done! Jest it will look for babel-jest module and automatically compile JavaScript code using Babel.
results.
jest.mock('../moduleName', () => { return jest.fn(() => 42); }); const moduleName = require('../moduleName'); moduleName(); // Will return '42'; const React1 = require('react'); jest.resetModules(); const React2 = require('react'); React1 !== React2; // These two are separate copies of React
Error messages are helpful, visual and color
to the source of problems quickly.
it('calls the callback after 1 second', () => { const timerGame = require('../timerGame'); const callback = jest.fn(); timerGame(callback); // At this point in time, the callback should not have been called yet expect(callback).not.toBeCalled(); // Fast-forward until all timers have been executed jest.runAllTimers(); // Now our callback should have been called! expect(callback).toBeCalled(); expect(callback.mock.calls.length).toBe(1); });
Default: “jsdom” Also possible: “node”
it(‘converts DB to API', () => { const data = convertFromDbToApi(someValue); const serializedData = JSON.stringify(data); expect(serializedData).toMatchSnapshot(); }); Run jest -u to update the __snapshot__ files. “One of the most challenging aspects of my project was keeping the input and output files for each test case in sync. Each little change in functionality could cause all the output files to change. With snapshot testing I do not need the output files, the snapshots are generated for free by Jest! I can simply inspect how Jest updates the snapshots rather than making the changes manually.” – Kyle Davis working on fjs.
for every test
async/await
file and line you did it form
comes pre-configured in create-react-app and react- native projects.
Built-ins:
Built-ins:
Automatically convert Tape, AVA, Mocha tests to Jest. Demo!