Skip to content

Commit cba99ee

Browse files
committed
node test runner plugin
1 parent 4aa82c9 commit cba99ee

File tree

7 files changed

+57
-32
lines changed

7 files changed

+57
-32
lines changed

pnpm-lock.yaml

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

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
emptyTask,
66
configVariable,
77
globalOption,
8+
HardhatUserConfig,
89
} from "@ignored/hardhat-vnext/config";
910
import HardhatNodeTestRunner from "@ignored/hardhat-vnext-node-test-runner";
1011

@@ -93,7 +94,7 @@ const pluginExample = {
9394
],
9495
};
9596

96-
const config = {
97+
const config: HardhatUserConfig = {
9798
tasks: [
9899
exampleTaskOverride,
99100
testSolidityTask,

v-next/example-project/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,8 @@
2626
"@types/node": "^20.14.9",
2727
"prettier": "3.2.5",
2828
"typescript": "~5.5.0"
29+
},
30+
"dependencies": {
31+
"tsx": "^4.11.0"
2932
}
3033
}

v-next/example-project/test/example-test.js v-next/example-project/test/example-test.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import assert from "node:assert/strict";
22
import { describe, it } from "node:test";
33

4-
import hre from "@ignored/hardhat-vnext";
5-
64
describe("Example test", () => {
75
it("should work", () => {
86
assert.equal(1 + 1, 2);

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

+20
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,26 @@ Please add the property "type" with the value "module" in your package.json to e
196196
websiteTitle: "Invalid config",
197197
websiteDescription: `The configuration you provided is invalid. Please check the documentation to learn how to configure Hardhat correctly.`,
198198
},
199+
TYPESCRIPT_NOT_INSTALLED: {
200+
number: 17,
201+
messageTemplate: `Your Hardhat project uses typescript, but it's not installed.
202+
203+
Please run: npm install --save-dev typescript`,
204+
websiteTitle: "typescript not installed",
205+
websiteDescription: `You are running a Hardhat project that uses typescript, but it's not installed.
206+
207+
Please run this and try again: \`npm install --save-dev typescript\``,
208+
},
209+
TSX_NOT_INSTALLED: {
210+
number: 18,
211+
messageTemplate: `Your Hardhat project uses typescript, but tsx is not installed.
212+
213+
Please run: npm install --save-dev tsx`,
214+
websiteTitle: "tsx not installed",
215+
websiteDescription: `You are running a Hardhat project that uses typescript, but you haven't installed tsx.
216+
217+
Please run this and try again: \`npm install --save-dev tsx\``,
218+
},
199219
},
200220
INTERNAL: {
201221
ASSERTION_ERROR: {

v-next/hardhat-node-test-runner/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,6 @@
6060
"@ignored/hardhat-vnext-errors": "workspace:^3.0.0-next.2",
6161
"@ignored/hardhat-vnext-node-test-reporter": "workspace:^3.0.0-next.2",
6262
"@ignored/hardhat-vnext-utils": "workspace:^3.0.0-next.2",
63-
"ts-node": "^10.8.0"
63+
"tsx": "^4.11.0"
6464
}
6565
}

v-next/hardhat-node-test-runner/src/task-action.ts

+26-6
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import type { NewTaskActionFunction } from "@ignored/hardhat-vnext-core/types/ta
33

44
import { resolve } from "node:path";
55
import { run } from "node:test";
6-
import "tsx";
76

7+
import { HardhatError } from "@ignored/hardhat-vnext-errors";
88
import hardhatTestReporter from "@ignored/hardhat-vnext-node-test-reporter";
9-
import { getAllFilesMatching } from "@ignored/hardhat-vnext-utils/fs";
9+
import { findUp, getAllFilesMatching } from "@ignored/hardhat-vnext-utils/fs";
1010

1111
interface TestActionArguments {
1212
testFiles: string[];
@@ -39,16 +39,36 @@ async function getTestFiles(
3939
);
4040
}
4141

42+
async function hasTypescriptConfig(): Promise<boolean> {
43+
const hhConfig = await findUp("hardhat.config.ts");
44+
45+
return hhConfig !== undefined;
46+
}
47+
4248
const runScriptWithHardhat: NewTaskActionFunction<TestActionArguments> = async (
4349
{ testFiles, only },
4450
hre,
4551
) => {
4652
const files = await getTestFiles(testFiles, hre.config);
4753

48-
if (files.some((f) => isTypescriptFile(f))) {
49-
// @ts-expect-error -- typescript will complain about lack of type info for this import
50-
// but we don't need it, we just need to load the module to run typescript tests
51-
await import("ts-node/register/transpile-only");
54+
// the second check is needed for the case of a user having a hardhat.config.ts file
55+
// but all their test files are js files. probably an edge case, but we should handle it
56+
if (files.some((f) => isTypescriptFile(f)) || (await hasTypescriptConfig())) {
57+
try {
58+
import.meta.resolve("typescript");
59+
} catch {
60+
throw new HardhatError(
61+
HardhatError.ERRORS.GENERAL.TYPESCRIPT_NOT_INSTALLED,
62+
);
63+
}
64+
65+
try {
66+
import.meta.resolve("tsx");
67+
} catch {
68+
throw new HardhatError(HardhatError.ERRORS.GENERAL.TSX_NOT_INSTALLED);
69+
}
70+
71+
process.env.NODE_OPTIONS = "--import tsx";
5272
}
5373

5474
run({ files, only }).compose(hardhatTestReporter).pipe(process.stdout);

0 commit comments

Comments
 (0)