testdouble / teenytest

A very simple, zero-config test runner for Node.js
MIT License
96 stars 14 forks source link

provide default callback in index.js #19

Closed mweitzel closed 8 years ago

mweitzel commented 8 years ago

Extract cli's callback into lib/cli/exit-on-failure and use as default callback. This lets you call teenytest via the api with even less configuration.

Where the readme suggests:

  teenytest('test/lib/**/*.js', {
    helperPath: 'test/helper.js', // module that exports test hook functions (default: null)
    output: console.log, // output for writing results
    cwd: process.cwd(), // base path for test globs & helper path,
    asyncTimeout: 5000 // milliseconds to wait before triggering failure of async tests & hooks
  }, function(er, passing) {
    process.exit(!er && passing ? 0 : 1)
  })

you could use defaults by invoking as

  teenytest('test/lib/**/*.js', function(er, passing) {
    process.exit(!er && passing ? 0 : 1)
  })

but now you can just invoke as

  teenytest('test/lib/**/*.js')

This may seem inconsiquential, but among other things it allows a one-liner to be added to test modules which lets them self invoke with node my-test.js, as well as conventionally with teenytest my-test.js

For example, a module (foo.js) containing the following:

module.exports = function() {
  console.log('i will run as a test!')
}

module.parent || require('teenytest')(module.filename)

will still run with teenytest foo.js as well as standalone with node foo

searls commented 8 years ago

Hey Matthew, the reason I didn't merge this right away is because I'm really uneasy about this. process.exit is a very blunt instrument, and it'd be super hard for someone to debug if they stumbled upon this by not providing a callback argument.

If anything, I'd like to change the CLI behavior to not explicitly use process.exit, as I've even seen that eat some teenytest-scoped errors waiting to flush to stderr.