Skip to content

Commit 2911d55

Browse files
authored
test warnings (#1643)
* test warnings * warn asserts return value
1 parent 14ce030 commit 2911d55

9 files changed

+86
-28
lines changed

test/assert.js

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,67 @@
11
import assert from "assert";
22

3-
function warns(run, expected) {
3+
function warns(run, expected = /warning/i) {
44
const actual = [];
55
const warn = console.warn;
6+
let result;
67
try {
78
console.warn = (warning) => void actual.push(warning);
8-
run();
9-
assert.strictEqual(actual.length, 1);
9+
result = run();
10+
assert.strictEqual(actual.length, 1, "expected 1 warning");
1011
assert.match(actual[0], expected);
1112
} finally {
1213
console.warn = warn;
1314
}
15+
return result;
16+
}
17+
18+
async function warnsAsync(run, expected = /warning/i) {
19+
const actual = [];
20+
const warn = console.warn;
21+
let result;
22+
try {
23+
console.warn = (warning) => void actual.push(warning);
24+
result = await run();
25+
assert.strictEqual(actual.length, 1, "expected 1 warning");
26+
assert.match(actual[0], expected);
27+
} finally {
28+
console.warn = warn;
29+
}
30+
return result;
1431
}
1532

1633
function doesNotWarn(run) {
1734
const actual = [];
1835
const warn = console.warn;
36+
let result;
37+
try {
38+
console.warn = (warning) => void actual.push(warning);
39+
result = run();
40+
assert.strictEqual(actual.length, 0, "expected 0 warnings");
41+
} finally {
42+
console.warn = warn;
43+
}
44+
return result;
45+
}
46+
47+
async function doesNotWarnAsync(run) {
48+
const actual = [];
49+
const warn = console.warn;
50+
let result;
1951
try {
2052
console.warn = (warning) => void actual.push(warning);
21-
run();
22-
assert.strictEqual(actual.length, 0);
53+
result = await run();
54+
assert.strictEqual(actual.length, 0, "expected 0 warnings");
2355
} finally {
2456
console.warn = warn;
2557
}
58+
return result;
2659
}
2760

28-
export default {...assert, warns, doesNotWarn};
61+
export default {
62+
...assert,
63+
warns,
64+
warnsAsync,
65+
doesNotWarn,
66+
doesNotWarnAsync
67+
};

test/plot.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import assert from "assert";
21
import {promises as fs} from "fs";
32
import * as path from "path";
43
import beautify from "js-beautify";
4+
import assert from "./assert.js";
55
import it from "./jsdom.js";
66
import * as plots from "./plots/index.js";
77

88
for (const [name, plot] of Object.entries(plots)) {
99
it(`plot ${name}`, async () => {
10-
const root = await plot();
10+
const root = await (name.startsWith("warn") ? assert.warnsAsync : assert.doesNotWarnAsync)(plot);
1111
const ext = root.tagName === "svg" ? "svg" : "html";
1212
for (const svg of root.tagName === "svg" ? [root] : root.querySelectorAll("svg")) {
1313
svg.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", "http://www.w3.org/2000/svg");
@@ -59,7 +59,7 @@ for (const [name, plot] of Object.entries(plots)) {
5959
await fs.writeFile(diffile, actual, "utf8");
6060
}
6161

62-
assert(equal, `${name} must match snapshot`);
62+
assert.ok(equal, `${name} must match snapshot`);
6363
});
6464
}
6565

test/plots/color-misaligned.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
import * as Plot from "@observablehq/plot";
22
import * as d3 from "d3";
33

4-
export function colorMisalignedDivergingDomain() {
4+
export function warnMisalignedDivergingDomain() {
55
return Plot.cellX(d3.range(-5, 6), {x: Plot.identity, fill: Plot.identity}).plot({
66
color: {legend: true, type: "diverging", domain: [-5, 5, 10]}
77
});
88
}
99

10-
export function colorMisalignedLinearDomain() {
10+
export function warnMisalignedLinearDomain() {
1111
return Plot.cellX(d3.range(11), {fill: Plot.identity}).plot({
1212
color: {legend: true, type: "linear", domain: [0, 10, 20], range: ["red", "blue"]}
1313
});
1414
}
1515

16-
export function colorMisalignedLinearDomainReverse() {
16+
export function warnMisalignedLinearDomainReverse() {
1717
return Plot.cellX(d3.range(11), {fill: Plot.identity}).plot({
1818
color: {legend: true, type: "linear", domain: [0, 10, 20], reverse: true, range: ["red", "blue"]}
1919
});
2020
}
2121

22-
export function colorMisalignedLinearRange() {
22+
export function warnMisalignedLinearRange() {
2323
return Plot.cellX(d3.range(11), {fill: Plot.identity}).plot({
2424
color: {legend: true, type: "linear", domain: [0, 10], range: ["red", "blue", "green"]}
2525
});
2626
}
2727

28-
export function colorMisalignedLinearRangeReverse() {
28+
export function warnMisalignedLinearRangeReverse() {
2929
return Plot.cellX(d3.range(11), {fill: Plot.identity}).plot({
3030
color: {legend: true, type: "linear", domain: [0, 10], reverse: true, range: ["red", "blue", "green"]}
3131
});

test/scales/scales-test.js

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as Plot from "@observablehq/plot";
22
import * as d3 from "d3";
3-
import assert from "assert";
3+
import assert from "../assert.js";
44
import it from "../jsdom.js";
55

66
it("Plot throws an error if an ordinal position scale has a huge inferred domain", () => {
@@ -411,7 +411,10 @@ it("plot(…).scale(name) handles a reversed diverging scale with a descending d
411411
});
412412

413413
it("plot(…).scale(name) ignores extra domain elements with a diverging scale", async () => {
414-
const plot = Plot.plot({color: {type: "diverging", domain: [-5, 5, 10]}});
414+
const plot = assert.warns(
415+
() => Plot.plot({color: {type: "diverging", domain: [-5, 5, 10]}}),
416+
/domain contains extra/
417+
);
415418
const {interpolate, ...color} = plot.scale("color");
416419
scaleEqual(color, {
417420
type: "diverging",
@@ -722,9 +725,13 @@ it("plot(…).scale('color') can return a “polylinear” piecewise linear scal
722725
});
723726

724727
it("plot(…).scale('color') ignores extra domain elements with an explicit range", () => {
725-
const plot = Plot.cellX([100, 200, 300, 400], {fill: Plot.identity}).plot({
726-
color: {type: "linear", domain: [0, 100, 200], range: ["red", "blue"]}
727-
});
728+
const plot = assert.warns(
729+
() =>
730+
Plot.cellX([100, 200, 300, 400], {fill: Plot.identity}).plot({
731+
color: {type: "linear", domain: [0, 100, 200], range: ["red", "blue"]}
732+
}),
733+
/domain contains extra/
734+
);
728735
scaleEqual(plot.scale("color"), {
729736
type: "linear",
730737
domain: [0, 100],
@@ -735,9 +742,13 @@ it("plot(…).scale('color') ignores extra domain elements with an explicit rang
735742
});
736743

737744
it("plot(…).scale('color') ignores extra range elements with an explicit range", () => {
738-
const plot = Plot.cellX([100, 200, 300, 400], {fill: Plot.identity}).plot({
739-
color: {type: "linear", domain: [0, 100], range: ["red", "blue", "green"]}
740-
});
745+
const plot = assert.warns(
746+
() =>
747+
Plot.cellX([100, 200, 300, 400], {fill: Plot.identity}).plot({
748+
color: {type: "linear", domain: [0, 100], range: ["red", "blue", "green"]}
749+
}),
750+
/range contains extra/
751+
);
741752
scaleEqual(plot.scale("color"), {
742753
type: "linear",
743754
domain: [0, 100],
@@ -748,9 +759,13 @@ it("plot(…).scale('color') ignores extra range elements with an explicit range
748759
});
749760

750761
it("plot(…).scale('color') ignores extra domain elements with an explicit range when reversed", () => {
751-
const plot = Plot.cellX([100, 200, 300, 400], {fill: Plot.identity}).plot({
752-
color: {type: "linear", domain: [0, 100, 200], range: ["red", "blue"], reverse: true}
753-
});
762+
const plot = assert.warns(
763+
() =>
764+
Plot.cellX([100, 200, 300, 400], {fill: Plot.identity}).plot({
765+
color: {type: "linear", domain: [0, 100, 200], range: ["red", "blue"], reverse: true}
766+
}),
767+
/domain contains extra/
768+
);
754769
scaleEqual(plot.scale("color"), {
755770
type: "linear",
756771
domain: [100, 0],
@@ -761,9 +776,13 @@ it("plot(…).scale('color') ignores extra domain elements with an explicit rang
761776
});
762777

763778
it("plot(…).scale('color') ignores extra range elements with an explicit range when reversed", () => {
764-
const plot = Plot.cellX([100, 200, 300, 400], {fill: Plot.identity}).plot({
765-
color: {type: "linear", domain: [0, 100], range: ["red", "blue", "green"], reverse: true}
766-
});
779+
const plot = assert.warns(
780+
() =>
781+
Plot.cellX([100, 200, 300, 400], {fill: Plot.identity}).plot({
782+
color: {type: "linear", domain: [0, 100], range: ["red", "blue", "green"], reverse: true}
783+
}),
784+
/range contains extra/
785+
);
767786
scaleEqual(plot.scale("color"), {
768787
type: "linear",
769788
domain: [100, 0],

0 commit comments

Comments
 (0)