connor4312 / nodejs-testing

VS Code integration for node:test native tests
MIT License
48 stars 7 forks source link

Typescript "as const" may break tree parsing #51

Open yleflour opened 2 months ago

yleflour commented 2 months ago

I encountered a weird bug:

import assert from "node:assert/strict";
import { describe, it } from "node:test";

describe("A", () => {
  describe("A.1", () => {
    it("works", async () => {
      assert.equal(1, 1);
    });

    it("breaks the tree", async () => {
      const plop = `Hello ${{ type: "extra" } as const} World`;
      assert.equal(1, 1);
    });
  });

  describe("A.2", () => {
    it("doesn't show", async () => {
      assert.equal(1, 1);
    });
  });
});

Produces the following tree:

image

The issue comes from ${{ type: "extra" } as const} and the following rewrites work:

connor4312 commented 2 months ago

That shouldn't affect things because this repo parses JS only, not the TypeScript sources. Are you able to toss your example setup in a repo so I can see your compiler settings and such?

yleflour commented 2 months ago

Just got a minimal sample repo set up here: https://github.com/yleflour/nodejs-testing-demo I'll check out the built JS. if I have time

yleflour commented 2 months ago

FYI Adding an "outDir": "./dist" and setting "emitDeclarationOnly": false in "tsconfig.json" fixes the issue as the test runner uses the .js files from the "./dist" folder Only seem to break with on the fly transpilation

connor4312 commented 1 month ago

This is because we try to still parse the source naively with acorn in the transpilation case. With acorn-loose it's often good enough but it seems like we get tripped up by this case.

While TS is technically not the only language that could transpile and get turned into JS, it's by far the most common one, so let's just ship a transpiler to deal with it properly.