Skip to content

Instantly share code, notes, and snippets.

@mohayonao
Last active July 12, 2016 11:58
Show Gist options
  • Select an option

  • Save mohayonao/60e53fd0baf028ca33c60ded5ce19119 to your computer and use it in GitHub Desktop.

Select an option

Save mohayonao/60e53fd0baf028ca33c60ded5ce19119 to your computer and use it in GitHub Desktop.
"use strict";
function test(name, testfn) {
function ptest(promise, timeout) {
const timerId = setTimeout(() => {
console.error(`test failed: ${ name }`);
throw Error(`timeout (${ timeout }ms)`);
}, timeout);
promise.then(() => {
clearTimeout(timerId);
}, (e) => {
clearTimeout(timerId);
setImmediate(() => {
console.error(`test failed: ${ name }`);
throw e;
});
});
}
try {
const promise = testfn();
if (promise && typeof promise.then === "function") {
ptest(promise, test.timeout);
}
} catch (e) {
console.error(`test failed: ${ name }`);
throw e;
}
};
test.timeout = 2000;
module.exports = test;
const assert = require("assert");
const test = require("./test");
test("ok", () => {
assert("ok");
}); // テストが通ると何もしない
test("failed", () => {
assert(10 === 20);
}); // テストが失敗すると、テスト名とエラーを表示 (以下は実行されない)
test("async:ok", () => {
return Promise.resolve("ok");
}); // Promiseを返すと非同期テスト
test.timeout = 2000;
test("async:timeout", () => {
return new Promise(() => { /* noop */ });
}); // タイムアウトの場合
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment