mike-lischke / antlr4ng

Next Generation TypeScript runtime for ANTLR4
Other
66 stars 11 forks source link

How to load generated parser in tests? #13

Closed alessiostalla closed 7 months ago

alessiostalla commented 7 months ago

I've tried with Mocha and Jest (ts-jest) but both fail loading antlr4ng-generated parsers with ESM-related issues. I'm not sure how to tackle the problem. E.g. with Jest I get

 FAIL  tests/strumenta-playground.test.ts
  ● Test suite failed to run

    Must use import to load ES Module: /Users/alessio/projects/at-strumenta-ast/node_modules/antlr4ng/dist/antlr4.mjs

       5 | import {ParserTraceLoader, saveForStrumentaPlayground} from "../src/interop/strumenta-playground";
       6 | import {NodeSubclass} from "./nodes";
    >  7 | import {CharStream, CommonToken, Lexer, TerminalNode, Token, TokenStream} from "antlr4ng";
         | ^

Is there some recommended setup for using antlr4ng in tests? Note that the non-test build apparently succeeds (maybe it would later fail when linked, or at runtime).

mike-lischke commented 7 months ago

How do you run Jest? What I do in my VS Code extension (where I import antlr4ng just like you above) is:

node --no-warnings --experimental-vm-modules node_modules/jest/bin/jest.js --no-coverage

Additionally (to allow using TypeScript for tests and source files), I have a jest config file with a "transform" entry:

    transform: {
        '\\.ts?$': ['ts-jest', { useESM: true }]
    },

See the full setup here. You need to install ts-jest as a dev dependency.

alessiostalla commented 7 months ago

Thanks, I managed to get it working! Now I have other issues, but they're unrelated :)

mike-lischke commented 7 months ago

Did it work with what I suggested or did you have to find a different solution?

alessiostalla commented 7 months ago

A similar solution I copied from antlr4-c3 :)