ReTest Fails for Optional Chaining

Update: I’ve reverted to just CommonJS, and that fixed all this, but please see 2nd post below.

My tests are failing with a strange Node internal error. I’ve tried Node 14, 15, and 16, but all about the same. Binding to a JavaScript function, has some code like this:

    .then(
        json =>
            json?.auth?.client_token
    )

While ReScript compiles the code fine, ReTest gives me a runtime error:

(the arrow is pointing to the ? in the json, code snippet is just mis-aligned)

json?.auth?.client_token
                 ^

SyntaxError: Invalid or unexpected token

If I remove optional chaining, it’s fine. What?

1 Like

Oddly, I’m getting import errors too; I’ve had to change my JavaScript from ES6 .mjs to CommonJS .js. They work fine if I run them in Node 16. What is ReTest doing under the hood?

For example here’s the runtime generated code in the module up top:

import * as Sts from "./sts";
import * as Curry from "@rescript/std/lib/es6/curry.js";
import * as Fetch from "./fetch";

However, when I run it, it says it can’t find sts but it’s litterly right there, and the file path points to it. If I manually change the filenames, NO ERRORS:

import * as Sts from "./sts.mjs";
import * as Curry from "@rescript/std/lib/es6/curry.js";
import * as Fetch from "./fetch.mjs";

If I add --experimental-specifier-resolution flag, it works. This is… not desirable. Is there a way to follow the spec for now and add the file extensions to the compiled code? I can’t really change my build process (large company, I don’t own, yadda yadda) to add this experimental flag.

Oddly, the tests are compiled with the file extensions, but are trying to use import with js:

import * as Test from "rescript-test/src/Test.mjs"; // <-- notice the .mjs file extension
import * as Curry from "@rescript/std/lib/es6/curry.js"; // <-- notice the .js file extension
1 Like

Hi, can you open an issue on rescript-test with a small repro?

Although at first glance, it just seems like a node issue, you have some bindings missing their extensions. rescript-test doesn’t do anything special: it just imports your files and runs them.

Webpack and other bundlers tolerate these kind of imports but ES6 modules on Node don’t (like browsers), see Modules: ECMAScript modules | Node.js v16.3.0 Documentation.

I think it may have been older bs-platform vs. rescript and like you said some missing .mjs vs. js files. I upgraded, ran a clean, verified each file manually, and all is well, no need for issue; user error (sorry, I don’t know exactly what user error).

1 Like