Skip to content

Commit 012dffe

Browse files
committed
📦 Upgrade to vitest 3, adjust tests
Vitest 3 checks more properties when comparing errors via `toEqual` or `toThrow`. This commit adjusts all tests to use `toThrow(newError(...))` as this checks the error type, the _complete_ error message, and optionally also the cause.
1 parent 2d4ad75 commit 012dffe

19 files changed

+434
-796
lines changed

‎package-lock.json‎

Lines changed: 189 additions & 604 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
},
3737
"devDependencies": {
3838
"@types/node": "^22.10.7",
39-
"@vitest/coverage-v8": "^2.1.8",
39+
"@vitest/coverage-v8": "^3.0.2",
4040
"esbuild": "^0.24.2",
4141
"eslint": "^9.18.0",
4242
"eslint-config-prettier": "^10.0.1",
@@ -45,6 +45,6 @@
4545
"prettier": "^3.4.2",
4646
"typescript": "^5.7.3",
4747
"typescript-eslint": "^8.20.0",
48-
"vitest": "^2.1.8"
48+
"vitest": "^3.0.2"
4949
}
5050
}

‎src/api/PdfMaker.test.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import { readFile } from 'node:fs/promises';
22
import { join } from 'node:path';
3-
import { before } from 'node:test';
43

5-
import { describe, expect, it, vi } from 'vitest';
4+
import { beforeEach, describe, expect, it, vi } from 'vitest';
65

76
import { image, text } from './layout.ts';
87
import { PdfMaker } from './PdfMaker.ts';
98

