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.


#11

Last reply on this was back in August. Has the situation improved at all? I “solved” my testing problem by writing a script that converts package-specs/module to commonjs while running tests and then automatically changes it back after tests have finished running. But this is hacky and also takes a long time because it recompiles everything when you change package-specs/module.

@thangngoc89 can you show us how you solved it?


#12

I had a similar issue in a project set up with create-react-app.

My solution was to add the following jest configuration:

yarn run test --transformIgnorePatterns "node_modules/(?!(bs-platform|reason-react|@glennsl\/bs-jest)/)"

Here I’ve listed 3 libraries (bs-platform, reason-react and @glennsl/bs-jest) but you should add any other reason libraries that you’re using.

The default create-react-app jest configuration does not transform node_modules with babel. This flag tells jest to skip transforming apart from the listed libraries. The libraries’ code will be transformed, thus allowing jest to understand the import statement.


Jest testing with ReasonML & Create-React-App
#14

You can configure bsconfig to output both at the same time.

I am a bit confused, this is not a jest issue, did we output invaild es6 code?


#15

I figured out the problem: Jest assumes all the dependencies in node_modules are built in commonjs, which is not the case for BuckleScript. This seems to be the case to for many react-native deps. More info: https://jestjs.io/docs/en/tutorial-react-native#transformignorepatterns-customization.

Following the default Babel setup in the Jest docs, and adding the following to package.json fixes it:

  "jest": {
    "transformIgnorePatterns": [
      "/node_modules/(?!@glennsl/bs-jest|bs-platform).+\\.js$"
    ]
  }

Depending on the project, other BuckleScript dependencies used in the tests might need to be added that regexp.

I set up a demo repo in https://github.com/jchavarri/example-bsb-jest-es6