{"library":"miniprogram-simulate","title":"Miniprogram Simulate","description":"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.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install miniprogram-simulate"],"cli":null},"imports":["const simulate = require('miniprogram-simulate')","import type { Simulate } from 'miniprogram-simulate'","const simulate = require('miniprogram-simulate'); simulate.load(...); simulate.render(...);"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const simulate = require('miniprogram-simulate');\nconst path = require('path');\n\n// Assuming a basic Jest setup with JSDOM environment\n// and a component at 'src/components/my-comp/index.js'\n\ndescribe('MyComponent', () => {\n    let id;\n\n    beforeAll(() => {\n        // Load the component definition. Adjust path as needed.\n        // Ensure your test runner is configured to resolve miniprogram paths.\n        id = simulate.load(path.resolve(__dirname, './src/components/my-comp/index'));\n    });\n\n    test('should render correctly and display initial text', () => {\n        // Render the component instance\n        const comp = simulate.render(id);\n\n        // Get the shadow root of the component (if applicable, or direct element for non-shadow-dom)\n        // For miniprograms, custom components often behave like web components with internal encapsulation\n        const element = comp.querySelector('.my-text'); // Assuming an element with class 'my-text' inside the component\n        expect(element.textContent).toBe('Hello World');\n    });\n\n    test('should update text on prop change', async () => {\n        const comp = simulate.render(id, { initialText: 'Initial' });\n        let element = comp.querySelector('.my-text');\n        expect(element.textContent).toBe('Initial');\n\n        // Simulate prop update (often via setData for miniprograms)\n        await comp.setData({ initialText: 'Updated' });\n        element = comp.querySelector('.my-text'); // Re-query or expect direct update on existing reference\n        expect(element.textContent).toBe('Updated');\n    });\n});","lang":"javascript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}