Ask HN: Simplest JavaScript Testing Setup?

2 points by cimmanom ↗ HN
In 2019, what's the simplest setup for single-command local testing of a brand new Javascript library?

The goal is to minimize the NPM and Webpack (et al) yaks that need to be shaved in order to focus on the code first, while including test coverage from the outset.

2 comments

[ 6.2 ms ] story [ 27.5 ms ] thread
test/a-test.js

  const assert = require(‘assert’)

  exports.example = () =>
    assert.fail(‘implement me’)
test/index.js

  // Creates a giant array of test functions from multiple 
  // "suites"/vanilla node modules and executes them.
  const test = suites =>
    suites
      .map(Object.values)
      .reduce((all, tests) => all.concat(tests), [])
      .forEach(test => test())

  test([
    require(‘./a-test’)
  ])

package.json

  ...
  "scripts": {
    "test": "node test/"
  }
  ...
Since you asked for simplest…

In a.js:

    module.exports = (arg) => {
        return arg * 3;
    };
In a.test.js:

    // require module to test
    const testedFunction = require('./a.js');

    // testing
    console.log("Default export", "should multiply argument by two:", true &&
        testedFunction(3) === 3 * 2
    );
In package.json:

    "scripts": {
      "test": "for file in *test*; do node ${file}; done"
    }
In your shell:

    $ npm run test
A few things to note:

* Node.js has console.assert() but it is really noisy.

* console.group() could be used to… group assertions and indent output.

* This works for ES6 modules, too, with --experimental-modules and .mjs extensions.

If you want something serious but still lightweight, there's always https://github.com/MithrilJS/mithril.js/tree/next/ospec.