Skip to content

Commit 720b77a

Browse files
Mocha&Chai update (#1499)
- upgrade Mocha and Chai version - dump exception error object properties (works well with Chakra) - use Babel to translate ES module to umd - replace bigint notation (with `n` suffix) unsupported by Chakra - script loading tweaks for unittests to support `globalThis` and Chai exports
1 parent 9fbab1a commit 720b77a

File tree

6 files changed

+2813
-224
lines changed

6 files changed

+2813
-224
lines changed

Apps/UnitTests/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ set(SCRIPTS
99
set(EXTERNAL_SCRIPTS
1010
"../node_modules/babylonjs/babylon.max.js"
1111
"../node_modules/babylonjs-materials/babylonjs.materials.js"
12-
"../node_modules/chai/chai.js"
13-
"../node_modules/mocha/mocha.js")
12+
"../node_modules/chai/chai.umd.js"
13+
"../node_modules/mocha/mocha.umd.js")
1414

1515
set(SOURCES
1616
"Shared/Shared.h"

Apps/UnitTests/Shared/Shared.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,31 @@ TEST(JavaScript, All)
8989
loader.Eval("location = { href: '' };", ""); // Required for Mocha.js as we do not have a location in Babylon Native
9090
loader.LoadScript("app:///Scripts/babylon.max.js");
9191
loader.LoadScript("app:///Scripts/babylonjs.materials.js");
92-
loader.LoadScript("app:///Scripts/chai.js");
93-
loader.LoadScript("app:///Scripts/mocha.js");
92+
loader.Eval("exports = {};", ""); // Required for Chai.js as we do not have exports in Babylon Native
93+
// Required for Mocha.js as self and globalThis don't exist
94+
// This should be removed once using Webpack and making a bundle with test.ts instead of a .js
95+
loader.Eval(R"(
96+
(function() {
97+
if (typeof globalThis === 'object') return;
98+
99+
Object.defineProperty(Object.prototype, '__magic__', {
100+
get: function() {
101+
return this;
102+
},
103+
configurable : true
104+
});
105+
106+
__magic__.globalThis = __magic__;
107+
delete Object.prototype.__magic__;
108+
})();
109+
110+
if (typeof self === 'undefined') {
111+
self = globalThis;
112+
}
113+
)", "");
114+
loader.LoadScript("app:///Scripts/chai.umd.js");
115+
loader.Eval("globalThis.chai = exports;", "");
116+
loader.LoadScript("app:///Scripts/mocha.umd.js");
94117
loader.LoadScript("app:///Scripts/tests.js");
95118

96119
device.StartRenderingCurrentFrame();

Apps/babel-plugin-replace-bigint.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = function() {
2+
return {
3+
visitor: {
4+
BigIntLiteral(path) {
5+
const value = path.node.value; // Get the BigInt literal value
6+
path.replaceWith({
7+
type: 'NumericLiteral',
8+
value: Number(value), // Convert BigInt to Number
9+
});
10+
},
11+
},
12+
};
13+
};

Apps/babel.config.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"presets": [
3+
["@babel/preset-env", {
4+
"targets": {
5+
"ie": "11"
6+
}
7+
}]
8+
],
9+
"plugins": ["./babel-plugin-replace-bigint"]
10+
}

0 commit comments

Comments
 (0)