Skip to content

Commit f101294

Browse files
authored
Merge pull request #5598 from NomicFoundation/mocha-test-runner
Mocha test plugin
2 parents e7c33b6 + 7ee9f04 commit f101294

File tree

34 files changed

+594
-23
lines changed

34 files changed

+594
-23
lines changed

pnpm-lock.yaml

+85-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

v-next/example-project/hardhat.config.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
import type { HardhatUserConfig } from "@ignored/hardhat-vnext/types/config";
2+
13
import { HardhatPluginError } from "@ignored/hardhat-vnext/plugins";
24

35
import {
46
task,
57
emptyTask,
68
configVariable,
79
globalOption,
8-
HardhatUserConfig,
910
} from "@ignored/hardhat-vnext/config";
1011
import HardhatNodeTestRunner from "@ignored/hardhat-vnext-node-test-runner";
12+
import HardhatMochaTestRunner from "@ignored/hardhat-vnext-mocha-test-runner";
1113

1214
const exampleEmptyTask = emptyTask("empty", "An example empty task").build();
1315

@@ -102,8 +104,18 @@ const config: HardhatUserConfig = {
102104
exampleEmptySubtask,
103105
greeting,
104106
],
105-
plugins: [pluginExample, HardhatNodeTestRunner],
107+
plugins: [
108+
pluginExample,
109+
HardhatMochaTestRunner,
110+
// if testing node plugin, use the following line instead
111+
// HardhatNodeTestRunner,
112+
],
106113
privateKey: configVariable("privateKey"),
114+
paths: {
115+
tests: "test/mocha",
116+
// if testing node plugin, use the following line instead
117+
// tests: "test/node",
118+
},
107119
};
108120

109121
export default config;

