diff --git a/src/discoverer/evaluate.ts b/src/discoverer/evaluate.ts index d935c6a..871e394 100644 --- a/src/discoverer/evaluate.ts +++ b/src/discoverer/evaluate.ts @@ -14,6 +14,7 @@ import * as acornWalk from 'acorn-walk'; import * as errorParser from 'error-stack-parser'; import { CommonOptions, TsconfigRaw, transform as esbuildTransform } from 'esbuild'; import * as path from 'path'; +import { pathToFileURL } from 'url'; import * as vm from 'vm'; import * as vscode from 'vscode'; import { ConfigValue } from '../configValue'; @@ -196,6 +197,7 @@ export class EvaluationTestDiscoverer implements ITestDiscoverer { { __dirname: path.dirname(filePath), __filename: path.basename(filePath), + __import_meta_url: pathToFileURL(filePath).href, } as any, { get(target, prop) { @@ -315,6 +317,9 @@ export class EvaluationTestDiscoverer implements ITestDiscoverer { ...this.esbuildCommonOptions(filePath), sourcefile: filePath, // for auto-detection of the loader loader: 'default', // use the default loader + define: { + 'import.meta.url': '__import_meta_url', + }, }); code = result.code; diff --git a/src/test/integration/typescript-esm.test.ts b/src/test/integration/typescript-esm.test.ts new file mode 100644 index 0000000..21b58e3 --- /dev/null +++ b/src/test/integration/typescript-esm.test.ts @@ -0,0 +1,58 @@ +/** + * Copyright (C) Daniel Kuschny (Danielku15) and contributors. + * Copyright (C) Microsoft Corporation. All rights reserved. + * + * Use of this source code is governed by an MIT-style + * license that can be found in the LICENSE file or at + * https://opensource.org/licenses/MIT. + */ + +import { expect } from 'chai'; +import * as vscode from 'vscode'; +import { captureTestRun, expectTestTree, getController } from '../util'; + +describe('typescript-esm', () => { + it('discovers tests', async () => { + const c = await getController(); + + expectTestTree(c, [['test', [['hello.spec.ts', [['import-meta', [['dirname']]]]]]]]); + }); + + it('runs tests', async () => { + const c = await getController(); + const profiles = c.profiles; + expect(profiles).to.have.lengthOf(2); + + const run = await captureTestRun( + c, + new vscode.TestRunRequest( + undefined, + undefined, + profiles.find((p) => p.kind === vscode.TestRunProfileKind.Run), + ), + ); + + run.expectStates({ + 'test/hello.spec.ts/import-meta/dirname': ['enqueued', 'started', 'passed'], + }); + }); + + it('debugs tests', async () => { + const c = await getController(); + const profiles = c.profiles; + expect(profiles).to.have.lengthOf(2); + + const run = await captureTestRun( + c, + new vscode.TestRunRequest( + undefined, + undefined, + profiles.find((p) => p.kind === vscode.TestRunProfileKind.Debug), + ), + ); + + run.expectStates({ + 'test/hello.spec.ts/import-meta/dirname': ['enqueued', 'started', 'passed'], + }); + }); +}); diff --git a/test-workspaces/typescript-esm/.mocharc.json b/test-workspaces/typescript-esm/.mocharc.json new file mode 100644 index 0000000..de6d37e --- /dev/null +++ b/test-workspaces/typescript-esm/.mocharc.json @@ -0,0 +1,7 @@ +{ + "spec": ["**/test/**/*.spec.ts"], + "extensions": ["ts"], + "node-option": ["experimental-specifier-resolution=node", "import=tsx", "no-warnings"], + "ignore": ["**/node_modules/**", "**/dist/**"], + "timeout": "10000" +} diff --git a/test-workspaces/typescript-esm/package.json b/test-workspaces/typescript-esm/package.json new file mode 100644 index 0000000..2961e24 --- /dev/null +++ b/test-workspaces/typescript-esm/package.json @@ -0,0 +1,4 @@ +{ + "name": "typescript-esm", + "type": "module" +} diff --git a/test-workspaces/typescript-esm/test/hello.spec.ts b/test-workspaces/typescript-esm/test/hello.spec.ts new file mode 100644 index 0000000..c0894ae --- /dev/null +++ b/test-workspaces/typescript-esm/test/hello.spec.ts @@ -0,0 +1,10 @@ +import { strictEqual } from 'node:assert'; + +import { fileURLToPath, URL } from 'node:url'; + +const importMetaDirname = fileURLToPath(new URL('.', import.meta.url)); +describe('import-meta', () => { + it('dirname', async () => { + strictEqual(typeof importMetaDirname, 'string'); + }); +}); diff --git a/test-workspaces/typescript-esm/tsconfig.json b/test-workspaces/typescript-esm/tsconfig.json new file mode 100644 index 0000000..5cf1d91 --- /dev/null +++ b/test-workspaces/typescript-esm/tsconfig.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "strict": true, + "outDir": "dist", + "allowJs": true, + "checkJs": true, + "target": "ES2022", + "module": "ES2022" + } +}