miniprogram-simulate is a testing utility designed to facilitate unit testing for WeChat miniprogram custom components. It addresses the challenges of testing in the miniprogram's unique dual-threaded runtime environment by simulating a single-threaded DOM environment. This approach allows developers to leverage standard JavaScript testing frameworks like Jest, Karma, or Mocha (with JSDOM) to test their miniprogram components. The package essentially recreates the component tree within a DOM environment for easier assertion and interaction. The current stable version is 1.6.1, and it maintains a relatively active release cadence, with minor versions typically released every 1-3 months. Its key differentiator is providing a mock DOM environment for miniprogram components, abstracting away the platform-specific runtime intricacies.
npm install miniprogram-simulateVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to load and render a miniprogram custom component using `miniprogram-simulate` within a Jest testing environment, including basic assertion and prop updates.
Always refer to the '暂不支持特性' and '细节实现' sections of the official documentation to understand current limitations and adjust test strategies accordingly. For features not supported, consider integration tests on a real device or E2E tests using official WeChat DevTools capabilities.
Ensure your test runner (e.g., Jest) is configured to use a JSDOM environment. For Jest, add `testEnvironment: 'jsdom'` to your `jest.config.js` or package.json. Alternatively, use a browser-based test runner like Karma.
Use `path.resolve()` with `__dirname` or project root to construct robust paths for `simulate.load()`. Ensure any custom path aliases or resolvers used in your miniprogram project are also configured in your test setup if `miniprogram-simulate` needs to load sub-components or dependencies via those aliases.
Configure your test runner (e.g., Jest) to use a JSDOM environment. For Jest, add `testEnvironment: 'jsdom'` to your `jest.config.js` or package.json.
Verify the path to your miniprogram component. Use `path.resolve(__dirname, 'relative/path/to/component')` to ensure an absolute path. Check if your test runner's module name mapper or resolver configuration needs adjustment for miniprogram-specific path aliases.
Ensure you are using `const simulate = require('miniprogram-simulate')` for CommonJS. If using ESM, try `import * as simulate from 'miniprogram-simulate'`. Do not try to destructure `load` or `render` directly from the package as they are methods on the `simulate` object.