mirror of
https://github.com/denoland/deno.git
synced 2024-12-26 00:59:24 -05:00
caa383a583
After some discussion it was found that assertEquals is more common
in JS (vs assertEqual, assertEq) and sounds better in the negated form:
assertNotEquals vs assertNE.
Original: 4cf39d4a14
3.3 KiB
3.3 KiB
Testing
This module provides a few basic utilities to make testing easier and consistent in Deno.
Usage
The module exports a test
function which is the test harness in Deno. It
accepts either a function (including async functions) or an object which
contains a name
property and a fn
property. When running tests and
outputting the results, the name of the past function is used, or if the
object is passed, the name
property is used to identify the test.
Asserts are exposed in testing/asserts.ts
module.
equal
- Deep comparision function, whereactual
andexpected
are compared deeply, and if they vary,equal
returnsfalse
.assert()
- Expects a boolean value, throws if the value isfalse
.assertEquals()
- Uses theequal
comparison and throws if theactual
andexpected
are not equal.assertStrictEq()
- Comparesactual
andexpected
strictly, therefore for non-primitives the values must reference the same instance.assertThrows()
- Expects the passedfn
to throw. Iffn
does not throw, this function does. Also compares any errors thrown to an optional expectedError
class and checks that the error.message
includes an optional string.assertThrowsAsync()
- Expects the passedfn
to be async and throw (or return aPromise
that rejects). If thefn
does not throw or reject, this function will throw asynchronously. Also compares any errors thrown to an optional expectedError
class and checks that the error.message
includes an optional string.
runTests()
executes the declared tests.
Basic usage:
import { runTests, test } from "https://deno.land/std/testing/mod.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
test({
name: "testing example",
fn() {
assertEquals("world", "world"));
assertEquals({ hello: "world" }, { hello: "world" }));
}
});
runTests();
Short syntax (named function instead of object):
test(function example() {
assertEquals("world", "world"));
assertEquals({ hello: "world" }, { hello: "world" }));
});
Using assertStrictEq()
:
test(function isStrictlyEqual() {
const a = {};
const b = a;
assertStrictEq(a, b);
});
// This test fails
test(function isNotStrictlyEqual() {
const a = {};
const b = {};
assertStrictEq(a, b);
});
Using assertThrows()
:
test(function doesThrow() {
assertThrows(() => {
throw new TypeError("hello world!");
});
assertThrows(() => {
throw new TypeError("hello world!");
}, TypeError);
assertThrows(
() => {
throw new TypeError("hello world!");
},
TypeError,
"hello"
);
});
// This test will not pass
test(function fails() {
assertThrows(() => {
console.log("Hello world");
});
});
Using assertThrowsAsync()
:
test(async function doesThrow() {
assertThrowsAsync(async () => {
throw new TypeError("hello world!");
});
assertThrowsAsync(async () => {
throw new TypeError("hello world!");
}, TypeError);
assertThrowsAsync(
async () => {
throw new TypeError("hello world!");
},
TypeError,
"hello"
);
assertThrowsAsync(async () => {
return Promise.reject(new Error());
});
});
// This test will not pass
test(async function fails() {
assertThrowsAsync(async () => {
console.log("Hello world");
});
});