@mpflow/plugin-unit-test
TypeScript icon, indicating that this package has built-in type declarations

0.0.34 • Public • Published

@mpflow/plugin-unit-test

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

说明

该插件用于给 Mpflow 的构架提供单元测试功能的集成,,使用 jest 为测试执行器,miniprogram-simulate 为自动化执行器

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

安装

mpflow add unit-test
#
mpflow add @mpflow/plugin-unit-test

使用

在自定义组件目录下新增 __test__ 文件夹,在文件夹下添加测试文件,如:

// 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()
    })
})

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

npm run test:unit
#
yarn test:unit

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

npm run test:unit -- --coverage
#
yarn test:unit --coverage

/@mpflow/plugin-unit-test/

    Package Sidebar

    Install

    npm i @mpflow/plugin-unit-test

    Weekly Downloads

    0

    Version

    0.0.34

    License

    MIT

    Unpacked Size

    10.9 kB

    Total Files

    9

    Last publish

    Collaborators

    • wechat-miniprogram