# @mpflow/plugin-unit-test

> > TODO: description

Latest version **2.0.2** (published 2026-09-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install @mpflow/plugin-unit-test
pnpm add @mpflow/plugin-unit-test
yarn add @mpflow/plugin-unit-test
bun add @mpflow/plugin-unit-test
```

## Health

**Score 65/100 (B)** — status: active.

Positive: has types; no vulnerabilities; has provenance; recently updated; high maintenance score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 2.0.2 |
| Published | 2026-09-17 |
| First published | 2020-08-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 66 |
| Author | chyizheng |
| Maintainers | wechat-miniprogram |

## Links

- npm: https://www.npmjs.com/package/@mpflow/plugin-unit-test
- Repository: https://github.com/wechat-miniprogram/mpflow
- Homepage: https://github.com/wechat-miniprogram/mpflow#readme
- Issues: https://github.com/wechat-miniprogram/mpflow/issues
- npm.io page: https://npm.io/package/@mpflow/plugin-unit-test

## Dependencies (1)

- [jest](https://npm.io/package/jest.md) ^29.7.0

## Recent versions

- 2.0.2 (latest) — 2026-09-17
- 1.0.1 — 2026-04-20
- 1.0.0-alpha.2 — 2026-02-26
- 1.0.0-alpha.1 — 2026-02-13
- 1.0.0-alpha — 2026-02-11
- 0.0.34 — 2023-05-25
- 0.0.33 — 2022-02-10
- 0.0.32 — 2021-10-13
- 0.0.31 — 2021-10-13
- 0.0.30 — 2021-10-11
- 0.0.29 — 2021-09-24
- 0.0.28 — 2021-09-23
- 0.0.27 — 2021-07-13
- 0.0.26 — 2021-06-22
- 0.0.25 — 2021-06-22
- … 23 more at https://npm.io/package/@mpflow/plugin-unit-test/versions

## README

# `@mpflow/plugin-unit-test`

> Mpflow 的小程序单元测试支持插件

## 说明

该插件用于给 Mpflow 的构架提供单元测试功能的集成，，使用 [jest](https://jestjs.io/) 为测试执行器，[miniprogram-simulate](https://github.com/wechat-miniprogram/miniprogram-simulate) 为自动化执行器

`miniprogram-simulate` 是通过模拟小程序的运行环境，让小程序的自定义组件能够在 DOM 环境下进行测试。因此适用于对单一组件进行单元测试，而不适用与对整个小程序进行集成测试

## 安装

```bash
mpflow add unit-test
# 或
mpflow add @mpflow/plugin-unit-test
```

## 使用

在自定义组件目录下新增 `__test__` 文件夹，在文件夹下添加测试文件，如：

```js
// comp.test.js

import path from 'path'
import simulate from 'miniprogram-simulate'

describe('comp', () => {
    let id

    beforeAll(() => {
        id = simulate.load(path.resolve(__dirname, '../comp'), { less: true })
    })

    test('should match snapshot', () => {
        const comp = simulate.render(id, {})
        comp.attach(document.createElement('parent-wrapper'))

        expect(comp.toJSON()).toMatchSnapshot()
    })
})
```

然后执行命令启动单元测试

```bash
npm run test:unit
# 或
yarn test:unit
```

也可以通过 `--coverage` 参数来获取代码覆盖率报告

```bash
npm run test:unit -- --coverage
# 或
yarn test:unit --coverage
```

---
_Source: https://npm.io/package/@mpflow/plugin-unit-test · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
