Skip to content

Instantly share code, notes, and snippets.

@webpro
Last active May 26, 2019 19:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webpro/775b108ac5bae505b7a4e98fc1aa0872 to your computer and use it in GitHub Desktop.
Save webpro/775b108ac5bae505b7a4e98fc1aa0872 to your computer and use it in GitHub Desktop.
Minimal test runner that covers most of your needs
const util = require('util');
const result = title => err => {
if (err instanceof Error) {
console.log(`✖ ${title}`);
console.error(err.actual || err);
} else {
console.log(`✔ ${title}`);
}
};
module.exports = (title, fn) => {
const end = result(title);
try {
const maybePromise = fn();
if (util.types.isPromise(maybePromise)) {
maybePromise.then(end).catch(end);
} else {
end();
}
} catch (err) {
end(err);
}
};
const assert = require('assert');
const test = require('./runner');
test('should be simple', () => {
assert(1 === 1);
});
test('should handle promises', async () => {
await assert.rejects(() => Promise.reject());
});
test('should handle async stuff', async () => {
await assert.doesNotReject(() => Promise.resolve());
});
test('should be simple', () => {
assert(1 === 2);
});
test('should handle promises', async () => {
await assert.rejects(() => Promise.resolve());
});
test('should handle async stuff', async () => {
await assert.doesNotReject(() => Promise.reject(new Error('failed!')));
});
$ node test.js
✔ should be simple
✖ should be simple
AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:
assert(1 === 2)
at /Users/lars/Projects/@release-it/plugin-starterkit/test.js:17:3
at module.exports (/Users/lars/Projects/@release-it/plugin-starterkit/runner.js:15:26)
at Object.<anonymous> (/Users/lars/Projects/@release-it/plugin-starterkit/test.js:16:1)
at Module._compile (internal/modules/cjs/loader.js:759:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:770:10)
at Module.load (internal/modules/cjs/loader.js:628:32)
at Function.Module._load (internal/modules/cjs/loader.js:555:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:824:10)
at internal/main/run_main_module.js:17:11 {
generatedMessage: true,
code: 'ERR_ASSERTION',
actual: false,
expected: true,
operator: '=='
}
✔ should handle promises
✔ should handle async stuff
✖ should handle promises
AssertionError [ERR_ASSERTION]: Missing expected rejection.
at processTicksAndRejections (internal/process/task_queues.js:89:5)
at async /Users/lars/Projects/@release-it/plugin-starterkit/test.js:21:3 {
generatedMessage: false,
code: 'ERR_ASSERTION',
actual: undefined,
expected: undefined,
operator: 'rejects'
}
✖ should handle async stuff
Error: failed!
at /Users/lars/Projects/@release-it/plugin-starterkit/test.js:25:51
at waitForActual (assert.js:620:21)
at Function.doesNotReject (assert.js:729:39)
at /Users/lars/Projects/@release-it/plugin-starterkit/test.js:25:16
at module.exports (/Users/lars/Projects/@release-it/plugin-starterkit/runner.js:15:26)
at Object.<anonymous> (/Users/lars/Projects/@release-it/plugin-starterkit/test.js:24:1)
at Module._compile (internal/modules/cjs/loader.js:759:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:770:10)
at Module.load (internal/modules/cjs/loader.js:628:32)
at Function.Module._load (internal/modules/cjs/loader.js:555:12)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment