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

@jimi1126/unitest

v1.0.0

Published

generate unit test

Downloads

1

Readme

Unitest

一个 cli 工具,通过注解生成单元测试,让单元测试变得简单。

安装

npm i unitest -g

快速开始

在需要生成单元测试的文件里,为函数添加单元测试注解,然后告诉 unitest 文件在哪里并运行,当运行完毕在项目的根目录下就多了一个 test 目录,里面就是生成的单元测试了。

  1. 示例:demo.js
/**
 * 返回两数相加结果
 * @param {Number} a
 * @param {Number} b
 * @return {Number}
 * @test 1|2 3
 */
exports.add = function (a, b) {
  return a + b;
};

假设目录结构是这样的 1712396079101.png

unitest -f src/demo.js

1712398185730.png 可以看到,项目添加了一个 test 目录,并且生成两个测试文件,为什么是两个?考虑到会有自定义单元测试的情况,避免覆盖,我们把自定义的测试代码放在.test.js 文件里,.test.js 文件只会生成一次,而.unitest.js 总是会重新生成。

现在运行测试工具,就可以看到函数完成了单元测试。

image.png

说明

注释

unitest 会检查所有添加@test 注释的函数

// 1、只添加@test,会生成单元测试,单元测试代码无参数传入
/**
 * @test
 */
exports.add = function (a, b) {
  return a + b;
};
// 2、添加@test与@param,会生成单元测试,并且单元测试代码根据@param类型mock参数传入
/**
 * 返回两数相加结果
 * @param {Number} a
 * @param {Number} b
 * @test
 */
exports.add = function (a, b) {
  return a + b;
};
// 3、添加@test并描述输入(以|隔开)与输出(与输入以空格隔开),会生成单元测试,单元测试代码会根据描述生成输入输出
// 注意:这样注释都将被解析成String
/**
 * 返回两数相加结果
 * @test 1|2 3
 */
exports.add = function (a, b) {
  return a + b;
};
// 4、为了支持输入输出类型,需要添加@param与@return来定义输入与输出类型
/**
 * 返回两数相加结果
 * @param {Number} a
 * @param {Number} b
 * @return {Number}
 * @test 1|2 3
 */
exports.add = function (a, b) {
  return a + b;
};

options

支持指定目标目录与指定生成目录,当前只支持.js 后缀

Options:
  -v, --version          当前版本
  -f, --from <dir|file>  指定目标目录或文件 (default: "src")
  -t, --to <dir>         指定生成目录 (default: "test")
  -e, --ext <suffix>     指定目标文件后缀 (default: ".js")
  -c, --config <file>    指定配置文件
  -h, --help             display help for command