v-next/example-project/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
},
2323
"devDependencies": {
2424
"@ignored/hardhat-vnext": "workspace:^3.0.0-next.3",
25+
"@ignored/hardhat-vnext-mocha-test-runner": "workspace:^3.0.0-next.2",
2526
"@ignored/hardhat-vnext-node-test-runner": "workspace:^3.0.0-next.2",
27+
"@types/mocha": ">=9.1.0",
2628
"@types/node": "^20.14.9",
29+
"mocha": "^10.0.0",
2730
"prettier": "3.2.5",
2831
"typescript": "~5.5.0"
29-
},
30-
"dependencies": {
31-
"tsx": "^4.11.0"
3232
}
3333
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import assert from "node:assert/strict";
2+
import { describe, it } from "mocha";
3+
4+
describe("Mocha test", () => {
5+
it("should work", () => {
6+
assert.equal(1 + 1, 2);
7+
});
8+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import assert from "node:assert/strict";
2+
import { describe, it } from "mocha";
3+
4+
import hre from "@ignored/hardhat-vnext";
5+
6+
describe("Other mocha test", () => {
7+
it("should have the example task", () => {
8+
assert.ok(hre.tasks.getTask("example"));
9+
});
10+
});

v-next/example-project/tsconfig.json

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
"references": [
44
{
55
"path": "../hardhat"
6+
},
7+
{
8+
"path": "../hardhat-node-test-runner"
9+
},
10+
{
11+
"path": "../hardhat-mocha-test-runner"
612
}
713
]
814
}

v-next/hardhat-errors/src/descriptors.ts

+7
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,13 @@ Please double check your script's path.`,
473473
474474
Please check Hardhat's output for more details.`,
475475
},
476+
TEST_TASK_ESM_TESTS_RUN_TWICE: {
477+
number: 602,
478+
messageTemplate: `Your project uses ESM and you've programmatically run your tests twice. This is not supported yet.`,
479+
websiteTitle: "Running tests twice in an ESM project",
480+
websiteDescription:
481+
'You have run your tests twice programmatically and your project is an ESM project (you have `"type": "module"` in your `package.json`, or some of your files have the `.mjs` extension). This is not supported by Mocha yet (https://github.com/mochajs/mocha/issues/2706).',
482+
},
476483
},
477484
NETWORK: {
478485
INVALID_URL: {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const { createConfig } = require("../../config-v-next/eslint.cjs");
2+
3+
module.exports = createConfig(__filename);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Node modules
2+
/node_modules
3+
4+
# Compilation output
5+
/dist
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/node_modules
2+
/dist
3+
CHANGELOG.md
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Nomic Foundation
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Hardhat's `mocha` test runner
2+
3+
This package includes Hardhat's `mocha` test runner.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{
2+
"name": "@ignored/hardhat-vnext-mocha-test-runner",
3+
"version": "3.0.0-next.2",
4+
"description": "A mocha test runner",
5+
"homepage": "https://github.com/nomicfoundation/hardhat/tree/v-next/v-next/hardhat-mocha-test-runner",
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/NomicFoundation/hardhat",
9+
"directory": "v-next/hardhat-mocha-test-runner"
10+
},
11+
"author": "Nomic Foundation",
12+
"license": "MIT",
13+
"type": "module",
14+
"exports": {
15+
".": "./dist/src/index.js"
16+
},
17+
"keywords": [
18+
"ethereum",
19+
"smart-contracts",
20+
"hardhat"
21+
],
22+
"scripts": {
23+
"lint": "pnpm prettier --check && pnpm eslint",
24+
"lint:fix": "pnpm prettier --write && pnpm eslint --fix",
25+
"eslint": "eslint \"src/**/*.ts\" \"test/**/*.ts\"",
26+
"prettier": "prettier \"**/*.{ts,js,md,json}\"",
27+
"test": "node --import tsx/esm --test --test-reporter=@ignored/hardhat-vnext-node-test-reporter \"test/*.ts\" \"test/!(fixture-projects|helpers)/**/*.ts\"",
28+
"test:only": "node --import tsx/esm --test --test-only --test-reporter=@ignored/hardhat-vnext-node-test-reporter \"test/*.ts\" \"test/!(fixture-projects|helpers)/**/*.ts\"",
29+
"pretest": "pnpm build",
30+
"build": "tsc --build .",
31+
"prepublishOnly": "pnpm build",
32+
"clean": "rimraf dist"
33+
},
34+
"files": [
35+
"dist/src/",
36+
"src/",
37+
"CHANGELOG.md",
38+
"LICENSE",
39+
"README.md"
40+
],
41+
"devDependencies": {
42+
"@eslint-community/eslint-plugin-eslint-comments": "^4.3.0",
43+
"@ignored/hardhat-vnext-node-test-reporter": "workspace:^3.0.0-next.2",
44+
"@nomicfoundation/hardhat-test-utils": "workspace:^",
45+
"@types/mocha": ">=9.1.0",
46+
"@types/node": "^20.14.9",
47+
"@typescript-eslint/eslint-plugin": "^7.7.1",
48+
"@typescript-eslint/parser": "^7.7.1",
49+
"eslint": "8.57.0",
50+
"eslint-config-prettier": "9.1.0",
51+
"eslint-import-resolver-typescript": "^3.6.1",
52+
"eslint-plugin-import": "2.29.1",
53+
"eslint-plugin-no-only-tests": "3.1.0",
54+
"expect-type": "^0.19.0",
55+
"prettier": "3.2.5",
56+
"rimraf": "^5.0.5",
57+
"typescript": "~5.5.0",
58+
"typescript-eslint": "7.7.1"
59+
},
60+
"dependencies": {
61+
"@ignored/hardhat-vnext": "workspace:^3.0.0-next.2",
62+
"@ignored/hardhat-vnext-errors": "workspace:^3.0.0-next.2",
63+
"@ignored/hardhat-vnext-utils": "workspace:^3.0.0-next.2",
64+
"@ignored/hardhat-vnext-zod-utils": "workspace:^3.0.0-next.2",
65+
"mocha": "^10.0.0",
66+
"tsx": "^4.11.0",
67+
"zod": "^3.23.8"
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import type { ConfigHooks } from "@ignored/hardhat-vnext/types/hooks";
2+
3+
import { validateUserConfigZodType } from "@ignored/hardhat-vnext-zod-utils";
4+
import { z } from "zod";
5+
6+
const mochaConfigType = z.object({
7+
allowUncaught: z.boolean().optional(),
8+
asyncOnly: z.boolean().optional(),
9+
bail: z.boolean().optional(),
10+
checkLeaks: z.boolean().optional(),
11+
color: z.boolean().optional(),
12+
delay: z.boolean().optional(),
13+
diff: z.boolean().optional(),
14+
dryRun: z.boolean().optional(),
15+
failZero: z.boolean().optional(),
16+
fgrep: z.string().optional(),
17+
forbidOnly: z.boolean().optional(),
18+
forbidPending: z.boolean().optional(),
19+
fullTrace: z.boolean().optional(),
20+
globals: z.array(z.string()).optional(),
21+
grep: z.string().optional(),
22+
growl: z.boolean().optional(),
23+
inlineDiffs: z.boolean().optional(),
24+
invert: z.boolean().optional(),
25+
noHighlighting: z.boolean().optional(),
26+
reporter: z.string().optional(),
27+
reporterOptions: z.any().optional(),
28+
retries: z.number().optional(),
29+
slow: z.number().optional(),
30+
timeout: z.union([z.number(), z.string()]).optional(),
31+
ui: z
32+
.union([
33+
z.literal("bdd"),
34+
z.literal("tdd"),
35+
z.literal("qunit"),
36+
z.literal("exports"),
37+
])
38+
.optional(),
39+
parallel: z.boolean().optional(),
40+
jobs: z.number().optional(),
41+
rootHooks: z
42+
.object({
43+
afterAll: z.union([z.function(), z.array(z.function())]).optional(),
44+
beforeAll: z.union([z.function(), z.array(z.function())]).optional(),
45+
afterEach: z.union([z.function(), z.array(z.function())]).optional(),
46+
beforeEach: z.union([z.function(), z.array(z.function())]).optional(),
47+
})
48+
.optional(),
49+
require: z.array(z.string()).optional(),
50+
isWorker: z.boolean().optional(),
51+
});
52+
53+
const userConfigType = z.object({
54+
mocha: z.optional(mochaConfigType),
55+
});
56+
57+
export default async (): Promise<Partial<ConfigHooks>> => {
58+
const handlers: Partial<ConfigHooks> = {
59+
validateUserConfig: async (userConfig) => {
60+
return validateUserConfigZodType(userConfig, userConfigType);
61+
},
62+
resolveUserConfig: async (
63+
userConfig,
64+
resolveConfigurationVariable,
65+
next,
66+
) => {
67+
const resolvedConfig = await next(
68+
userConfig,
69+
resolveConfigurationVariable,
70+
);
71+
72+
return {
73+
...resolvedConfig,
74+
mocha: {
75+
timeout: 40000,
76+
...resolvedConfig.mocha,
77+
...userConfig.mocha,
78+
},
79+
};
80+
},
81+
};
82+
83+
return handlers;
84+
};

0 commit comments

Comments
 (0)