Skip to content

Commit 4dd0241

Browse files
committed
Initial commit of the test-reporter
1 parent 399e449 commit 4dd0241

18 files changed

+1456
-9
lines changed

pnpm-lock.yaml

+518-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
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, ["src/reporter.ts"]);
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
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,5 @@
1+
# Hardhat's `node:test` reporter
2+
3+
This package includes Hardhat's `node:test` reporter.
4+
5+
This project is heavily inspired by https://github.com/voxpelli/node-test-pretty-reporter
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Integration tests
2+
3+
This folder contains integration tests for the reporter. They don't use `node:test` as driver of the test runs, as you can't run `node:test` within `node:test`.
4+
5+
Instead, the script `index.ts` runs all the tests in each fixture folder, comparing the reporter's results with `result.txt`.
6+
7+
## Running each test manually
8+
9+
You can run each of the fixture test manually from the package root by building it and running `node --import tsx/esm --test --test-reporter=./dist/src/reporter.js integration-tests/fixture-tests/example-test/*.ts`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
1
2+
2
3+
Foooo
4+
✔ test
5+
1) test with cause
6+
child
7+
2) asdasd
8+
9+
a
10+
aa
11+
aaa
12+
✔ aaaa
13+
14+
top level test
15+
✔ bar
16+
17+
✔ top level test
18+
19+
in describe
20+
3) foo
21+
22+
testing before each
23+
nested
24+
✔ neseted foo
25+
✔ neseted foo 2
26+
27+
28+
+ todo test
29+
30+
- skipped test
31+
32+
33+
6 passing (104ms)
34+
2 failing
35+
1 skipped
36+
1 todo
37+
1 cancelled
38+
39+
1) Foooo
40+
test with cause:
41+
42+
Error: withCause
43+
 at TestContext.<anonymous> (integration-tests/fixture-tests/example-test/test.ts:1:136)
44+
 at Test.runInAsyncScope (node:async_hooks:206:9)
45+
 ... 6 lines matching cause stack trace ...
46+
 at async Test.processPendingSubtests (node:internal/test_runner/test:533:7) {
47+
 [cause]: Error: cause
48+
 at TestContext.<anonymous> (integration-tests/fixture-tests/example-test/test.ts:1:165)
49+
 at Test.runInAsyncScope (node:async_hooks:206:9)
50+
 at Test.run (node:internal/test_runner/test:824:25)
51+
 at Suite.processPendingSubtests (node:internal/test_runner/test:533:18)
52+
 at Test.postRun (node:internal/test_runner/test:923:19)
53+
 at Test.run (node:internal/test_runner/test:866:12)
54+
 at async Promise.all (index 0)
55+
 at async Suite.run (node:internal/test_runner/test:1183:7)
56+
 at async Test.processPendingSubtests (node:internal/test_runner/test:533:7)
57+
}
58+
59+
2) Foooo
60+
child
61+
asdasd:
62+
63+
Error: Different arrays
64+
- Expected
65+
+ Received
66+
67+
 Array [
68+
 1,
69+
- 2,
70+
+ 3,
71+
 3,
72+
 ]
73+
74+
 at TestContext.<anonymous> (integration-tests/fixture-tests/example-test/test.ts:1:239)
75+
 at Test.runInAsyncScope (node:async_hooks:206:9)
76+
 ... 7 lines matching cause stack trace ...
77+
 at Array.map (<anonymous>) {
78+
 [cause]: Error: cause
79+
 at TestContext.<anonymous> (integration-tests/fixture-tests/example-test/test.ts:1:275)
80+
 at Test.runInAsyncScope (node:async_hooks:206:9)
81+
 at Test.run (node:internal/test_runner/test:824:25)
82+
 at Test.start (node:internal/test_runner/test:721:17)
83+
 at node:internal/test_runner/test:1181:71
84+
 at node:internal/per_context/primordials:488:82
85+
 at new Promise (<anonymous>)
86+
 at new SafePromise (node:internal/per_context/primordials:456:29)
87+
 at node:internal/per_context/primordials:488:9
88+
 at Array.map (<anonymous>)
89+
}
90+
91+
3) in describe
92+
foo:
93+
94+
Test cancelled by parent error
95+
 This test was cancelled due to an error in its parent suite/it or test/it, or in one of its before/beforeEach
