Skip to content

Commit 4ac7c87

Browse files
committed
test: Improve testing by using @ts-expect-error
This way we can ensure that we get errors when trying to call using bad types rather than just forcing the test to run, they provide verification of the typings too.
1 parent d59465c commit 4ac7c87

File tree

4 files changed

+83
-26
lines changed

4 files changed

+83
-26
lines changed

.eslintrc.json

-6
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@
1919
"extends": [
2020
"plugin:@typescript-eslint/recommended-requiring-type-checking"
2121
]
22-
},
23-
{
24-
"files": ["src/**/*.spec.ts"],
25-
"rules": {
26-
"@typescript-eslint/no-explicit-any": "off"
27-
}
2822
}
2923
]
3024
}

src/Encoding.spec.ts

+18-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,23 @@ describe("isEncoding", () => {
1515
it("handle falsy cases", () => {
1616
expect(isEncoding("HE")).toBe(false);
1717
expect(isEncoding("XX")).toBe(false);
18-
expect(isEncoding({} as any)).toBe(false);
19-
expect(isEncoding(123 as any)).toBe(false);
20-
expect(isEncoding(undefined as any)).toBe(false);
18+
expect(
19+
isEncoding(
20+
// @ts-expect-error
21+
{},
22+
),
23+
).toBe(false);
24+
expect(
25+
isEncoding(
26+
// @ts-expect-error
27+
123,
28+
),
29+
).toBe(false);
30+
expect(
31+
isEncoding(
32+
// @ts-expect-error
33+
undefined,
34+
),
35+
).toBe(false);
2136
});
2237
});

src/Endianness.spec.ts

+18-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,23 @@ describe("isEndianness", () => {
99
it("handle falsy cases", () => {
1010
expect(isEndianness("HE")).toBe(false);
1111
expect(isEndianness("XX")).toBe(false);
12-
expect(isEndianness({} as any)).toBe(false);
13-
expect(isEndianness(123 as any)).toBe(false);
14-
expect(isEndianness(undefined as any)).toBe(false);
12+
expect(
13+
isEndianness(
14+
// @ts-expect-error
15+
{},
16+
),
17+
).toBe(false);
18+
expect(
19+
isEndianness(
20+
// @ts-expect-error
21+
123,
22+
),
23+
).toBe(false);
24+
expect(
25+
isEndianness(
26+
// @ts-expect-error
27+
undefined,
28+
),
29+
).toBe(false);
1530
});
1631
});

src/WalkableBuffer.spec.ts

+47-14
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,19 @@ describe("constructor", () => {
1717
});
1818

1919
test("throws when providing no or incorrect buffer", () => {
20-
expect(() => new WalkableBuffer({ buffer: null as any })).toThrow();
2120
expect(
22-
() => new WalkableBuffer({ buffer: { a: 1, b: 2 } as any }),
21+
() =>
22+
new WalkableBuffer({
23+
// @ts-expect-error
24+
buffer: null,
25+
}),
26+
).toThrow();
27+
expect(
28+
() =>
29+
new WalkableBuffer({
30+
// @ts-expect-error
31+
buffer: { a: 1, b: 2 },
32+
}),
2333
).toThrow();
2434
});
2535

@@ -39,7 +49,8 @@ describe("constructor", () => {
3949
() =>
4050
new WalkableBuffer({
4151
buffer,
42-
endianness: "QQ" as any,
52+
// @ts-expect-error
53+
endianness: "QQ",
4354
}),
4455
).toThrow(/Invalid endianness/);
4556
});
@@ -60,7 +71,8 @@ describe("constructor", () => {
6071
() =>
6172
new WalkableBuffer({
6273
buffer,
63-
encoding: "STEVEN" as any,
74+
// @ts-expect-error
75+
encoding: "STEVEN",
6476
}),
6577
).toThrow(/Invalid encoding/);
6678
});
@@ -112,7 +124,8 @@ describe("constructor", () => {
112124
() =>
113125
new WalkableBuffer({
114126
buffer,
115-
signed: "hello" as any,
127+
// @ts-expect-error
128+
signed: "hello",
116129
}),
117130
).toThrow();
118131
});
@@ -200,7 +213,8 @@ describe("integer functions", () => {
200213

201214
test("reads NOT (throws)", () => {
202215
expect(() =>
203-
walkableBuffer.get(SHORT, "NOT" as any),
216+
// @ts-expect-error
217+
walkableBuffer.get(SHORT, "NOT"),
204218
).toThrow(/invalid endianness/i);
205219
});
206220
});
@@ -593,7 +607,8 @@ describe("integer functions", () => {
593607

594608
test("reads NOT (throws)", () => {
595609
expect(() =>
596-
walkableBuffer.getBigInt("NOT" as any),
610+
// @ts-expect-error
611+
walkableBuffer.getBigInt("NOT"),
597612
).toThrow(/invalid endianness/i);
598613
});
599614
});
@@ -881,7 +896,8 @@ describe("integer functions", () => {
881896

882897
test("reads NOT (throws)", () => {
883898
expect(() =>
884-
walkableBuffer.peekBigInt(0, "NOT" as any),
899+
// @ts-expect-error
900+
walkableBuffer.peekBigInt(0, "NOT"),
885901
).toThrow(/invalid endianness/i);
886902
});
887903
});
@@ -1098,7 +1114,11 @@ describe("string functions", () => {
10981114

10991115
test("throws on invalid encoding", () => {
11001116
expect(() =>
1101-
u16walkableBuffer.getString(1, "notAnEncoding" as any),
1117+
u16walkableBuffer.getString(
1118+
1,
1119+
//@ts-expect-error
1120+
"notAnEncoding",
1121+
),
11021122
).toThrow(/unknown encoding/i);
11031123
});
11041124
});
@@ -1169,7 +1189,12 @@ describe("string functions", () => {
11691189

11701190
test("throws on invalid encoding", () => {
11711191
expect(() =>
1172-
u16walkableBuffer.peekString(1, 0, "notAnEncoding" as any),
1192+
u16walkableBuffer.peekString(
1193+
1,
1194+
0,
1195+
// @ts-expect-error
1196+
"notAnEncoding",
1197+
),
11731198
).toThrow(/unknown encoding/i);
11741199
});
11751200
});
@@ -1623,7 +1648,12 @@ describe("string functions", () => {
16231648
});
16241649

16251650
test("set NOT (throws)", () => {
1626-
expect(() => wBuf.setEndianness("NOT" as any)).toThrow();
1651+
expect(() =>
1652+
wBuf.setEndianness(
1653+
// @ts-expect-error
1654+
"NOT",
1655+
),
1656+
).toThrow();
16271657
expect(wBuf.getEndianness()).toBe("LE");
16281658
});
16291659
});
@@ -1668,9 +1698,12 @@ describe("string functions", () => {
16681698
});
16691699

16701700
test("set NOT (throws)", () => {
1671-
expect(() => wBuf.setEncoding("NOT" as any)).toThrow(
1672-
/invalid encoding/i,
1673-
);
1701+
expect(() =>
1702+
wBuf.setEncoding(
1703+
// @ts-expect-error
1704+
"NOT",
1705+
),
1706+
).toThrow(/invalid encoding/i);
16741707
expect(wBuf.getEncoding()).toBe("utf8");
16751708
});
16761709
});

0 commit comments

Comments
 (0)