Comparing version 1.3.1 to 1.4.0
61
ospec.js
@@ -87,5 +87,31 @@ /* eslint-disable no-bitwise, no-process-exit */ | ||
var fn = fns[cursor++] | ||
var timeout = 0, delay = 200, s = new Date | ||
var isDone = false | ||
function done(err) { | ||
if (err) { | ||
if (err.message) record(err.message, err) | ||
else record(err) | ||
subjects.pop() | ||
next() | ||
} | ||
if (timeout !== undefined) { | ||
timeout = clearTimeout(timeout) | ||
if (delay !== Infinity) record(null) | ||
if (!isDone) next() | ||
else throw new Error("`" + arg + "()` should only be called once") | ||
isDone = true | ||
} | ||
else console.log("# elapsed: " + Math.round(new Date - s) + "ms, expected under " + delay + "ms") | ||
} | ||
function startTimer() { | ||
timeout = setTimeout(function() { | ||
timeout = undefined | ||
record("async test timed out") | ||
next() | ||
}, Math.min(delay, 2147483647)) | ||
} | ||
if (fn.length > 0) { | ||
var timeout = 0, delay = 200, s = new Date | ||
var isDone = false | ||
var body = fn.toString() | ||
@@ -95,29 +121,19 @@ var arg = (body.match(/\(([\w$]+)/) || body.match(/([\w$]+)\s*=>/) || []).pop() | ||
try { | ||
fn(function done() { | ||
if (timeout !== undefined) { | ||
timeout = clearTimeout(timeout) | ||
if (delay !== Infinity) record(null) | ||
if (!isDone) next() | ||
else throw new Error("`" + arg + "()` should only be called once") | ||
isDone = true | ||
} | ||
else console.log("# elapsed: " + Math.round(new Date - s) + "ms, expected under " + delay + "ms") | ||
}, function(t) {delay = t}) | ||
fn(done, function(t) {delay = t}) | ||
} | ||
catch (e) { | ||
record(e.message, e) | ||
subjects.pop() | ||
next() | ||
done(e) | ||
} | ||
if (timeout === 0) { | ||
timeout = setTimeout(function() { | ||
timeout = undefined | ||
record("async test timed out") | ||
next() | ||
}, Math.min(delay, 2147483647)) | ||
startTimer() | ||
} | ||
} | ||
else { | ||
fn() | ||
nextTickish(next) | ||
var p = fn() | ||
if (p && p.then) { | ||
startTimer() | ||
p.then(function() { done() }, done) | ||
} else { | ||
nextTickish(next) | ||
} | ||
} | ||
@@ -211,2 +227,3 @@ } | ||
function serialize(value) { | ||
if (hasProcess) return require("util").inspect(value) | ||
if (value === null || (typeof value === "object" && !(value instanceof Array)) || typeof value === "number") return String(value) | ||
@@ -213,0 +230,0 @@ else if (typeof value === "function") return value.name || "<anonymous function>" |
{ | ||
"name": "ospec", | ||
"version": "1.3.1", | ||
"version": "1.4.0", | ||
"description": "Noiseless testing framework", | ||
@@ -10,3 +10,3 @@ "main": "ospec.js", | ||
"keywords": [ "testing" ], | ||
"author": "Leo Horie <leohorie@hotmail.com>", | ||
"author": "Leo Horie <lhorie@hotmail.com>", | ||
"license": "MIT", | ||
@@ -13,0 +13,0 @@ "bin": { |
@@ -151,2 +151,18 @@ ospec [![NPM Version](https://img.shields.io/npm/v/ospec.svg)](https://www.npmjs.com/package/ospec) [![NPM License](https://img.shields.io/npm/l/ospec.svg)](https://www.npmjs.com/package/ospec) | ||
Alternativly you can return a promise or even use an async function in tests: | ||
```javascript | ||
o("promise test", function() { | ||
return new Promise(function(resolve) { | ||
setTimeout(resolve, 10) | ||
}) | ||
}) | ||
``` | ||
```javascript | ||
o("promise test", async function() { | ||
await someOtherAsyncFunction() | ||
}) | ||
``` | ||
By default, asynchronous tests time out after 20ms. This can be changed on a per-test basis using the `timeout` argument: | ||
@@ -162,4 +178,19 @@ | ||
Note that the `timeout` function call must be the first statement in its test. | ||
Note that the `timeout` function call must be the first statement in its test. This currently does not work for promise tests. You can combine both methods to do this: | ||
```javascript | ||
o("promise test", function(done, timeout) { | ||
timeout(1000) | ||
someOtherAsyncFunctionThatTakes900ms().then(done) | ||
}) | ||
``` | ||
```javascript | ||
o("promise test", async function(done, timeout) { | ||
timeout(1000) | ||
await someOtherAsyncFunctionThatTakes900ms() | ||
done() | ||
}) | ||
``` | ||
Asynchronous tests generate an assertion that succeeds upon calling `done` or fails on timeout with the error message `async test timed out`. | ||
@@ -166,0 +197,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
477
23697
6
242