Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocha test plugin #5598

Merged
merged 9 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 85 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 14 additions & 2 deletions v-next/example-project/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import type { HardhatUserConfig } from "@ignored/hardhat-vnext/types/config";

import { HardhatPluginError } from "@ignored/hardhat-vnext/plugins";

import {
task,
emptyTask,
configVariable,
globalOption,
HardhatUserConfig,
} from "@ignored/hardhat-vnext/config";
import HardhatNodeTestRunner from "@ignored/hardhat-vnext-node-test-runner";
import HardhatMochaTestRunner from "@ignored/hardhat-vnext-mocha-test-runner";

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

Expand Down Expand Up @@ -102,8 +104,18 @@ const config: HardhatUserConfig = {
exampleEmptySubtask,
greeting,
],
plugins: [pluginExample, HardhatNodeTestRunner],
plugins: [
pluginExample,
HardhatMochaTestRunner,
// if testing node plugin, use the following line instead
// HardhatNodeTestRunner,
],
privateKey: configVariable("privateKey"),
paths: {
tests: "test/mocha",
// if testing node plugin, use the following line instead
// tests: "test/node",
},
};

export default config;
6 changes: 3 additions & 3 deletions v-next/example-project/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
},
"devDependencies": {
"@ignored/hardhat-vnext": "workspace:^3.0.0-next.3",
"@ignored/hardhat-vnext-mocha-test-runner": "workspace:^3.0.0-next.2",
"@ignored/hardhat-vnext-node-test-runner": "workspace:^3.0.0-next.2",
"@types/mocha": ">=9.1.0",
"@types/node": "^20.14.9",
"mocha": "^10.0.0",
"prettier": "3.2.5",
"typescript": "~5.5.0"
},
"dependencies": {
"tsx": "^4.11.0"
}
}
8 changes: 8 additions & 0 deletions v-next/example-project/test/mocha/mocha-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import assert from "node:assert/strict";
import { describe, it } from "mocha";

describe("Mocha test", () => {
it("should work", () => {
assert.equal(1 + 1, 2);
});
});
10 changes: 10 additions & 0 deletions v-next/example-project/test/mocha/other-mocha-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import assert from "node:assert/strict";
import { describe, it } from "mocha";

import hre from "@ignored/hardhat-vnext";

