Trouble with @bablyonjs es6 modules and jest and typescript

Not sure if this is the correct forum for this. Please excuse me if it’s not. I’m starting a project from scratch and I want to use the ES6 modules of BabylonJS with Typescript and Jest. Problem is that I can’t write the simplest unit tests using Babylon.

I’ve created a repo to demonstrate the problem.

I can create a regular unit test without babylon, but once I add a reference to one of babylon’s ES6 modules, it fails.

Any help would be greatly appreciated. I really want to make a lightweight app that doesn’t need a lot of Babylon’s extra features so I want to take advantage of tree shaking and get it down to the core.

Thanks,
Joe

pinging @RaananW

What error did you get?

<rootDir>/BabylonTypeScriptJest/node_modules/@babylonjs/core/Engines/nullEngine.js:1
    import * as tslib_1 from "tslib";
           ^

    SyntaxError: Unexpected token *

      1 | import { Engine } from "@babylonjs/core/Engines/engine";
    > 2 | import { NullEngine } from "@babylonjs/core/Engines/nullEngine";

I am not sure why it imports * from tslib, this is a line that is added by typescript.

do you have tslib included as a dependency? could you try adding it as a devDependency and see if it helps?

Added “tslib”: “^1.11.0”, didn’t help. Same error. Could there be something in tsconfig?

Can I ask what node version you are using? 10 or under by any chance?

v10.16.0

Node 10 and under doesn’t support the * import style, and this is what it is complaining about. It seems like we should set node’s target in or package. This import doesn’t come from us but from TypeScript, otherwise we would have fixed it for sure.

Having said that, we build Babylon with node 10 as well with no problem. We need to check what causes this.

1 Like

@Deltakosh - what node version are you running when building?

Running exactly on that version :slight_smile: (v10.16.0)

Can you share your TSC -v output?

3.7.5

I’ll tell you the weirdest thing - we don’t have a line with * from tslib. we have imports, but not *. Is that an npm package? what version if I can ask?

The point is to have jest transform babylon’s .js files in node_modules.

I managed to run jest in your demo repo, with node 10 and the following changes:

diff --git a/jest.config.js b/jest.config.js
index 91a2d2..6d6a94 100644
--- a/jest.config.js
+++ b/jest.config.js
@@ -1,4 +1,14 @@
 module.exports = {
-  preset: 'ts-jest',
+  preset: 'ts-jest/presets/js-with-ts',
+  transformIgnorePatterns: [/* to have node_modules transformed*/],
   testEnvironment: 'node',
-};
\ No newline at end of file
+  globals: {
+    'ts-jest': {
+      isolatedModules: true, // to make type check faster
+      tsConfig: {            // to have tsc transform .js files
+        allowJs: true,
+        checkJs: false,
+      },
+    }
+  },
+};
2 Likes

npm package @babylonjs/core 4.0.3 had that import * as tslib line.

I’m guessing OP’s problem rose from running ES module build of babylon with node v10 and no transform. This usage may not be covered in build process (I didn’t look).

1 Like

Thank, I’ll give it a try.

It worked! Thanks for helping me out.

1 Like