Last active
July 12, 2016 11:58
-
-
Save mohayonao/60e53fd0baf028ca33c60ded5ce19119 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| "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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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