-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
test.ts
128 lines (106 loc) · 3.5 KB
/
test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import chalk from "chalk";
import path from "path";
import { HARDHAT_NETWORK_NAME } from "../internal/constants";
import { subtask, task } from "../internal/core/config/config-env";
import { isRunningWithTypescript } from "../internal/core/typescript-support";
import { getForkCacheDirPath } from "../internal/hardhat-network/provider/utils/disk-cache";
import { showForkRecommendationsBannerIfNecessary } from "../internal/hardhat-network/provider/utils/fork-recomendations-banner";
import { glob } from "../internal/util/glob";
import { pluralize } from "../internal/util/strings";
import {
TASK_COMPILE,
TASK_TEST,
TASK_TEST_GET_TEST_FILES,
TASK_TEST_RUN_MOCHA_TESTS,
TASK_TEST_RUN_SHOW_FORK_RECOMMENDATIONS,
TASK_TEST_SETUP_TEST_ENVIRONMENT,
} from "./task-names";
subtask(TASK_TEST_GET_TEST_FILES)
.addOptionalVariadicPositionalParam(
"testFiles",
"An optional list of files to test",
[]
)
.setAction(async ({ testFiles }: { testFiles: string[] }, { config }) => {
if (testFiles.length !== 0) {
return testFiles;
}
const jsFiles = await glob(path.join(config.paths.tests, "**/*.js"));
if (!isRunningWithTypescript(config)) {
return jsFiles;
}
const tsFiles = await glob(path.join(config.paths.tests, "**/*.ts"));
return [...jsFiles, ...tsFiles];
});
subtask(TASK_TEST_SETUP_TEST_ENVIRONMENT, async () => {});
subtask(TASK_TEST_RUN_MOCHA_TESTS)
.addOptionalVariadicPositionalParam(
"testFiles",
"An optional list of files to test",
[]
)
.setAction(async ({ testFiles }: { testFiles: string[] }, { config }) => {
const { default: Mocha } = await import("mocha");
const mocha = new Mocha(config.mocha);
testFiles.forEach((file) => mocha.addFile(file));
const testFailures = await new Promise<number>((resolve) => {
mocha.run(resolve);
});
mocha.dispose();
return testFailures;
});
subtask(TASK_TEST_RUN_SHOW_FORK_RECOMMENDATIONS).setAction(
async (_, { config, network }) => {
if (network.name !== HARDHAT_NETWORK_NAME) {
return;
}
const forkCache = getForkCacheDirPath(config.paths);
await showForkRecommendationsBannerIfNecessary(network.config, forkCache);
}
);
task(TASK_TEST, "Runs mocha tests")
.addOptionalVariadicPositionalParam(
"testFiles",
"An optional list of files to test",
[]
)
.addFlag("noCompile", "Don't compile before running this task")
.setAction(
async (
{
testFiles,
noCompile,
}: {
testFiles: string[];
noCompile: boolean;
},
{ run, network }
) => {
if (!noCompile) {
await run(TASK_COMPILE, { quiet: true });
}
const files = await run(TASK_TEST_GET_TEST_FILES, { testFiles });
await run(TASK_TEST_SETUP_TEST_ENVIRONMENT);
await run(TASK_TEST_RUN_SHOW_FORK_RECOMMENDATIONS);
const testFailures = await run(TASK_TEST_RUN_MOCHA_TESTS, {
testFiles: files,
});
if (network.name === HARDHAT_NETWORK_NAME) {
const stackTracesFailures = await network.provider.send(
"hardhat_getStackTraceFailuresCount"
);
if (stackTracesFailures !== 0) {
console.warn(
chalk.yellow(
`Failed to generate ${stackTracesFailures} ${pluralize(
stackTracesFailures,
"stack trace"
)}. Run Hardhat with --verbose to learn more.`
)
);
}
}
process.exitCode = testFailures;
return testFailures;
}
);