109
describe('makePdf', () => {
1110
let pdfMaker: PdfMaker;
1211

13-
before(async () => {
12+
beforeEach(async () => {
1413
pdfMaker = new PdfMaker();
1514
pdfMaker.setResourceRoot(join(__dirname, '../test/resources'));
1615
const fontData = await readFile(

‎src/base64.test.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,21 @@ describe('decodeBase64', () => {
2121
expect(decodeBase64(base64(array3))).toEqual(array3);
2222
});
2323

24-
it('fails if string is not a multiple of 4', () => {
24+
it('throws if string is not a multiple of 4', () => {
2525
expect(() => decodeBase64('A')).toThrow(
26-
'Invalid base64 string: length must be a multiple of 4',
26+
new Error('Invalid base64 string: length must be a multiple of 4'),
2727
);
2828
expect(() => decodeBase64('AA')).toThrow(
29-
'Invalid base64 string: length must be a multiple of 4',
29+
new Error('Invalid base64 string: length must be a multiple of 4'),
3030
);
3131
expect(() => decodeBase64('AAA')).toThrow(
32-
'Invalid base64 string: length must be a multiple of 4',
32+
new Error('Invalid base64 string: length must be a multiple of 4'),
3333
);
3434
});
3535

36-
it('fails if string contains invalid characters', () => {
37-
expect(() => decodeBase64('ABØ=')).toThrow("Invalid Base64 character 'Ø' at position 2");
36+
it('throws if string contains invalid characters', () => {
37+
expect(() => decodeBase64('ABØ=')).toThrow(
38+
new Error("Invalid Base64 character 'Ø' at position 2"),
39+
);
3840
});
3941
});

‎src/binary-data.test.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ describe('parseBinaryData', () => {
2222
});
2323

2424
it('throws for arrays', () => {
25-
expect(() => parseBinaryData([1, 2, 3])).toThrowError(
26-
'Expected Uint8Array, ArrayBuffer, or base64-encoded string, got: [1, 2, 3]',
25+
expect(() => parseBinaryData([1, 2, 3])).toThrow(
26+
new TypeError('Expected Uint8Array, ArrayBuffer, or base64-encoded string, got: [1, 2, 3]'),
2727
);
2828
});
2929

3030
it('throws for other types', () => {
31-
expect(() => parseBinaryData(23)).toThrowError(
32-
'Expected Uint8Array, ArrayBuffer, or base64-encoded string, got: 23',
31+
expect(() => parseBinaryData(23)).toThrow(
32+
new TypeError('Expected Uint8Array, ArrayBuffer, or base64-encoded string, got: 23'),
3333
);
34-
expect(() => parseBinaryData(null)).toThrowError(
35-
'Expected Uint8Array, ArrayBuffer, or base64-encoded string, got: null',
34+
expect(() => parseBinaryData(null)).toThrow(
35+
new TypeError('Expected Uint8Array, ArrayBuffer, or base64-encoded string, got: null'),
3636
);
3737
});
3838
});

‎src/box.test.ts‎

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,26 @@ describe('parseEdges', () => {
7474
});
7575

7676
it('throws on invalid lengths', () => {
77-
expect(() => parseEdges('')).toThrowError("Expected number or length string, got: ''");
78-
expect(() => parseEdges(Infinity)).toThrowError(
79-
'Expected number or length string, got: Infinity',
77+
expect(() => parseEdges('')).toThrow(
78+
new TypeError("Expected number or length string, got: ''"),
79+
);
80+
expect(() => parseEdges(Infinity)).toThrow(
81+
new TypeError('Expected number or length string, got: Infinity'),
8082
);
8183
});
8284

8385
it('throws on invalid types', () => {
84-
expect(() => parseEdges('')).toThrowError("Expected number or length string, got: ''");
85-
expect(() => parseEdges(null)).toThrowError(
86-
'Expected number, length string, or object, got: null',
86+
expect(() => parseEdges('')).toThrow(
87+
new TypeError("Expected number or length string, got: ''"),
88+
);
89+
expect(() => parseEdges(null)).toThrow(
90+
new TypeError('Expected number, length string, or object, got: null'),
8791
);
88-
expect(() => parseEdges(true)).toThrowError(
89-
'Expected number, length string, or object, got: true',
92+
expect(() => parseEdges(true)).toThrow(
93+
new TypeError('Expected number, length string, or object, got: true'),
9094
);
91-
expect(() => parseEdges(() => 23)).toThrowError(
92-
'Expected number, length string, or object, got: anonymous function',
95+
expect(() => parseEdges(() => 23)).toThrow(
96+
new TypeError('Expected number, length string, or object, got: anonymous function'),
9397
);
9498
});
9599
});
@@ -125,23 +129,35 @@ describe('parseLength', () => {
125129
});
126130

127131
it('throws on invalid strings', () => {
128-
expect(() => parseLength('')).toThrowError("Expected number or length string, got: ''");
129-
expect(() => parseLength('1')).toThrowError("Expected number or length string, got: '1'");
130-
expect(() => parseLength('1xy')).toThrowError("Expected number or length string, got: '1xy'");
132+
expect(() => parseLength('')).toThrow(
133+
new TypeError("Expected number or length string, got: ''"),
134+
);
135+
expect(() => parseLength('1')).toThrow(
136+
new TypeError("Expected number or length string, got: '1'"),
137+
);
138+
expect(() => parseLength('1xy')).toThrow(
139+
new TypeError("Expected number or length string, got: '1xy'"),
140+
);
131141
});
132142

133143
it('throws on invalid numbers', () => {
134-
expect(() => parseLength(Infinity)).toThrowError(
135-
'Expected number or length string, got: Infinity',
144+
expect(() => parseLength(Infinity)).toThrow(
145+
new TypeError('Expected number or length string, got: Infinity'),
146+
);
147+
expect(() => parseLength(NaN)).toThrow(
148+
new TypeError('Expected number or length string, got: NaN'),
136149
);
137-
expect(() => parseLength(NaN)).toThrowError('Expected number or length string, got: NaN');
138150
});
139151

140152
it('throws on invalid types', () => {
141-
expect(() => parseLength(null)).toThrowError('Expected number or length string, got: null');
142-
expect(() => parseLength(true)).toThrowError('Expected number or length string, got: true');
143-
expect(() => parseLength(() => 23)).toThrowError(
144-
'Expected number or length string, got: anonymous function',
153+
expect(() => parseLength(null)).toThrow(
154+
new TypeError('Expected number or length string, got: null'),
155+
);
156+
expect(() => parseLength(true)).toThrow(
157+
new TypeError('Expected number or length string, got: true'),
158+
);
159+
expect(() => parseLength(() => 23)).toThrow(
160+
new TypeError('Expected number or length string, got: anonymous function'),
145161
);
146162
});
147163
});

‎src/data-loader.test.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ describe('createDataLoader', () => {
1212
const loader = createDataLoader();
1313

1414
it('throws for invalid URLs', async () => {
15-
await expect(loader('')).rejects.toThrow("Invalid URL: ''");
16-
await expect(loader('http://')).rejects.toThrow("Invalid URL: 'http://'");
15+
await expect(loader('')).rejects.toThrow(new Error("Invalid URL: ''"));
16+
await expect(loader('http://')).rejects.toThrow(new Error("Invalid URL: 'http://'"));
1717
});
1818

1919
it('throws for unsupported URL scheme', async () => {
20-
await expect(loader('foo:bar')).rejects.toThrow("URL not supported: 'foo:bar'");
20+
await expect(loader('foo:bar')).rejects.toThrow(new Error("URL not supported: 'foo:bar'"));
2121
});
2222

2323
describe('http:', () => {
@@ -49,7 +49,7 @@ describe('createDataLoader', () => {
4949

5050
it('throws if 404 received', async () => {
5151
await expect(loader('https://example.com/not-there')).rejects.toThrow(
52-
'Received 404 Not Found',
52+
new Error('Received 404 Not Found'),
5353
);
5454
});
5555
});
@@ -62,12 +62,12 @@ describe('createDataLoader', () => {
6262
});
6363

6464
it('throws for invalid data: URLs', async () => {
65-
await expect(loader('data:foo')).rejects.toThrow("Invalid data URL: 'data:foo'");
65+
await expect(loader('data:foo')).rejects.toThrow(new Error("Invalid data URL: 'data:foo'"));
6666
});
6767

6868
it('throws for unsupported encoding in data: URLs', async () => {
6969
await expect(loader('data:foo,bar')).rejects.toThrow(
70-
"Unsupported encoding in data URL: 'data:foo,bar'",
70+
new Error("Unsupported encoding in data URL: 'data:foo,bar'"),
7171
);
7272
});
7373
});

‎src/font-store.test.ts‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,15 @@ describe('FontStore', () => {
7676
const store = new FontStore();
7777

7878
await expect(store.selectFont({ fontFamily: 'Foo' })).rejects.toThrow(
79-
expect.objectContaining({
80-
message: "Could not load font for 'Foo', style=normal, weight=normal",
79+
new Error("Could not load font for 'Foo', style=normal, weight=normal", {
8180
cause: new Error('No fonts defined'),
8281
}),
8382
);
8483
});
8584

8685
it('rejects for unknown font name', async () => {
8786
await expect(store.selectFont({ fontFamily: 'Unknown' })).rejects.toThrow(
88-
expect.objectContaining({
89-
message: "Could not load font for 'Unknown', style=normal, weight=normal",
87+
new Error("Could not load font for 'Unknown', style=normal, weight=normal", {
9088
cause: new Error(
9189
"No matching font found for family 'Unknown'. Registered families are: 'Test', 'Other'.",
9290
),
@@ -98,8 +96,7 @@ describe('FontStore', () => {
9896
store = new FontStore([normalFont, boldFont]);
9997

10098
await expect(store.selectFont({ fontFamily: 'Test', fontStyle: 'italic' })).rejects.toThrow(
101-
expect.objectContaining({
102-
message: "Could not load font for 'Test', style=italic, weight=normal",
99+
new Error("Could not load font for 'Test', style=italic, weight=normal", {
103100
cause: new Error("No matching font found for 'Test', style=italic"),
104101
}),
105102
);
@@ -170,8 +167,7 @@ describe('FontStore', () => {
170167

171168
it('rejects if font could not be loaded', async () => {
172169
await expect(store.selectFont({ fontFamily: 'foo' })).rejects.toThrow(
173-
expect.objectContaining({
174-
message: "Could not load font for 'foo', style=normal, weight=normal",
170+
new Error("Could not load font for 'foo', style=normal, weight=normal", {
175171
cause: new Error(
176172
"No matching font found for family 'foo'. Registered families are: 'Test', 'Other'.",
177173
),

‎src/fonts.test.ts‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,31 @@ describe('readFonts', () => {
2626
});
2727

2828
it('throws on missing input', () => {
29-
expect(() => readFonts(undefined)).toThrowError('Expected object, got: undefined');
29+
expect(() => readFonts(undefined)).toThrow(new TypeError('Expected object, got: undefined'));
3030
});
3131

3232
it('throws on invalid type', () => {
33-
expect(() => readFonts(23)).toThrowError('Expected object, got: 23');
33+
expect(() => readFonts(23)).toThrow(new TypeError('Expected object, got: 23'));
3434
});
3535

3636
it('throws on invalid italic value', () => {
3737
const fn = () => readFonts({ Test: [{ data: 'data', italic: 23 }] });
3838

39-
expect(fn).toThrowError('Invalid value for "Test/0/italic":');
39+
expect(fn).toThrow(
40+
new TypeError('Invalid value for "Test/0/italic": Expected boolean, got: 23'),
41+
);
4042
});
4143

4244
it('throws on invalid bold value', () => {
4345
const fn = () => readFonts({ Test: [{ data: 'data', bold: 23 }] });
4446

45-
expect(fn).toThrowError('Invalid value for "Test/0/bold":');
47+
expect(fn).toThrow(new TypeError('Invalid value for "Test/0/bold": Expected boolean, got: 23'));
4648
});
4749

4850
it('throws on missing data', () => {
4951
const fn = () => readFonts({ Test: [{ italic: true }] });
5052

51-
expect(fn).toThrowError('Missing value for "data"');
53+
expect(fn).toThrow(new TypeError('Invalid value for "Test/0": Missing value for "data"'));
5254
});
5355
});
5456

@@ -63,13 +65,13 @@ describe('weightToNumber', () => {
6365
});
6466

6567
it('throws for invalid types', () => {
66-
expect(() => weightToNumber('foo' as any)).toThrowError("Invalid font weight: 'foo'");
67-
expect(() => weightToNumber(null as any)).toThrowError('Invalid font weight: null');
68+
expect(() => weightToNumber('foo' as any)).toThrow(new Error("Invalid font weight: 'foo'"));
69+
expect(() => weightToNumber(null as any)).toThrow(new Error('Invalid font weight: null'));
6870
});
6971

7072
it('throws for invalid numbers', () => {
71-
expect(() => weightToNumber(NaN)).toThrowError('Invalid font weight: NaN');
72-
expect(() => weightToNumber(0.1)).toThrowError('Invalid font weight: 0.1');
73+
expect(() => weightToNumber(NaN)).toThrow(new Error('Invalid font weight: NaN'));
74+
expect(() => weightToNumber(0.1)).toThrow(new Error('Invalid font weight: 0.1'));
7375
});
7476
});
7577

‎src/image-store.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe('ImageStore', () => {
3636
});
3737

3838
it('rejects if image could not be loaded', async () => {
39-
await expect(store.selectImage('foo')).rejects.toThrow("Could not load image 'foo'");
39+
await expect(store.selectImage('foo')).rejects.toThrow(new Error("Could not load image 'foo'"));
4040
});
4141

4242
it('loads registered images (deprecated)', async () => {

0 commit comments

Comments
 (0)