describe("Other mocha test", () => {
it("should have the example task", () => {
assert.ok(hre.tasks.getTask("example"));
});
});
6 changes: 6 additions & 0 deletions v-next/example-project/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
"references": [
{
"path": "../hardhat"
},
{
"path": "../hardhat-node-test-runner"
},
{
"path": "../hardhat-mocha-test-runner"
}
]
}
7 changes: 7 additions & 0 deletions v-next/hardhat-errors/src/descriptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,13 @@ Please double check your script's path.`,
Please check Hardhat's output for more details.`,
},
TEST_TASK_ESM_TESTS_RUN_TWICE: {
number: 602,
messageTemplate: `Your project uses ESM and you've programmatically run your tests twice. This is not supported yet.`,
websiteTitle: "Running tests twice in an ESM project",
websiteDescription:
'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).',
},
},
NETWORK: {
INVALID_URL: {
Expand Down
3 changes: 3 additions & 0 deletions v-next/hardhat-mocha-test-runner/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { createConfig } = require("../../config-v-next/eslint.cjs");

module.exports = createConfig(__filename);
5 changes: 5 additions & 0 deletions v-next/hardhat-mocha-test-runner/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Node modules
/node_modules

# Compilation output
/dist
3 changes: 3 additions & 0 deletions v-next/hardhat-mocha-test-runner/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/node_modules
/dist
CHANGELOG.md
9 changes: 9 additions & 0 deletions v-next/hardhat-mocha-test-runner/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MIT License

Copyright (c) 2024 Nomic Foundation

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:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

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.
3 changes: 3 additions & 0 deletions v-next/hardhat-mocha-test-runner/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Hardhat's `mocha` test runner

This package includes Hardhat's `mocha` test runner.
69 changes: 69 additions & 0 deletions v-next/hardhat-mocha-test-runner/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"name": "@ignored/hardhat-vnext-mocha-test-runner",
"version": "3.0.0-next.2",
"description": "A mocha test runner",
"homepage": "https://github.com/nomicfoundation/hardhat/tree/v-next/v-next/hardhat-mocha-test-runner",
"repository": {
"type": "git",
"url": "https://github.com/NomicFoundation/hardhat",
"directory": "v-next/hardhat-mocha-test-runner"
},
"author": "Nomic Foundation",
"license": "MIT",
"type": "module",
"exports": {
".": "./dist/src/index.js"
},
"keywords": [
"ethereum",
"smart-contracts",
"hardhat"
],
"scripts": {
"lint": "pnpm prettier --check && pnpm eslint",
"lint:fix": "pnpm prettier --write && pnpm eslint --fix",
"eslint": "eslint \"src/**/*.ts\" \"test/**/*.ts\"",
"prettier": "prettier \"**/*.{ts,js,md,json}\"",
"test": "node --import tsx/esm --test --test-reporter=@ignored/hardhat-vnext-node-test-reporter \"test/*.ts\" \"test/!(fixture-projects|helpers)/**/*.ts\"",
"test:only": "node --import tsx/esm --test --test-only --test-reporter=@ignored/hardhat-vnext-node-test-reporter \"test/*.ts\" \"test/!(fixture-projects|helpers)/**/*.ts\"",
"pretest": "pnpm build",
"build": "tsc --build .",
"prepublishOnly": "pnpm build",
"clean": "rimraf dist"
},
"files": [
"dist/src/",
"src/",
"CHANGELOG.md",
"LICENSE",
"README.md"
],
"devDependencies": {
"@eslint-community/eslint-plugin-eslint-comments": "^4.3.0",
"@ignored/hardhat-vnext-node-test-reporter": "workspace:^3.0.0-next.2",
"@nomicfoundation/hardhat-test-utils": "workspace:^",
"@types/mocha": ">=9.1.0",
"@types/node": "^20.14.9",
"@typescript-eslint/eslint-plugin": "^7.7.1",
"@typescript-eslint/parser": "^7.7.1",
"eslint": "8.57.0",
"eslint-config-prettier": "9.1.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "2.29.1",
"eslint-plugin-no-only-tests": "3.1.0",
"expect-type": "^0.19.0",
"prettier": "3.2.5",
"rimraf": "^5.0.5",
"typescript": "~5.5.0",
"typescript-eslint": "7.7.1"
},
"dependencies": {
"@ignored/hardhat-vnext": "workspace:^3.0.0-next.2",
"@ignored/hardhat-vnext-errors": "workspace:^3.0.0-next.2",
"@ignored/hardhat-vnext-utils": "workspace:^3.0.0-next.2",
"@ignored/hardhat-vnext-zod-utils": "workspace:^3.0.0-next.2",
"mocha": "^10.0.0",
"tsx": "^4.11.0",
"zod": "^3.23.8"
}
}
84 changes: 84 additions & 0 deletions v-next/hardhat-mocha-test-runner/src/hookHandlers/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import type { ConfigHooks } from "@ignored/hardhat-vnext/types/hooks";

import { validateUserConfigZodType } from "@ignored/hardhat-vnext-zod-utils";
import { z } from "zod";

const mochaConfigType = z.object({
allowUncaught: z.boolean().optional(),
asyncOnly: z.boolean().optional(),
bail: z.boolean().optional(),
checkLeaks: z.boolean().optional(),
color: z.boolean().optional(),
delay: z.boolean().optional(),
diff: z.boolean().optional(),
dryRun: z.boolean().optional(),
failZero: z.boolean().optional(),
fgrep: z.string().optional(),
forbidOnly: z.boolean().optional(),
forbidPending: z.boolean().optional(),
fullTrace: z.boolean().optional(),
globals: z.array(z.string()).optional(),
grep: z.string().optional(),
growl: z.boolean().optional(),
inlineDiffs: z.boolean().optional(),
invert: z.boolean().optional(),
noHighlighting: z.boolean().optional(),
reporter: z.string().optional(),
reporterOptions: z.any().optional(),
retries: z.number().optional(),
slow: z.number().optional(),
timeout: z.union([z.number(), z.string()]).optional(),
ui: z
.union([
z.literal("bdd"),
z.literal("tdd"),
z.literal("qunit"),
z.literal("exports"),
])
.optional(),
parallel: z.boolean().optional(),
jobs: z.number().optional(),
rootHooks: z
.object({
afterAll: z.union([z.function(), z.array(z.function())]).optional(),
beforeAll: z.union([z.function(), z.array(z.function())]).optional(),
afterEach: z.union([z.function(), z.array(z.function())]).optional(),
beforeEach: z.union([z.function(), z.array(z.function())]).optional(),
})
.optional(),
require: z.array(z.string()).optional(),
isWorker: z.boolean().optional(),
});

const userConfigType = z.object({
mocha: z.optional(mochaConfigType),
});

export default async (): Promise<Partial<ConfigHooks>> => {
const handlers: Partial<ConfigHooks> = {
validateUserConfig: async (userConfig) => {
return validateUserConfigZodType(userConfig, userConfigType);
},
resolveUserConfig: async (
userConfig,
resolveConfigurationVariable,
next,
) => {
const resolvedConfig = await next(
userConfig,
resolveConfigurationVariable,
);

return {
...resolvedConfig,
mocha: {
timeout: 40000,
...resolvedConfig.mocha,
...userConfig.mocha,
},
};
},
};

return handlers;
};
Loading