# dud.js

> JavaScript Testing Library (with a twist)

Latest version **0.0.2** (published 2020-07-01) · ISC license · 0 weekly downloads

## Install

```sh
npm install dud.js
pnpm add dud.js
yarn add dud.js
bun add dud.js
```

## 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.0.2 |
| Published | 2020-07-01 |
| First published | 2020-07-01 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 656 B |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 11 |
| Author | Oscar Olsson |
| Maintainers | happybits |
| Keywords | JavaScript, TDD, Test, Experimental |

## Links

- npm: https://www.npmjs.com/package/dud.js
- Repository: https://github.com/happy-bits/dud
- Homepage: https://github.com/happy-bits/dud#readme
- Issues: https://github.com/happy-bits/dud/issues
- npm.io page: https://npm.io/package/dud.js

## 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
- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads

## Recent versions

- 0.0.2 (latest) — 2020-07-01
- 0.0.1 — 2020-07-01

## README

# dud.js

Experimental JavaScript testing library.

With this testing library you can comment your functions with simple test cases. When *dud.js* is executed it will **run your tests from the comments**.

The benefits are that you can write short and **expressive test cases** + your test cases are **physically close** to your functions. A side effect is that test cases often gives **a pretty good description** of what your function is supposed to do (so you don't need any normal comments)

I work as a teacher and currently use this in my education, but I think the idea might work in the real world as well :)

## Example 1

Here's an example of a function with five tests.

    /* 
        [1, 2, 3, 4]         ➞ [2, 4]
        ["A", "B", "C", "D"] ➞ ["B", "D"]
        ["A", "B"]           ➞ ["B"]
        []                   ➞ []
        null                 ➞ null
    */

    function pickEverySecondElement(list) {
        if (list === null)
            return null

        let result = []

        for (let i = 1; i < list.length; i = i + 2) {
            result.push(list[i])
        }
        return result;
    }


A ten minute clip showing how to use dud.js:

https://youtu.be/2sV_dMO46as

## Example 2

Of course you can have multiple parameters. And you can easily create multiple versions of the same function and run the same test cases on all functions.

    /*
        5, true     ➞ "*6*"
        20, true    ➞ "*21*"
        10, false   ➞ "11"
        15, false   ➞ "16"
    */

    function maybeAddStars(a, b) {
        if (b)
            return `*${a + 1}*`

        return `${a + 1}`
    }

    function maybeAddStars_oneline(a, b) {
        return b === true ? `*${(a + 1)}*` : (a + 1).toString()
    }

## Example 3

If you just write normal comments these lines will be ignored. Only lines that contains '➞' will be interpreted as tests. So here we have four tests:

    /*
        Get the age of the oldest person.

        [{ "name": "Kalle", "age": 22 }]                                ➞ 22
        [{ "name": "Kalle", "age": 22 }, { "name": "Lisa", "age": 43 }] ➞ 43
        [{ "name": "Kalle", "age": 50 }, { "name": "Lisa", "age": 22 }] ➞ 50

        If no object have the property "age" then zero will be returned.

        [] ➞ 0

    */

    function maxage(arr) {
        let result=0
        for(let x of arr)
            result = Math.max(x.age, result)
        return result
    }


## Usage

Install the package:

    npm install @happybits/dud


Create a file **example.js** with this code:

    const dud = require('@happybits/dud')

    dud.run(

        { path: 'tests/' },

        ['maxage'],
        ['maybeAddStars', 'oneline'],
        ['pickEverySecondElement'],
    )

In the folder **tests** add the files maxage.js, maybeAddStars.js, pickEverySecondElement.js

Run **example.js**

## Join me 

Interested in developing this idea in JavaScript or another language like C#?

Send a mail to oo@happybits.se

Oscar

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