npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mocha-annotation

v1.2.0

Published

Annotation for Mocha

Downloads

473

Readme

mocha-annotation

解析使用 Mocha 编写的测试用例中的注解。

1. 安装

$ npm install mocha-annotation --save

2. API

2.1 getParseResult(sourceFiles, opts)

获得注解解析结果,结果为一个对象。

  • sourceFilesstring[],要解析的测试文件的列表
  • optsObject,额外参数
    • opts.encodingstring,读取文件时需要的文件编码格式,默认为 utf8
    • opts.isInheritboolean,是否启用继承注解的方式,如果设置为 true,则会按照用例组织的方式,优先使用自己的注解,其他则继承父级的注解

例如测试用例代码:

const {expect} = require('chai');

/**
 * @author matmanjs
 * @description hello,world!
 * @other one-describe-one-it.test.js
 * @testid one-describe-one-it:describe1
 */
describe('一个 describe 和一个 it', function () {
  /**
   * @testid one-describe-one-it:describe1:it1
   */
  it('1 等于 1', function () {
    expect(1).to.equal(1);
  });
});

解析的结果为:

{
    "children": [
        {
            "fullFile": "/Users/helinjiang/gitprojects/mocha-annotation/test/data/mocha-examples/one-describe-one-it.test.js",
            "uuid": "267d1bc4-3c65-4710-ba87-7d471845c927",
            "children": [
                {
                    "comment": {
                        "author": "matmanjs",
                        "description": "hello,world!",
                        "other": "one-describe-one-it.test.js",
                        "testid": "one-describe-one-it:describe1"
                    },
                    "nodeInfo": {
                        "describe": "一个 describe 和一个 it",
                        "callee": "describe",
                        "prelayer": "267d1bc4-3c65-4710-ba87-7d471845c927"
                    },
                    "uuid": "42f9e2e4-524d-4b79-b8cb-f752dddab5c7",
                    "children": [
                        {
                            "comment": {
                                "author": "matmanjs",
                                "description": "hello,world!",
                                "other": "one-describe-one-it.test.js",
                                "testid": "one-describe-one-it:describe1:it1"
                            },
                            "nodeInfo": {
                                "describe": "1 等于 1",
                                "callee": "it",
                                "prelayer": "42f9e2e4-524d-4b79-b8cb-f752dddab5c7"
                            },
                            "uuid": "232e201b-eec3-4967-bd9a-3e710f31b75b",
                            "children": [],
                            "fullFile": "/Users/helinjiang/gitprojects/mocha-annotation/test/data/mocha-examples/one-describe-one-it.test.js",
                            "parentId": "42f9e2e4-524d-4b79-b8cb-f752dddab5c7"
                        }
                    ],
                    "fullFile": "/Users/helinjiang/gitprojects/mocha-annotation/test/data/mocha-examples/one-describe-one-it.test.js",
                    "parentId": "267d1bc4-3c65-4710-ba87-7d471845c927"
                }
            ],
            "comment": {}
        }
    ]
}

2.2 getTestResultMap(mochaTestTreeNode, opts)

获得测试结果,一个扁平化的数据字典,key 为测试用例的描述,也即 fullTitle 。可以借助 mochawesome 生成的 mochawesome.json,获得最终测试结果的结果。

  • mochaTestTreeNodeMochaTestTreeNode,要解析的测试文件的列表
  • optsObject,额外参数
    • opts.fullTitleSepString,组合 fullTitle 是需要的间隔符号,默认情况下该值会由用例数中的描述组合而成,组合间隔符默认为空格
    • opts.mochawesomeJsonFileStringmochawesome 生成的 mochawesome.json 路径

fullTitle 参考 mochawesome.json 中的定义方式,即从 suit 到 test 的 title 值拼接起来,中间用 fullTitleSep 分割

{
    "/Users/helinjiang/gitprojects/mocha-annotation/test/data/mocha-examples/one-describe-one-it.test.js 一个 describe 和一个 it 1 等于 1": {
        "comment": {
            "author": "matmanjs",
            "description": "hello,world!",
            "other": "one-describe-one-it.test.js",
            "testid": "one-describe-one-it:describe1:it1"
        },
        "nodeInfo": {
            "describe": "1 等于 1",
            "callee": "it",
            "prelayer": "42f9e2e4-524d-4b79-b8cb-f752dddab5c7"
        },
        "uuid": "232e201b-eec3-4967-bd9a-3e710f31b75b",
        "children": [],
        "fullFile": "/Users/helinjiang/gitprojects/mocha-annotation/test/data/mocha-examples/one-describe-one-it.test.js",
        "parentId": "42f9e2e4-524d-4b79-b8cb-f752dddab5c7",
        "fullTitle": "/Users/helinjiang/gitprojects/mocha-annotation/test/data/mocha-examples/one-describe-one-it.test.js 一个 describe 和一个 it 1 等于 1",
        "result": {
            "title": "1 等于 1",
            "fullTitle": "一个 describe 和一个 it 1 等于 1",
            "timedOut": false,
            "duration": 0,
            "state": "passed",
            "speed": "fast",
            "pass": true,
            "fail": false,
            "pending": false,
            "context": null,
            "code": "expect(1).to.equal(1);",
            "err": {},
            "uuid": "43f91ee7-b9ac-4cbc-bd35-8d38b0fef5fb",
            "parentUUID": "4a52b6a5-5b59-4bf9-83ce-187e5d099b82",
            "isHook": false,
            "skipped": false
        }
    }
}