Jest Development
Jest Development
Jest is a delightful JavaScript Testing Framework with a focus on simplicity.
Preparing
- Learning about
npmoryarn
In Normal Projects
Full reference doc links here
Install Jest
yarn add jest -dev
或
npm i jest --save-dev
Setup The Configuration File
- Create javascript file in the root dir & named
'jest.config.js' - With this JS file, export the configuration we needed
e.g.
module.exports = { watchPathIgnorePatterns: ['/node_modules/', '/dist/', '/.git/'], testMatch: ['<rootDir>/src/**/__tests__/**/*test.js'], coverageDirectory: 'build/test-coverage', coverageReporters: ['html', 'text'], collectCoverageFrom: [ 'src/main/*.js', ], rootDir: __dirname };
jest --initcommand can help to create configuration file too.
Run With ES6
- Install babel
yarn add --dev babel-jest @babel/core @babel/preset-env - Create the configuration file named
'.babelrc'in the root dir for babel{ "presets": [ [ "@babel/preset-env" ] ] }
More configuration reference doc here: Configuration Jest
Run Jest
- Run all test:
jest - Run all test while files changed
# with git jest --watchjest --watchAll - Run all test & generator the code coverage result
jest --coverage - We could add those commands to package.json
And the we could execute the command to run test"scripts": { "test": "yarn run test:all", "test:all": "jest", "test:watch": "jest --watch", "test:coverage": "jest --coverage" },yarn run test
Writing Test
Simple API
describeitortestbeforeEach&afterEachbeforeAll&afterAll- …
More reference links here
Simple Matchers
Using the chain expression
expectmethod- receive one param as the expect result
toBematches when equalstoBeNullmatches onlynulltoBeUndefinedmatches onlyundefinedtoBeDefinedis the opposite of toBeUndefinedtoBeTruthymatches anything that an if statement treats astruetoBeFalsymatches anything that an if statement treats asfalse
nottoMatchtoThrow- …
More reference links here
Simple Mocks
Using mocked function by using the api - jest.fn(func)
- Using as the callback with function type params.
- If you want to assert the callback params, you can get the callback first called params from
mockCallback.mock.calls[0][index] mockCallback.mock.results[0].valuemeans the return value of the first call to the functionmockCallback.mock.calls.lengthmeans the mock function is called twice
- If you want to assert the callback params, you can get the callback first called params from
- Mock the return value (Using the
chain expression)mockReturnValueOncemockReturnValue
More reference links here
Modules Mock
Mocking modules by using this api - jest.mock()
jest.mock(moduleName, factory, options)jest.mock('../moduleName', () => { return jest.fn(() => 42); }); // This runs the function specified as second argument to `jest.mock`. const moduleName = require('../moduleName'); moduleName(); // Will return '42';moduleNameMapperproperty injest.config.jsmodule.exports = { // more... moduleNameMapper: { '^@module(.*)$': '<rootDir>/test/mock-module/$1' } };
Jest for Lightning Web Component
Install the jest plugins
yarn add sfdx-lwc-jest -dev
或
npm i sfdx-lwc-jest --save-dev
Create the test source dir
- Using the commands - CLI Commands
sfdx force:lightning:lwc:test:create -f force-app/main/default/lwc/myButton/myButton.js - Execute the action by using VS Code.
How to Test
Testing DOM
- To testing the DOM of LWC, the first thing is to create the component while testing,
createElementmethod can help us. e.g.import {createElement} from lwc; import myComponent from 'c/myComponent' // 'myComponent' is the custom component we are testing createElement( 'c-my-component', { is: myComponent } ) - Testing initialed HTML rendered content
describe('c-jest01', () => { afterEach(() => { // The jsdom instance is shared across test cases in a single file so reset the DOM // The callback - disconnectCallback will be called while (document.body.firstChild) { document.body.removeChild(document.body.firstChild); } }); it('Test initial content', () => { // create the element const element = createElement('c-jest01', { is: Jest01 }); // while element appended, the connectedCallback method will be called document.body.appendChild(element); // all the content is on the shadowRoot NODE const root = element.shadowRoot; const nameDiv = root.querySelector('.name'); expect(nameDiv.textContent).toBe('hello world'); }); }); - Testing the component with props
it('Test props changed', async () => { const element = createElement('c-jest01', { is: Jest01 }); document.body.appendChild(element); element.content = 'Testing content'; const root = element.shadowRoot; const contentDiv = root.querySelector('.content'); // waiting the dom rendered await Promise.resolve(); expect(contentDiv.textContent).toBe('Testing content'); });
For testing the dom, being better to testing with
stateless components, thestateful componentstesting is relatively difficult
Mock Wire Service
- Get the data snapshot & save it in the target dir named as
xxx.jsonfile - Using
registerLdsTestWireAdapterset the target mockingimport { registerLdsTestWireAdapter } from '@salesforce/sfdx-lwc-jest'; import { getRecord } from 'lightning/uiRecordApi'; // ... const adapter = registerLdsTestWireAdapter(getRecord); // ... - While component initialed, call
emitmethod ofadapterto loading the mocked dataimport { registerLdsTestWireAdapter } from '@salesforce/sfdx-lwc-jest'; import { getRecord } from 'lightning/uiRecordApi'; it('testing mocked wire service', async () => { // declared the wire adapter const adapter = registerLdsTestWireAdapter(getRecord); // load the mocked data const mockGetRecord = require('./mockData/getRecord.json'); const element = createElement('c-jest01', { is: Jest01 }); document.body.appendChild(element); // emit the saved data adapter.emit(mockGetRecord); // wait the DOM rendered await Promise.resolve(); const root = element.shadowRoot; const serviceNameDiv = root.querySelector('.service-name'); expect(serviceNameDiv.textContent).toBe(mockGetRecord.fields.Name.value); });
Testing Event Listener
Method - addEventListener in element to listen, jest.fn to mock.
e.g.
it('Test event handler', () => {
const element = createElement('c-jest01', {
is: Jest01
});
// create mock function
const connectCallback = jest.fn();
// listen connect
element.addEventListener('connected', connectCallback);
document.body.appendChild(element);
// the callback executed once
expect(connectCallback.mock.calls.length).toBe(1);
});
Mock Importing Modules
- With
moduleNameMapperin configuration- Download the basic resources from lwc-recipes
- Create dir
/force-app/test& zip above in it - In the property
moduleNameMappermodule.export = { // other... moduleNameMapper: { '^@salesforce/apex$': '<rootDir>/force-app/test/jest-mocks/apex', '^@salesforce/schema$': '<rootDir>/force-app/test/jest-mocks/schema', '^lightning/navigation$': '<rootDir>/force-app/test/jest-mocks/lightning/navigation', '^lightning/platformShowToastEvent$': '<rootDir>/force-app/test/jest-mocks/lightning/platformShowToastEvent', '^lightning/uiRecordApi$': '<rootDir>/force-app/test/jest-mocks/lightning/uiRecordApi', '^lightning/messageService$': '<rootDir>/force-app/test/jest-mocks/lightning/messageService' }, }
- With
jest.mockfunction
You can write the custom roles for all above
End-to-End Test
Jest is better for unit testing, using Selenium WebDriver or other tools for End-to-End Test

文章评论 ( 0 )