When developing high-quality custom components, unit testing is an inevitable topic. Comprehensive test cases guarantee the usability of custom components, and test code coverage is also an essential part of the process. Mini Program Starting from Base Library version 2.2.1, WeChat embraced open source by supporting the installation of custom components via npm. Consequently, unit testing for custom components must also be supported.
The following section introduces how to perform unit testing for custom components.
Testing Framework
Any popular testing framework available on the market can be used, provided it supports both the Node.js and DOM environments. This is because we rely on certain Node.js libraries to enhance the testing environment. Additionally, a DOM environment is essential, as we need to build a complete DOM tree structure to better simulate the runtime behavior of custom components. For example, you can choose a combination of Mocha + jsdom or use Jest. The following example uses Jest as the testing framework for illustration.
Custom Component Testing Toolkit
The runtime environment of Mini Program is quite distinct from common browser environments, as it adopts a dual-thread architecture. When conducting unit tests, however, the benefits of such a complex architecture are not necessary. Since we are performing functional tests rather than demanding strict performance or security, we provide a testing toolkit that enables custom components to run in a Node.js single-threaded environment.
First, let’s install the testing toolkit—miniprogram-simulate:
npm i --save-dev miniprogram-simulate
Writing Test Cases
Suppose we have the following custom component:
<!-- /components/index.wxml -->
<view class="index">{{prop}}</view>
// /components/index.js
export default Component({
properties: {
prop: {
type: String,
value: 'index.properties'
},
},
})
/* /components/index.wxss */
.index {
color: green;
}
To test the rendering result, you can write the test case as follows:
// /test/components/index.test.js
const simulate = require('miniprogram-simulate')
test('components/index', () => {
const id = simulate.load('/components/index') // An absolute path must be passed here
const comp = simulate.render(id) // Renders into a custom component tree instance
const parent = document.createElement('parent-wrapper') // Creates a parent node
comp.attach(parent) // Attaches to the parent node, triggering the attached hook of the custom component
const view = comp.querySelector('.index') // Retrieves the child view component
expect(view.dom.innerHTML).toBe('index.properties') // Tests the rendering result
expect(window.getComputedStyle(view.dom).color).toBe('green') // Tests the rendering result
})
Preview the effect in DevTools
Note: The
wxobject and built-in components in the testing toolkit do not implement actual functionality. If you need to test specific scenarios, you can override the API interfaces and built-in components in the testing toolkit yourself.Note: Currently, some custom component features are not yet supported (such as abstract nodes), so the testing toolkit cannot yet fully cover all characteristics of custom components. Continuous improvements will be made.
The testing toolkit provides several interfaces to facilitate testing, such as:
- Simulating touch events and triggering custom events
- Selecting child nodes
- Updating custom component data
- Triggering lifecycle hooks
- ...
For more detailed usage, please refer to the documentation on the GitHub repository.