# defcomment

> A simple library to translate code commenting to tests or other.

Latest version **0.4.13** (published 2017-06-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install defcomment
pnpm add defcomment
yarn add defcomment
bun add defcomment
```

Provides the command `deftest`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.13 |
| Published | 2017-06-12 |
| First published | 2016-04-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 9 |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | ddchen |
| Maintainers | ddchen |
| Keywords | auto, test, comment, doc |

## Links

- npm: https://www.npmjs.com/package/defcomment
- npm.io page: https://npm.io/package/defcomment

## Dependencies (9)

- [del](https://npm.io/package/del.md) ^2.2.2
- [glob](https://npm.io/package/glob.md) ^7.1.1
- [uuid](https://npm.io/package/uuid.md) ^3.0.1
- [yargs](https://npm.io/package/yargs.md) ^7.0.1
- [mkdirp](https://npm.io/package/mkdirp.md) ^0.5.1
- [chokidar](https://npm.io/package/chokidar.md) ^1.6.1
- [cl-jsoneq](https://npm.io/package/cl-jsoneq.md) 0.0.2
- [es6-promisify](https://npm.io/package/es6-promisify.md) ^4.0.0
- [browser-js-env](https://npm.io/package/browser-js-env.md) 0.0.9

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads
- [aws-elasticsearch-connector](https://npm.io/package/aws-elasticsearch-connector.md) — 37.4K weekly downloads

## Recent versions

- 0.4.13 (latest) — 2017-06-12
- 0.4.12 — 2017-04-17
- 0.4.11 — 2017-04-17
- 0.4.10 — 2017-04-17
- 0.4.9 — 2017-04-17
- 0.4.8 — 2017-04-17
- 0.4.7 — 2017-03-29
- 0.4.6 — 2017-03-27
- 0.4.5 — 2017-03-27
- 0.4.4 — 2017-03-21
- 0.4.3 — 2017-03-21
- 0.4.2 — 2017-03-21
- 0.4.1 — 2017-03-21
- 0.4.0 — 2017-03-21
- 0.3.16 — 2017-03-21
- … 25 more at https://npm.io/package/defcomment/versions

## README

# defcomment
 
[![Build Status](https://travis-ci.org/LoveKino/defcomment.svg?branch=master)](https://travis-ci.org/LoveKino/defcomment)
[![Coverage Status](https://coveralls.io/repos/github/LoveKino/defcomment/badge.svg)](https://coveralls.io/github/LoveKino/defcomment)

## goal

A simple library to translate code commenting to tests or other.

## features

- write simple test data in code comment

- support function test

- support browser and node environment

- support bash test case

## install

`npm i defcomment -g` or `npm i defcomment --save-dev` 

## run

- quickest

`deftest -s project/src` or `./node_modules/.bin/deftest -s project/src`

- assign dest directory and test directory

`deftest -s project/src -t project/test/unit -d project/test/dest`

- watch

Just add `--watch`

`deftest -s project/src --watch`

- environment

Run js tests in browser environment, just add `-e browser` option.

## generate test

### how to write tests in commenting

```js
/**
 * other commenting
 * 
 * the test commenting
 * ## test
 * [
 *    [[1, 2], 3]
 *    [[4, 5], 9]
 * ]
 */
var add = (a, b) => a + b
```

In our code, we use `## test` as commenting title (you can use one or more # before the word test).the next lines after `## test` is our test data. It's a matrix which contain some arrays, each array has two value. The first is input arguments, the second is the expected response.

- js code in  test commenting

In your test data matrix, you can use js code. For example,

```js
/**
 * define map function
 *
 * ## test
 * [
 *      [[v => ++v, [3, 4, 7]], [4, 5, 8]]
 * ]
 */
var map = (handler, data) => {}
```

### how about high order function

When your unit test is about a high order function, you should expand your array. See the example

```js
/**
 * ## test
 * [
 *      [[3], [5], 15],
 *      [[6], [7], 42]
 * ]
 */
var high = (a) => (b) => a * b;
```

### how about exception

If you function throw an error, you could just set expected output as an error.

In the comparation between real output and expected output, just compare error message.

```js
/**
 * ## test
 * [
 *      [[2], 2],
 *      [[-1], new Error('v is too little. v = -1')]
 * ]
 */
var error = (v) => {
    if(v < 0) {
        throw new Error('v is too little. v = ' + v);
    }
    return v;
};
```

### simple bash test

```js
/**
 * ## test tar=bash
 * cd .. && echo 123
 */
```

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