Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/discoverer/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
58 changes: 58 additions & 0 deletions src/test/integration/typescript-esm.test.ts
Original file line number Diff line number Diff line change
@@ -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'],
});
});
});
7 changes: 7 additions & 0 deletions test-workspaces/typescript-esm/.mocharc.json
Original file line number Diff line number Diff line change
@@ -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"
}
4 changes: 4 additions & 0 deletions test-workspaces/typescript-esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "typescript-esm",
"type": "module"
}
10 changes: 10 additions & 0 deletions test-workspaces/typescript-esm/test/hello.spec.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
10 changes: 10 additions & 0 deletions test-workspaces/typescript-esm/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"strict": true,
"outDir": "dist",
"allowJs": true,
"checkJs": true,
"target": "ES2022",
"module": "ES2022"
}
}
Loading