
Ask HN: Simplest JavaScript Testing Setup? - cimmanom
In 2019, what&#x27;s the simplest setup for single-command local testing of a brand new Javascript library?<p>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.
======
johncoltrane
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](https://github.com/MithrilJS/mithril.js/tree/next/ospec).

------
assertmezaddy
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/"
      }
      ...

