Skip to content

Commit 70f6e94

Browse files
committed
tests: polyfill globalThis.crypto for tests run in node.js
This is not provided by jsdom. It is not clear why the tests didn't fail previously.
1 parent c81cbc8 commit 70f6e94

File tree

4 files changed

+36
-33
lines changed

4 files changed

+36
-33
lines changed

build_tools/vitest/setup-crypto.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Polyfill for missing `crypto` in jsdom.
2+
3+
import { webcrypto } from "node:crypto";
4+
5+
Object.defineProperty(globalThis, "crypto", {
6+
value: webcrypto,
7+
});

src/sliceview/chunk_format_testing.ts

+20-23
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { describe, it, expect } from "vitest";
17+
import { it, expect } from "vitest";
1818
import type { SingleTextureChunkFormat } from "#src/sliceview/single_texture_chunk_format.js";
1919
import { defineChunkDataShaderAccess } from "#src/sliceview/volume/frontend.js";
2020
import type { TypedArray } from "#src/util/array.js";
@@ -43,7 +43,7 @@ export function chunkFormatTest<TextureLayout extends Disposable>(
4343
for (let channelIndex = 0; channelIndex < numChannels; ++channelIndex) {
4444
outputs[`output${channelIndex}`] = dataType;
4545
}
46-
describe(
46+
it(
4747
`volumeSize = ${volumeSize.join()}, numChannels = ${volumeSize[3]}, ` +
4848
`dataType = ${DataType[dataType]}`,
4949
() => {
@@ -116,28 +116,25 @@ output${channel} = getDataValue(${channel});
116116
const values = tester.values;
117117
for (let channel = 0; channel < numChannels; ++channel) {
118118
const curOffset = offset + channel * strides[3];
119-
it(
119+
const msg =
120120
`volumeSize = ${vec3Key(volumeSize)}, ` +
121-
`positionInChunk = ${vec3Key(positionInChunk)}, ` +
122-
`channel = ${channel}, offset = ${curOffset}`,
123-
() => {
124-
switch (dataType) {
125-
case DataType.UINT64: {
126-
const result = values[`output${channel}`] as Uint64;
127-
expect([result.low, result.high]).toEqual([
128-
rawData[curOffset * 2],
129-
rawData[curOffset * 2 + 1],
130-
]);
131-
break;
132-
}
133-
default: {
134-
const result = values[`output${channel}`];
135-
expect(result).toBe(rawData[curOffset]);
136-
break;
137-
}
138-
}
139-
},
140-
);
121+
`positionInChunk = ${vec3Key(positionInChunk)}, ` +
122+
`channel = ${channel}, offset = ${curOffset}`;
123+
switch (dataType) {
124+
case DataType.UINT64: {
125+
const result = values[`output${channel}`] as Uint64;
126+
expect([result.low, result.high], msg).toEqual([
127+
rawData[curOffset * 2],
128+
rawData[curOffset * 2 + 1],
129+
]);
130+
break;
131+
}
132+
default: {
133+
const result = values[`output${channel}`];
134+
expect(result, msg).toBe(rawData[curOffset]);
135+
break;
136+
}
137+
}
141138
}
142139
}
143140
checkPosition(vec3.fromValues(0, 0, 0));

src/util/uint64.spec.ts

+8-10
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,14 @@ describe("uint64", () => {
8181
expect(temp.tryParseString("1")).toBe(true);
8282
});
8383

84-
describe("parseString toString round trip", () => {
84+
it("parseString toString round trip", () => {
8585
function check(s: string, base: number) {
8686
const x = Uint64.parseString(s, base);
87-
it(`low=${x.low}, high=${x.high}, toString(${base}) = ${x.toString(
87+
const message = `low=${x.low}, high=${x.high}, toString(${base}) = ${x.toString(
8888
base,
89-
)}, s=${s}`, () => {
90-
expect(x.valid()).toBe(true);
91-
expect(x.toString(base)).toEqual(s);
92-
});
89+
)}, s=${s}`;
90+
expect(x.valid(), message).toBe(true);
91+
expect(x.toString(base), message).toEqual(s);
9392
}
9493
check("0", 10);
9594
check("1", 10);
@@ -98,13 +97,12 @@ describe("uint64", () => {
9897
check("3w5e11264sgsf", 36);
9998
});
10099

101-
describe("toString parseString round trip", () => {
100+
it("toString parseString round trip", () => {
102101
function check(x: Uint64, base: number) {
103102
const s = x.toString(base);
104103
const y = Uint64.parseString(s, base);
105-
it(`s=${s}, x.low=${x.low}, x.high=${x.high}, y.low=${y.low}, y.high=${y.high}, base=${base}`, () => {
106-
expect([y.low, y.high]).toEqual([x.low, x.high]);
107-
});
104+
const message = `s=${s}, x.low=${x.low}, x.high=${x.high}, y.low=${y.low}, y.high=${y.high}, base=${base}`;
105+
expect([y.low, y.high], message).toEqual([x.low, x.high]);
108106
}
109107
const count = 100;
110108
{

vitest.workspace.ts

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export default defineWorkspace([
1111
...baseConfig,
1212
test: {
1313
environment: "node",
14+
setupFiles: ["./build_tools/vitest/setup-crypto.ts"],
1415
include: ["src/**/*.spec.ts"],
1516
benchmark: {
1617
include: ["src/**/*.benchmark.ts"],

0 commit comments

Comments
 (0)