Public
Edited
Mar 12
Importers
13 stars
Also listed in…
Highlighted
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
tests = ({
successWithEnsure() {
return ensure(Math.PI > 3, Math.PI < 4);
},

// To fix this test, remove all errors and falsy values
failWithEnsure() {
ensure("foo", Math.PI < 3, true, false, undefined, new Error());
},

// Tests written with expect.js, see below
helloWorld() {
expect(typeof "hello").to.be("string");
},

// To fix this test, do basic math
"Fail with expect"() {
expect(1 + 1).to.be(3);
},

math: {
subtraction: [
() => {
expect(1 - 0).to.be(1);
},
() => {
expect(1 - 2).to.be(-1);
}
],

add: {
twoNumbers() {
expect(3 + 4).to.be(7);
}
}
},

more: [
() => {
expect(1 - 0).to.be(1);
},
() => {
expect(1 - 2).to.be(-1);
}
],

// async
async fetchSuccess(i, id) {
console.log("Logging...", i, id);

// For demo purposes, this only runs on first iteration (i === 0) when tests are repeated
// Otherwise, changing `repeat` in the slider below would trigger a lot of requests
if (!i) {
await fetch("https://jsonplaceholder.typicode.com/posts/1");
}
},

async fetch404(i) {
if (!i) {
const res = await fetch("https://jsonplaceholder.typicode.com/posts/999");
ensure(!res.ok, res.status === 404);
}
},

// This test will fail (due to browser restrictions)
async fetchError(i) {
if (!i) {
await fetch("http://url.that.fails");
}
},

// This is testing an expected error
async fetchErrorPass(i) {
if (!i) {
const failure = await fails(async () => {
await fetch("http://url.that.fails");
});

ensure(_.isError(failure.error), failure.errorName === "TypeError");
}
}
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
ensure(2 < 1)
Insert cell
Insert cell
ensure(1 < 2)
Insert cell
Insert cell
Insert cell
ensure(
Math.PI,
Math.PI > 3,
Math.PI < 4,
_.isPlainObject({}),
_.isArray([]),
_.isNumber(Date.now()),
_.isDate(new Date()),
_.isEqual({ foo: "bar" }, { foo: "bar" })
)
Insert cell
Insert cell
Insert cell
ensure(
1 < 2,
2 < 1,
Math.PI < 3,
true,
false,
undefined,
_.isNumber("Hello"),
new Error()
)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
ensure(
results,
_.isPlainObject(results),
_.isArray(results.all),
_.isBoolean(run),

// If you set run to false with the toggle above, this will fail
run,

_.isFunction(inspect),
_.isFunction(ensure),
_.isFunction(expect),

// Being clever: use map to avoid repeating the same test function
...[
repeat,
results.testCount,
results.failCount,
results.successCount
].map(
_.isNumber
)
)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function fail(...args) {
throw Error(...args);
}
Insert cell
Insert cell
Insert cell
Insert cell
await fetch("http://url.that.fails")
Insert cell
Insert cell
// This will NOT throw an error, so your test will pass
fails(async () => {
await fetch("http://url.that.fails");
})
Insert cell
Insert cell
// This WILL throw an error and fail your test
fails(() => {
console.log("Foo")
})
Insert cell
Insert cell
Insert cell
Insert cell
{
const { error } = await fails(async () => {
await fetch("http://url.that.fails");
});

return ensure(
_.isError(error),
error.name === "TypeError",
error.message === "Failed to fetch"
);
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
ensure(Math.PI < 3)
Insert cell
inspect(Math.PI < 3)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more