Skip to content

Commit

Permalink
fix: Remove top level support (currently not possible)
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielku15 committed Apr 2, 2024
1 parent 6354d4a commit 63032d1
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 25 deletions.
16 changes: 2 additions & 14 deletions src/discoverer/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ import { IExtensionSettings, IParsedNode, ITestDiscoverer, NodeKind } from './ty
*/

export class EvaluationTestDiscoverer implements ITestDiscoverer {
private static readonly asyncEntryStart = '(async function(){';
private static readonly asyncEntryEnd = '})()';
constructor(
private logChannel: vscode.LogOutputChannel | undefined,
private symbols: IExtensionSettings,
Expand Down Expand Up @@ -122,14 +120,6 @@ export class EvaluationTestDiscoverer implements ITestDiscoverer {
startLine--;
endLine--;

// top level await wrapper begin
if (startLine === 0) {
startColumn -= EvaluationTestDiscoverer.asyncEntryStart.length;
}
if (endLine === 0) {
endColumn -= EvaluationTestDiscoverer.asyncEntryStart.length;
}

if (endLine === startLine) {
endColumn = Number.MAX_SAFE_INTEGER; // assume it takes the entire line of a single-line test case
} else {
Expand Down Expand Up @@ -169,13 +159,11 @@ export class EvaluationTestDiscoverer implements ITestDiscoverer {
}

let sourceMap: TraceMap | undefined;
const needsTranspile = isTypeScript(filePath) || isEsm(filePath, code);
// transpile typescript or ESM via esbuild if needed
if (isTypeScript(filePath) || isEsm(filePath, code)) {
if (needsTranspile) {
const tsconfig = this.tsconfigStore.getTsconfig(filePath);

// NOTE: CJS does not support top level awaits, we add a wrapper for that.
code = `${EvaluationTestDiscoverer.asyncEntryStart}${code}${EvaluationTestDiscoverer.asyncEntryEnd}`;

const result = await esbuildTransform(code, {
target: `node${process.versions.node.split('.')[0]}`, // target current runtime
sourcemap: true, // need source map for correct test positions
Expand Down
18 changes: 18 additions & 0 deletions src/test/integration/typescript-top-level-await.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* 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 { expectTestTree, getController } from '../util';

describe('typescript top level await', () => {
it('discovers tests fails', async () => {
const c = await getController();

await expectTestTree(c, []);
});
});
16 changes: 8 additions & 8 deletions src/test/unit/extract/syntax.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ describe('syntax', () => {
return discoverer.discover('test.js', source(...lines));
}

it('extracts basic suite', () => {
const src = extractWithAst(
it('extracts basic suite', async () => {
const src = await extractWithAst(
"suite('hello', () => {", //
" it('works', () => {});",
'})',
Expand Down Expand Up @@ -49,8 +49,8 @@ describe('syntax', () => {
]);
});

it('works with skip/only', () => {
const src = extractWithAst(
it('works with skip/only', async () => {
const src = await extractWithAst(
"suite('hello', () => {", //
" it.only('a', ()=>{});",
" it.skip('a', ()=>{});",
Expand Down Expand Up @@ -90,8 +90,8 @@ describe('syntax', () => {
]);
});

it('can detect suite but not dynamic tests', () => {
const src = extractWithAst(
it('can detect suite but not dynamic tests', async () => {
const src = await extractWithAst(
"suite('hello', () => {", //
" for (const name of ['foo', 'bar', 'baz']) {",
' it(name, () => {});',
Expand All @@ -111,8 +111,8 @@ describe('syntax', () => {
]);
});

it('stubs out requires and placeholds correctly', () => {
const src = extractWithAst('require("some invalid module").doing().other.things()');
it('stubs out requires and placeholds correctly', async () => {
const src = await extractWithAst('require("some invalid module").doing().other.things()');
expect(src).to.deep.equal([]);
});
});
11 changes: 11 additions & 0 deletions test-workspaces/typescript-top-level-await/.mocharc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
spec: '**/*.test.ts',
extension: [
"ts"
],
"node-option": [
"experimental-specifier-resolution=node",
"import=tsx",
"no-warnings"
],
};
26 changes: 26 additions & 0 deletions test-workspaces/typescript-top-level-await/hello.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { strictEqual } from 'node:assert';

// just some typescript code which would be valid directly in Node

function topLevel(a: number): string {
return a.toString() as string;
}

const fn = async () => {};
await fn();

describe('math', () => {
function inDescribe(a: number): string {
return a.toString() as string;
}
inDescribe(1);
topLevel(0);

it('addition', async () => {
strictEqual((1 + 1) as number, 2 as any as number);
});

it('subtraction', async () => {
strictEqual((1 - 1) as number, 0 as any as number);
});
});
File renamed without changes.
3 changes: 0 additions & 3 deletions test-workspaces/typescript/hello.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ function topLevel(a: number): string {
return a.toString() as string;
}

const fn = async () => {};
await fn();

describe('math', () => {
function inDescribe(a: number): string {
return a.toString() as string;
Expand Down

0 comments on commit 63032d1

Please sign in to comment.