Problem with bsconfig.json when running tests


#1

I created a new project using reason-scripts. When I run yarn test, I get this error:

 FAIL  src/App_test.re
  ● Test suite failed to run

    /Users/fhsu/reference/hello/node_modules/@glennsl/bs-jest/src/jest.js:3
    import * as List from "bs-platform/lib/es6/list.js";
           ^

    SyntaxError: Unexpected token *

      at new Script (vm.js:74:7)
      at Object.<anonymous> (src/App_test.re:3:36)

In my bsconfig.json, if I change package-specs from es6 to commonjs, the tests pass. However, I generally want to compile as es6 so that I get dead code elimination. What’s the workaround for this? Write a script that automatically changes package-specs to commonjs whenever I want to run my tests?


#2

This is a huge problem for Reason-scripts. There isn’t a workaround atm, and it’s just a bad situation.


#3

There’s a couple of things you can do @feihong. I’m assuming you are using Jest? Perhaps even Create React App? The simplest thing is to compile to commonjs modules rather than es6 (lame I know) this will fix the issue because Jest by default doesn’t handle es6, at least with my limited experience at configuring it.

The Reason version of react-scripts has it configured properly so you can use es6 module, I just converted my project over to that. It has the added benefit of allowing you to import .re files in JS. The downside for me was the fact that default local imports would favor a file of the same name as the reason one which caused a lot of issues for me.

Hopefully there will be a better solution in the future.


#4

Does the latest reason-scripts work with ES6 modules + tests?


#5

I actually have this figured out sometimes ago. I’ll dig my old code tomorrow for the configuration.


#6

It should :slight_smile:


#7

Has anyone managed to find a solution to this yet?


#8

@justgage I’m looking at bsconfig.json in master, and it looks like they “fixed” it by setting package-specs/module to commonjs.


#9

I know Jest can be configured to work with es6, it’s just tricky


#10

My advice: don’t use reason-scripts.