96+
97+
4) in describe:
98+
99+
Error: before
100+
 at SuiteContext.<anonymous> (integration-tests/fixture-tests/example-test/test.ts:1:696)
101+
 at TestHook.runInAsyncScope (node:async_hooks:206:9)
102+
 at TestHook.run (node:internal/test_runner/test:824:25)
103+
 at TestHook.run (node:internal/test_runner/test:1073:18)
104+
 at TestHook.run (node:internal/util:528:20)
105+
 at node:internal/test_runner/test:744:20
106+
 at async Suite.runHook (node:internal/test_runner/test:742:7)
107+
 at async Suite.run (node:internal/test_runner/test:1177:7)
108+
 at async Test.processPendingSubtests (node:internal/test_runner/test:533:7)
109+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { before, beforeEach, describe, it } from "node:test";
2+
3+
describe("Foooo", () => {
4+
it("test", async () => {});
5+
6+
it("test with cause", async () => {
7+
throw new Error("withCause", { cause: new Error("cause") });
8+
});
9+
10+
describe("child", () => {
11+
it("asdasd", () => {
12+
const error = new Error("Different arrays", {
13+
cause: new Error("cause"),
14+
});
15+
16+
Object.defineProperty(error, "expected", {
17+
configurable: false,
18+
enumerable: false,
19+
get() {
20+
return [1, 2, 3];
21+
},
22+
});
23+
24+
Object.defineProperty(error, "actual", {
25+
configurable: false,
26+
enumerable: false,
27+
get() {
28+
return [1, 3, 3];
29+
},
30+
});
31+
32+
throw error;
33+
});
34+
});
35+
});
36+
37+
describe("a", () => {
38+
describe("aa", () => {
39+
describe("aaa", () => {
40+
it("aaaa", () => {});
41+
});
42+
});
43+
});
44+
45+
it("top level test", async (t) => {
46+
await t.test("bar", () => {});
47+
});
48+
49+
describe("in describe", () => {
50+
before(() => {
51+
throw new Error("before");
52+
});
53+
54+
beforeEach(() => {
55+
throw new Error("before each");
56+
});
57+
58+
it("foo", async (t) => {
59+
await t.test("foo/bar", () => {});
60+
61+
throw new Error("asd");
62+
});
63+
});
64+
65+
describe("testing before each", () => {
66+
describe("nested", () => {
67+
it("neseted foo", async () => {
68+
console.log("1");
69+
});
70+
71+
it("neseted foo 2", async () => {
72+
console.log("2");
73+
});
74+
});
75+
});
76+
77+
it.todo("todo test", async () => {});
78+
79+
it.skip("skipped test", async () => {});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import {
2+
isDirectory,
3+
readdir,
4+
getAllFilesMatching,
5+
readUtf8File,
6+
} from "@nomicfoundation/hardhat-utils/fs";
7+
import { run } from "node:test";
8+
9+
import reporter from "../src/reporter.js";
10+
import { diff } from "jest-diff";
11+
12+
const SHOW_OUTPUT = process.argv.includes("--show-output");
13+
14+
for (let entry of await readdir(import.meta.dirname + "/fixture-tests")) {
15+
const entryPath = import.meta.dirname + "/fixture-tests/" + entry;
16+
if (await isDirectory(entryPath)) {
17+
console.log("Running integration test: " + entry);
18+
19+
const testFiles = await getAllFilesMatching(entryPath, (file) =>
20+
file.endsWith(".ts"),
21+
);
22+
23+
const outputChunks = [];
24+
25+
const reporterStream = run({
26+
files: testFiles,
27+
}).compose(reporter);
28+
29+
for await (const chunk of reporterStream) {
30+
outputChunks.push(chunk);
31+
}
32+
33+
const output = outputChunks.join("");
34+
const expectedOutput = await readUtf8File(entryPath + "/result.txt");
35+
36+
const normalizedOutput = normalizeOutputs(output);
37+
const normalizedExpectedOutput = normalizeOutputs(expectedOutput);
38+
39+
if (normalizedOutput !== normalizedExpectedOutput) {
40+
console.log("Normalized outputs differ:");
41+
console.log(diff(normalizedExpectedOutput, normalizedOutput));
42+
process.exitCode = 1;
43+
} else {
44+
console.log(" Passed");
45+
}
46+
47+
if (SHOW_OUTPUT) {
48+
console.log();
49+
console.log();
50+
console.log(output);
51+
}
52+
53+
console.log();
54+
console.log();
55+
console.log();
56+
console.log();
57+
}
58+
}
59+
60+
function normalizeOutputs(output: string): string {
61+
return output.replace(/\(\d+ms\)/, "(Xms)");
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"name": "@nomicfoundation/hardhat-node-test-reporter",
3+
"version": "3.0.0",
4+
"description": "A node:test reporter",
5+
"homepage": "https://github.com/nomicfoundation/hardhat/tree/v-next/v-next/hardhat-node-test-reporter",
6+
"repository": "github:nomicfoundation/hardhat",
7+
"author": "Nomic Foundation",
8+
"license": "MIT",
9+
"type": "module",
10+
"exports": {
11+
".": "./dist/src/reporter.js"
12+
},
13+
"keywords": [
14+
"ethereum",
15+
"smart-contracts",
16+
"hardhat"
17+
],
18+
"scripts": {
19+
"lint": "pnpm prettier --check && pnpm eslint",
20+
"lint:fix": "pnpm prettier --write && pnpm eslint --fix",
21+
"eslint": "eslint \"src/**/*.ts\" \"test/**/*.ts\"",
22+
"prettier": "prettier \"**/*.{ts,js,md,json}\"",
23+
"test": "glob --cmd=\"node --import tsx/esm --test\" \"test/**/*.ts\"",
24+
"test:github": "glob --cmd=\"node --import tsx/esm --test --test-reporter=@reporters/github --test-reporter-destination=stdout --test-reporter=spec --test-reporter-destination=stdout\" \"test/**/*.ts\"",
25+
"test:integration": "node --import tsx/esm integration-tests/index.ts",
26+
"posttest": "pnpm test:integration",
27+
"posttest:github": "pnpm test:integration",
28+
"pretest": "pnpm build",
29+
"build": "tsc --build .",
30+
"prepublishOnly": "pnpm build",
31+
"clean": "rimraf dist"
32+
},
33+
"files": [
34+
"dist/src/",
35+
"src/",
36+
"CHANGELOG.md",
37+
"LICENSE",
38+
"README.md"
39+
],
40+
"devDependencies": {
41+
"@nomicfoundation/eslint-plugin-hardhat-internal-rules": "workspace:^",
42+
"@nomicfoundation/eslint-plugin-slow-imports": "workspace:^",
43+
"@nomicfoundation/hardhat-utils": "workspace:^3.0.0",
44+
"@reporters/github": "^1.7.0",
45+
"@types/node": "^20.0.0",
46+
"@typescript-eslint/eslint-plugin": "^7.7.1",
47+
"@typescript-eslint/parser": "^7.7.1",
48+
"@voxpelli/node-test-pretty-reporter": "^1.1.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+
"glob": "^10.3.12",
56+
"prettier": "3.2.5",
57+
"rimraf": "^5.0.5",
58+
"tsx": "^4.7.1",
59+
"typescript": "~5.4.0",
60+
"typescript-eslint": "7.7.1"
61+
},
62+
"dependencies": {
63+
"chalk": "^5.3.0",
64+
"jest-diff": "^29.7.0"
65+
}
66+
}

0 commit comments

Comments
 (0)