Babylon does not work with Jest

Does anyone have experience with Jest?

utils.ts

import { Vector3 } from '@babylonjs/core';

export const roundVector = (vec: Vector3): Vector3 => {
  return new Vector3(Math.round(vec.x), Math.round(vec.y), Math.round(vec.z));
};

utils.spec.ts

import { Vector3 } from '@babylonjs/core';
import { roundVector } from './utils';

describe('utils', () => {
  test('roundVector', () => {
    expect(roundVector(new Vector3(0.4, 1.5, 2.6)).toString()).toBe(
      '{ X: 0 Y: 2 Z: 3 }'
    );
  });
});

In this example I get the error:

\node_modules\@babylonjs\core\index.js:2
export * from "./abstractScene.js";
^^^^^^

SyntaxError: Unexpected token 'export'

The Jest config transformIgnorePatterns does not work in this repo. I have another repo where transformIgnorePatterns works, but then Jest RUN hangs without errors. I’m going crazy. @babylonjs/core is the only package which causes this problem. My original repo is a huge project with a lot of packages. Including ES modules. It’s weird why babaylon js is not working here. I feel like a noob. Ok, this repo may works a bit different like my original repo (Angular). I have no idea what’s wrong.

Please help! I appreciate any hint or pray for solution.

Again the info: In my Angular repo I was able to mute the Unexpected token ‘export’. But then, Jest runs infinitely. No error nothing. I never experiences such a weird behavior. Does anyone have a working repo with similar setup?

ping @RaananW

Due to the way jest is dealing with es modules, you need to configure your project a bit differently to get baylon to work. This template has a working jest configuration: RaananW/babylonjs-webpack-es6: Babylon.js basic scene with typescript, webpack, es6 modules, editorconfig, eslint, hot loading and more. Will even make coffee if you ask nicely. (github.com)

Thanks. It seems that the following changes works:

jest.config.ts

preset: "ts-jest/presets/default-esm"

package.json scripts

"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest"

Not fully tested yet and I’m not a fan of experimental unstable stuff… But ok. I just wondering why Babylon.js is my first esm package which does not work. I have a lot of other esm libs. Weird…

1 Like