Skip to content

Commit f506960

Browse files
authored
fix: mock import.meta.url when evaluating esm files (#216)
1 parent 4d8a2cd commit f506960

File tree

6 files changed

+94
-0
lines changed

6 files changed

+94
-0
lines changed

src/discoverer/evaluate.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import * as acornWalk from 'acorn-walk';
1414
import * as errorParser from 'error-stack-parser';
1515
import { CommonOptions, TsconfigRaw, transform as esbuildTransform } from 'esbuild';
1616
import * as path from 'path';
17+
import { pathToFileURL } from 'url';
1718
import * as vm from 'vm';
1819
import * as vscode from 'vscode';
1920
import { ConfigValue } from '../configValue';
@@ -196,6 +197,7 @@ export class EvaluationTestDiscoverer implements ITestDiscoverer {
196197
{
197198
__dirname: path.dirname(filePath),
198199
__filename: path.basename(filePath),
200+
__import_meta_url: pathToFileURL(filePath).href,
199201
} as any,
200202
{
201203
get(target, prop) {
@@ -315,6 +317,9 @@ export class EvaluationTestDiscoverer implements ITestDiscoverer {
315317
...this.esbuildCommonOptions(filePath),
316318
sourcefile: filePath, // for auto-detection of the loader
317319
loader: 'default', // use the default loader
320+
define: {
321+
'import.meta.url': '__import_meta_url',
322+
},
318323
});
319324

320325
code = result.code;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Copyright (C) Daniel Kuschny (Danielku15) and contributors.
3+
* Copyright (C) Microsoft Corporation. All rights reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style
6+
* license that can be found in the LICENSE file or at
7+
* https://opensource.org/licenses/MIT.
8+
*/
9+
10+
import { expect } from 'chai';
11+
import * as vscode from 'vscode';
12+
import { captureTestRun, expectTestTree, getController } from '../util';
13+
14+
describe('typescript-esm', () => {
15+
it('discovers tests', async () => {
16+
const c = await getController();
17+
18+
expectTestTree(c, [['test', [['hello.spec.ts', [['import-meta', [['dirname']]]]]]]]);
19+
});
20+
21+
it('runs tests', async () => {
22+
const c = await getController();
23+
const profiles = c.profiles;
24+
expect(profiles).to.have.lengthOf(2);
25+
26+
const run = await captureTestRun(
27+
c,
28+
new vscode.TestRunRequest(
29+
undefined,
30+
undefined,
31+
profiles.find((p) => p.kind === vscode.TestRunProfileKind.Run),
32+
),
33+
);
34+
35+
run.expectStates({
36+
'test/hello.spec.ts/import-meta/dirname': ['enqueued', 'started', 'passed'],
37+
});
38+
});
39+
40+
it('debugs tests', async () => {
41+
const c = await getController();
42+
const profiles = c.profiles;
43+
expect(profiles).to.have.lengthOf(2);
44+
45+
const run = await captureTestRun(
46+
c,
47+
new vscode.TestRunRequest(
48+
undefined,
49+
undefined,
50+
profiles.find((p) => p.kind === vscode.TestRunProfileKind.Debug),
51+
),
52+
);
53+
54+
run.expectStates({
55+
'test/hello.spec.ts/import-meta/dirname': ['enqueued', 'started', 'passed'],
56+
});
57+
});
58+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"spec": ["**/test/**/*.spec.ts"],
3+
"extensions": ["ts"],
4+
"node-option": ["experimental-specifier-resolution=node", "import=tsx", "no-warnings"],
5+
"ignore": ["**/node_modules/**", "**/dist/**"],
6+
"timeout": "10000"
7+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "typescript-esm",
3+
"type": "module"
4+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { strictEqual } from 'node:assert';
2+
3+
import { fileURLToPath, URL } from 'node:url';
4+
5+
const importMetaDirname = fileURLToPath(new URL('.', import.meta.url));
6+
describe('import-meta', () => {
7+
it('dirname', async () => {
8+
strictEqual(typeof importMetaDirname, 'string');
9+
});
10+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"compilerOptions": {
3+
"strict": true,
4+
"outDir": "dist",
5+
"allowJs": true,
6+
"checkJs": true,
7+
"target": "ES2022",
8+
"module": "ES2022"
9+
}
10+
}

0 commit comments

Comments
 (0)