Skip to content

Commit fdd410a

Browse files
committed
Require assertion message for assert and assert.ok
1 parent 2807344 commit fdd410a

File tree

6 files changed

+107
-32
lines changed

6 files changed

+107
-32
lines changed

config-v-next/eslint.cjs

+11
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,17 @@ function createConfig(configFilePath, packageEntryPoints = []) {
220220
message:
221221
"Use non-strict methods when importing from 'node:assert/strict'",
222222
},
223+
{
224+
selector: "CallExpression[callee.name='assert'][arguments.length<2]",
225+
message:
226+
"assert should provide an error message as the second argument",
227+
},
228+
{
229+
selector:
230+
"CallExpression[callee.object.name='assert'][callee.property.name='ok'][arguments.length<2]",
231+
message:
232+
"assert.ok should provide an error message as the second argument",
233+
},
223234
],
224235
"@typescript-eslint/restrict-plus-operands": "error",
225236
"@typescript-eslint/restrict-template-expressions": [

v-next/hardhat-build-system/test/index.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,11 @@ describe("build-system", () => {
213213
const buildSystem = new BuildSystem(config);
214214
await buildSystem.build();
215215

216+
const calledWithExpectedMessage =
217+
"Compiled 4 Solidity files successfully (evm targets: paris, petersburg, shanghai, unknown evm version for solc version 0.4.11).";
216218
assert(
217-
spyFunctionConsoleLog.calledWith(
218-
"Compiled 4 Solidity files successfully (evm targets: paris, petersburg, shanghai, unknown evm version for solc version 0.4.11).",
219-
),
219+
spyFunctionConsoleLog.calledWith(calledWithExpectedMessage),
220+
`expected console.log to be called with "${calledWithExpectedMessage}" but got "${spyFunctionConsoleLog.args[0][0]}"`,
220221
);
221222

222223
spyFunctionConsoleLog.restore();

v-next/hardhat-errors/test/errors.ts

+34-13
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,56 @@ const mockErrorDescriptor = {
2525
describe("HardhatError", () => {
2626
describe("Type guard", () => {
2727
it("Should return true for HardhatErrors", () => {
28+
const error = new HardhatError(mockErrorDescriptor);
2829
assert.ok(
29-
HardhatError.isHardhatError(new HardhatError(mockErrorDescriptor)),
30+
HardhatError.isHardhatError(error),
31+
`error ${error.number} is a HardhatError, but isHardhatError returned false`,
3032
);
3133
});
3234

3335
it("Should return true for HardhatErrors with the same ErrorDescriptor", () => {
36+
const error = new HardhatError(mockErrorDescriptor);
3437
assert.ok(
35-
HardhatError.isHardhatError(
36-
new HardhatError(mockErrorDescriptor),
37-
mockErrorDescriptor,
38-
),
38+
HardhatError.isHardhatError(error, mockErrorDescriptor),
39+
`error ${error.number} matches the descriptor ${JSON.stringify(mockErrorDescriptor, null, 2)}, but isHardhatError returned false`,
3940
);
4041
});
4142

4243
it("Should return false for everything else", () => {
43-
assert.ok(!HardhatError.isHardhatError(new Error()));
44-
assert.ok(!HardhatError.isHardhatError(undefined));
45-
assert.ok(!HardhatError.isHardhatError(null));
46-
assert.ok(!HardhatError.isHardhatError(123));
47-
assert.ok(!HardhatError.isHardhatError("123"));
48-
assert.ok(!HardhatError.isHardhatError({ asd: 123 }));
44+
assert.ok(
45+
!HardhatError.isHardhatError(new Error()),
46+
"new Error() is not a HardhatError, but isHardhatError returned true",
47+
);
48+
assert.ok(
49+
!HardhatError.isHardhatError(undefined),
50+
"undefined is not a HardhatError, but isHardhatError returned true",
51+
);
52+
assert.ok(
53+
!HardhatError.isHardhatError(null),
54+
"null is not a HardhatError, but isHardhatError returned true",
55+
);
56+
assert.ok(
57+
!HardhatError.isHardhatError(123),
58+
"123 is not a HardhatError, but isHardhatError returned true",
59+
);
60+
assert.ok(
61+
!HardhatError.isHardhatError("123"),
62+
'"123" is not a HardhatError, but isHardhatError returned true',
63+
);
64+
assert.ok(
65+
!HardhatError.isHardhatError({ asd: 123 }),
66+
"{ asd: 123 } is not a HardhatError, but isHardhatError returned true",
67+
);
4968
});
5069

5170
it("Should return false for HardhatErrors with a different ErrorDescriptor", () => {
71+
const error = new HardhatError(mockErrorDescriptor);
5272
assert.ok(
53-
!HardhatError.isHardhatError(new HardhatError(mockErrorDescriptor), {
73+
!HardhatError.isHardhatError(error, {
5474
...mockErrorDescriptor,
5575
number: 1,
5676
}),
77+
`error ${error.number} doesn't match the descriptor ${JSON.stringify(mockErrorDescriptor, null, 2)}, but isHardhatError returned true`,
5778
);
5879
});
5980
});
@@ -400,7 +421,7 @@ describe("Type tests", () => {
400421
describe("Edge cases", () => {
401422
it("Should support {}", () => {
402423
expectTypeOf<MessagetTemplateArguments<"foo {} {}">>().toEqualTypeOf<{
403-
/* eslint-disable-next-line @typescript-eslint/naming-convention --
424+
/* eslint-disable-next-line @typescript-eslint/naming-convention --
404425
This test case is intentionally testing a weird variable name */
405426
"": ErrorMessageTemplateValue;
406427
}>();

v-next/hardhat-utils/test/lang.ts

+41-11
Original file line numberDiff line numberDiff line change
@@ -342,20 +342,50 @@ describe("lang", () => {
342342

343343
describe("isObject", () => {
344344
it("Should return true for objects", () => {
345-
assert.ok(isObject({}));
346-
assert.ok(isObject({ a: 1 }));
347-
assert.ok(isObject(new Date()));
348-
assert.ok(isObject(new Map()));
349-
assert.ok(isObject(new Set()));
345+
assert.ok(isObject({}), "{} is an object, but isObject returned false");
346+
assert.ok(
347+
isObject({ a: 1 }),
348+
"{ a: 1 } is an object, but isObject returned false",
349+
);
350+
assert.ok(
351+
isObject(new Date()),
352+
"new Date() is an object, but isObject returned false",
353+
);
354+
assert.ok(
355+
isObject(new Map()),
356+
"new Map() is an object, but isObject returned false",
357+
);
358+
assert.ok(
359+
isObject(new Set()),
360+
"new Set() is an object, but isObject returned false",
361+
);
350362
});
351363

352364
it("Should return false for non-objects", () => {
353-
assert.ok(!isObject(null));
354-
assert.ok(!isObject(undefined));
355-
assert.ok(!isObject([]));
356-
assert.ok(!isObject(""));
357-
assert.ok(!isObject(42));
358-
assert.ok(!isObject(true));
365+
assert.ok(
366+
!isObject(null),
367+
"null is not an object, but isObject returned true",
368+
);
369+
assert.ok(
370+
!isObject(undefined),
371+
"undefined is not an object, but isObject returned true",
372+
);
373+
assert.ok(
374+
!isObject([]),
375+
"[] is not an object, but isObject returned true",
376+
);
377+
assert.ok(
378+
!isObject(""),
379+
"'' is not an object, but isObject returned true",
380+
);
381+
assert.ok(
382+
!isObject(42),
383+
"42 is not an object, but isObject returned true",
384+
);
385+
assert.ok(
386+
!isObject(true),
387+
"true is not an object, but isObject returned true",
388+
);
359389
});
360390
});
361391
});

v-next/hardhat-zod-utils/test/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ import { describe, it } from "node:test";
33

44
describe("Example tests", () => {
55
it("foo", function () {
6-
assert.ok(true);
6+
assert.ok(true, "this shouldn't fail");
77
});
88
});

v-next/hardhat/test/hre/index.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ describe("HRE", () => {
6565
it("should load a config file in the current directory", async () => {
6666
const configPath = await resolveConfigPath();
6767

68-
assert(configPath.endsWith("hardhat.config.js"));
68+
assert(
69+
configPath.endsWith("hardhat.config.js"),
70+
`expected configPath to end with hardhat.config.js, but got ${configPath}`,
71+
);
6972
});
7073
});
7174

@@ -75,7 +78,10 @@ describe("HRE", () => {
7578
it("should load a config file in the parent directory", async () => {
7679
const configPath = await resolveConfigPath();
7780

78-
assert(configPath.endsWith("hardhat.config.js"));
81+
assert(
82+
configPath.endsWith("hardhat.config.js"),
83+
`expected configPath to end with hardhat.config.js, but got ${configPath}`,
84+
);
7985
});
8086
});
8187
});
@@ -87,7 +93,10 @@ describe("HRE", () => {
8793
it("should load a config file in the current directory", async () => {
8894
const configPath = await resolveConfigPath();
8995

90-
assert(configPath.endsWith("hardhat.config.ts"));
96+
assert(
97+
configPath.endsWith("hardhat.config.ts"),
98+
`expected configPath to end with hardhat.config.js, but got ${configPath}`,
99+
);
91100
});
92101
});
93102

@@ -97,7 +106,10 @@ describe("HRE", () => {
97106
it("should load a config file in the parent directory", async () => {
98107
const configPath = await resolveConfigPath();
99108

100-
assert(configPath.endsWith("hardhat.config.ts"));
109+
assert(
110+
configPath.endsWith("hardhat.config.ts"),
111+
`expected configPath to end with hardhat.config.js, but got ${configPath}`,
112+
);
101113
});
102114
});
103115
});

0 commit comments

Comments
 (0)