nodejs-testing
nodejs-testing copied to clipboard
Typescript "as const" may break tree parsing
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:
The issue comes from ${{ type: "extra" } as const} and the following rewrites work:
- Using
${{ type: "extra" as const }} - Declaring
const inlineObj = { type: "extra" } as constand${inlineObj}
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?
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
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
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.
Fixed in https://github.com/connor4312/nodejs-testing/commit/08b5a2f4b6e64b9c1fe3dbd60628774cdde57e40, we will now use VS Code's TS language features to do the parsing