Skip to content

Commit f46ccff

Browse files
committed
Add support for ESM test files
1 parent 4bc6a27 commit f46ccff

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

.changeset/friendly-dryers-mix.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"hardhat": patch
3+
---
4+
5+
Added support for ESM test files

packages/hardhat-core/src/builtin-tasks/test.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ import { HARDHAT_NETWORK_NAME } from "../internal/constants";
77
import { subtask, task } from "../internal/core/config/config-env";
88
import { HardhatError } from "../internal/core/errors";
99
import { ERRORS } from "../internal/core/errors-list";
10-
import { isRunningWithTypescript } from "../internal/core/typescript-support";
10+
import {
11+
isJavascriptFile,
12+
isRunningWithTypescript,
13+
isTypescriptFile,
14+
} from "../internal/core/typescript-support";
1115
import { getForkCacheDirPath } from "../internal/hardhat-network/provider/utils/disk-cache";
1216
import { showForkRecommendationsBannerIfNecessary } from "../internal/hardhat-network/provider/utils/fork-recomendations-banner";
1317
import { pluralize } from "../internal/util/strings";
@@ -40,7 +44,7 @@ subtask(TASK_TEST_GET_TEST_FILES)
4044

4145
const jsFiles = await getAllFilesMatching(
4246
config.paths.tests,
43-
(f) => f.endsWith(".js") || f.endsWith(".cjs") || f.endsWith(".mjs")
47+
isJavascriptFile
4448
);
4549

4650
if (!isRunningWithTypescript(config)) {
@@ -49,7 +53,7 @@ subtask(TASK_TEST_GET_TEST_FILES)
4953

5054
const tsFiles = await getAllFilesMatching(
5155
config.paths.tests,
52-
(f) => f.endsWith(".ts") || f.endsWith(".cts")
56+
isTypescriptFile
5357
);
5458

5559
return [...jsFiles, ...tsFiles];

packages/hardhat-core/src/internal/core/typescript-support.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ let cachedIsTypescriptSupported: boolean | undefined;
1313
*/
1414
export function willRunWithTypescript(configPath?: string): boolean {
1515
const config = resolveConfigPath(configPath);
16-
return isTypescriptFile(config);
16+
return isNonEsmTypescriptFile(config);
1717
}
1818

1919
/**
2020
* Returns true if an Hardhat is already running with typescript.
2121
*/
2222
export function isRunningWithTypescript(config: HardhatConfig): boolean {
23-
return isTypescriptFile(config.paths.configFile);
23+
return isNonEsmTypescriptFile(config.paths.configFile);
2424
}
2525

2626
export function isTypescriptSupported() {
@@ -80,6 +80,14 @@ export function loadTsNode(
8080
require(tsNodeRequirement);
8181
}
8282

83-
function isTypescriptFile(path: string): boolean {
84-
return path.endsWith(".ts") || path.endsWith(".cts");
83+
function isNonEsmTypescriptFile(path: string): boolean {
84+
return /\.(ts|cts)$/i.test(path);
85+
}
86+
87+
export function isTypescriptFile(path: string): boolean {
88+
return /\.(ts|cts|mts)$/i.test(path);
89+
}
90+
91+
export function isJavascriptFile(path: string): boolean {
92+
return /\.(js|cjs|mjs)$/i.test(path);
8593
}

0 commit comments

Comments
 (0)