UNPKG

2.96 kBPlain TextView Raw
1import chalk from "chalk";
2import path from "path";
3
4import { BUIDLEREVM_NETWORK_NAME } from "../internal/constants";
5import { internalTask, task } from "../internal/core/config/config-env";
6import { isTypescriptSupported } from "../internal/core/typescript-support";
7import { glob } from "../internal/util/glob";
8import { pluralize } from "../internal/util/strings";
9
10import {
11 TASK_COMPILE,
12 TASK_TEST,
13 TASK_TEST_GET_TEST_FILES,
14 TASK_TEST_RUN_MOCHA_TESTS,
15 TASK_TEST_SETUP_TEST_ENVIRONMENT,
16} from "./task-names";
17
18export default function () {
19 internalTask(TASK_TEST_GET_TEST_FILES)
20 .addOptionalVariadicPositionalParam(
21 "testFiles",
22 "An optional list of files to test",
23 []
24 )
25 .setAction(async ({ testFiles }: { testFiles: string[] }, { config }) => {
26 if (testFiles.length !== 0) {
27 return testFiles;
28 }
29
30 const jsFiles = await glob(path.join(config.paths.tests, "**/*.js"));
31
32 if (!isTypescriptSupported()) {
33 return jsFiles;
34 }
35
36 const tsFiles = await glob(path.join(config.paths.tests, "**/*.ts"));
37
38 return [...jsFiles, ...tsFiles];
39 });
40
41 internalTask(TASK_TEST_SETUP_TEST_ENVIRONMENT, async () => {});
42
43 internalTask(TASK_TEST_RUN_MOCHA_TESTS)
44 .addOptionalVariadicPositionalParam(
45 "testFiles",
46 "An optional list of files to test",
47 []
48 )
49 .setAction(async ({ testFiles }: { testFiles: string[] }, { config }) => {
50 const { default: Mocha } = await import("mocha");
51 const mocha = new Mocha(config.mocha);
52 testFiles.forEach((file) => mocha.addFile(file));
53
54 const runPromise = new Promise<number>((resolve, _) => {
55 mocha.run(resolve);
56 });
57
58 process.exitCode = await runPromise;
59 });
60
61 task(TASK_TEST, "Runs mocha tests")
62 .addOptionalVariadicPositionalParam(
63 "testFiles",
64 "An optional list of files to test",
65 []
66 )
67 .addFlag("noCompile", "Don't compile before running this task")
68 .setAction(
69 async (
70 {
71 testFiles,
72 noCompile,
73 }: {
74 testFiles: string[];
75 noCompile: boolean;
76 },
77 { run, network }
78 ) => {
79 if (!noCompile) {
80 await run(TASK_COMPILE);
81 }
82
83 const files = await run(TASK_TEST_GET_TEST_FILES, { testFiles });
84 await run(TASK_TEST_SETUP_TEST_ENVIRONMENT);
85 await run(TASK_TEST_RUN_MOCHA_TESTS, { testFiles: files });
86
87 if (network.name !== BUIDLEREVM_NETWORK_NAME) {
88 return;
89 }
90
91 const failures = await network.provider.send(
92 "buidler_getStackTraceFailuresCount"
93 );
94
95 if (failures === 0) {
96 return;
97 }
98
99 console.warn(
100 chalk.yellow(
101 `Failed to generate ${failures} ${pluralize(
102 failures,
103 "stack trace"
104 )}. Run Buidler with --verbose to learn more.`
105 )
106 );
107 }
108 );
109}
110
\No newline at end of file