test-assert-lite
v0.1.0
Published
A lightweight node:test and node:assert compatible library for browsers.
Maintainers
Readme
test-assert-lite
A lightweight node:test and node:assert compatible library for browsers.
- From
node:test:describe/it,testwitht.test()subtests,before/after,skipandtodo - From
node:assert:assertandstrict, withok,equal,deepStrictEqual,throws,rejects,matchand more - The same test file runs in both Node.js and the browser
- Under 32KB minified, under 11KB gzipped, no dependencies
- tacli and its browser runners run
node:testtest suites unchanged. They mapnode:testto this library
SYNOPSIS
BDD style with describe and it:
import {strict as assert} from "test-assert-lite/assert"
import {describe, it} from "test-assert-lite/test"
const parseQuery = (search) => Object.fromEntries(new URLSearchParams(search))
const buildQuery = (params) => new URLSearchParams(params).toString()
describe("buildQuery() from an object", () => {
it("round-trips through parseQuery()", () => {
const params = {q: "cat", page: "2"}
assert.deepEqual(parseQuery("?" + buildQuery(params)), params)
})
it("appends to a URL", () => {
const url = new URL("https://example.com/search")
url.search = buildQuery({q: "cat"})
assert.equal(url.href, "https://example.com/search?q=cat")
})
})See test-assert-lite.d.ts for the supported API.
The same test suite runs under node, and in a browser:
node --test test/query.test.mjs
# https://www.npmjs.com/package/test-assert-cli
tacli --serve test/query.test.mjs
# https://www.npmjs.com/package/webdriver-js-cli
webdriver-js --test test/query.test.mjs
# https://www.npmjs.com/package/playwright-js-cli
chromium-js --test test/query.test.mjstest with subtests:
import {strict as assert} from "test-assert-lite/assert"
import {test} from "test-assert-lite/test"
test("URLSearchParams", async (t) => {
const params = new URLSearchParams("a=1&b=2")
await t.test("reads a key", () => {
assert.equal(params.get("a"), "1")
})
await t.test("is null for a missing key", () => {
assert.equal(params.get("c"), null)
})
})BROWSER MODULE
Tests can also go straight into a page, with no build step.
The minified build is an ES module. An import map maps the package's name to it on a CDN.
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/test-assert-cli/htdocs/styles/test-assert-lite.css">
<script type="importmap">
{
"imports": {
"test-assert-lite": "https://cdn.jsdelivr.net/npm/test-assert-lite/dist/test-assert-lite.min.js",
"test-assert-lite/test": "https://cdn.jsdelivr.net/npm/test-assert-lite/exports/test.js",
"test-assert-lite/assert": "https://cdn.jsdelivr.net/npm/test-assert-lite/exports/assert.js",
"test-assert-lite/process": "https://cdn.jsdelivr.net/npm/test-assert-lite/exports/process.js",
"test-assert-lite/session": "https://cdn.jsdelivr.net/npm/test-assert-lite/exports/session.js"
}
}
</script>
<div id="output"></div>
<script type="module">
import {describe, it} from "test-assert-lite/test"
import {strict as assert} from "test-assert-lite/assert"
import {run, session} from "test-assert-lite/session"
// The report goes to console.log by default. Here it is rendered as HTML in the page.
// session() comes before the first test is declared, and run() closes it.
session({
reporter: "html",
output: html => document.getElementById("output").insertAdjacentHTML("beforeend", html),
})
describe("URL", () => {
it("keeps the host", () => {
assert.equal(new URL("https://example.com/a?b").host, "example.com")
})
})
// run() starts every test registered so far and resolves once they are reported
run().then(result => console.log(result.success ? "PASS" : "FAIL"))
</script>Bundled tests in a page
Or bundle the suites with the library into one ES module, so the page needs no import map. An entry imports the suites and calls run().
// test/browser.mjs
import "./query.test.mjs"
import "./params.test.mjs"
import {run} from "test-assert-lite/session"
run().then(result => console.log(result.success ? "PASS" : "FAIL"))// rollup.config.mjs
import {nodeResolve} from "@rollup/plugin-node-resolve"
export default {
input: "test/browser.mjs",
output: {
file: "htdocs/scripts/tests.js",
format: "es",
},
plugins: [nodeResolve()],
treeshake: false,
}<script type="module" src="./scripts/tests.js"></script>The report goes to the console. The same bundle runs under node unchanged.
SEE ALSO
- https://www.npmjs.com/package/test-assert-lite
- https://www.npmjs.com/package/test-assert-cli
- https://www.npmjs.com/package/webdriver-js-cli
- https://www.npmjs.com/package/playwright-js-cli
- https://github.com/kawanet/test-assert-lite
- https://nodejs.org/api/test.html
- https://nodejs.org/api/assert.html
