Skip to content

Mocha&Chai update #1499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Apr 25, 2025
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions Apps/UnitTests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ endif()
set(SCRIPTS
"Scripts/tests.js")

set(EXTERNAL_SCRIPTS
set(BABYLON_SCRIPTS
"../node_modules/babylonjs/babylon.max.js"
"../node_modules/babylonjs-materials/babylonjs.materials.js"
"../node_modules/chai/chai.js"
"../node_modules/mocha/mocha.js")
"../node_modules/babylonjs-materials/babylonjs.materials.js")

set(EXTERNAL_SCRIPTS
"../chai.umd.js"
"../mocha.umd.js")

set(SOURCES
"Shared/Shared.h"
Expand All @@ -26,7 +28,7 @@ elseif(WIN32)
set(SOURCES ${SOURCES} "Win32/App.cpp")
endif()

add_executable(UnitTests ${SCRIPTS} ${EXTERNAL_SCRIPTS} ${SOURCES})
add_executable(UnitTests ${SCRIPTS} ${EXTERNAL_SCRIPTS} ${BABYLON_SCRIPTS} ${SOURCES})
set_property(TARGET UnitTests PROPERTY UNITY_BUILD false)

target_link_libraries(UnitTests
Expand All @@ -49,7 +51,7 @@ add_test(NAME UnitTests COMMAND UnitTests)
add_custom_command(TARGET UnitTests POST_BUILD
COMMAND ${CMAKE_COMMAND} -E $<IF:$<BOOL:$<TARGET_RUNTIME_DLLS:UnitTests>>,copy,true> $<TARGET_RUNTIME_DLLS:UnitTests> $<TARGET_FILE_DIR:UnitTests> COMMAND_EXPAND_LISTS)

foreach(SCRIPT ${SCRIPTS} ${EXTERNAL_SCRIPTS})
foreach(SCRIPT ${SCRIPTS} ${EXTERNAL_SCRIPTS} ${BABYLON_SCRIPTS})
get_filename_component(SCRIPT_NAME "${SCRIPT}" NAME)
add_custom_command(
OUTPUT "${CMAKE_CFG_INTDIR}/Scripts/${SCRIPT_NAME}"
Expand All @@ -60,4 +62,5 @@ endforeach()

set_property(TARGET UnitTests PROPERTY FOLDER Apps)
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${SOURCES} ${SCRIPTS})
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR}/../node_modules PREFIX node_modules FILES ${EXTERNAL_SCRIPTS})
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR}/../ PREFIX umd FILES ${EXTERNAL_SCRIPTS})
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR}/../node_modules PREFIX node_modules FILES ${BABYLON_SCRIPTS})
55 changes: 52 additions & 3 deletions Apps/UnitTests/Shared/Shared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,34 @@ TEST(JavaScript, All)
Babylon::AppRuntime::Options options{};

options.UnhandledExceptionHandler = [&exitCodePromise](const Napi::Error& error) {
std::cerr << "[Uncaught Error] " << error.Get("stack").As<Napi::String>().Utf8Value() << std::endl;
std::ostringstream ss{};
ss << "[Uncaught Error] " << Napi::GetErrorString(error) << std::endl;

Napi::Value errorValue = error.Value();
if (errorValue.IsObject()) {
Napi::Object errorObj = errorValue.As<Napi::Object>();
// Get all property names
Napi::Array props = errorObj.GetPropertyNames();
uint32_t length = props.Length();

for (uint32_t i = 0; i < length; ++i) {
Napi::Value key = props[i];
Napi::Value val = errorObj.Get(key);

std::string keyStr = key.ToString().Utf8Value();
std::string valStr;

if (val.IsString() || val.IsNumber() || val.IsBoolean()) {
valStr = val.ToString().Utf8Value();
}
else {
valStr = "[non-primitive]";
}
ss << keyStr << ": " << valStr << std::endl;
}
}

std::cerr << ss.str();
std::cerr.flush();

exitCodePromise.set_value(-1);
Expand Down Expand Up @@ -89,8 +116,30 @@ TEST(JavaScript, All)
loader.Eval("location = { href: '' };", ""); // Required for Mocha.js as we do not have a location in Babylon Native
loader.LoadScript("app:///Scripts/babylon.max.js");
loader.LoadScript("app:///Scripts/babylonjs.materials.js");
loader.LoadScript("app:///Scripts/chai.js");
loader.LoadScript("app:///Scripts/mocha.js");
loader.Eval("exports = {};", ""); // Required for Chai.js as we do not have exports in Babylon Native
// Required for Mocha.js as self and globalThis don't exist
loader.Eval(R"(
(function() {
if (typeof globalThis === 'object') return;

Object.defineProperty(Object.prototype, '__magic__', {
get: function() {
return this;
},
configurable : true
});

__magic__.globalThis = __magic__;
delete Object.prototype.__magic__;
})();

if (typeof self === 'undefined') {
self = globalThis;
}
)", "");
loader.LoadScript("app:///Scripts/chai.umd.js");
loader.Eval("globalThis.chai = exports;", "");
loader.LoadScript("app:///Scripts/mocha.umd.js");
loader.LoadScript("app:///Scripts/tests.js");

device.StartRenderingCurrentFrame();
Expand Down
13 changes: 13 additions & 0 deletions Apps/babel-plugin-replace-bigint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = function() {
return {
visitor: {
BigIntLiteral(path) {
const value = path.node.value; // Get the BigInt literal value
path.replaceWith({
type: 'NumericLiteral',
value: Number(value), // Convert BigInt to Number
});
},
},
};
};
10 changes: 10 additions & 0 deletions Apps/babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"presets": [
["@babel/preset-env", {
"targets": {
"ie": "11"
}
}]
],
"plugins": ["./babel-plugin-replace-bigint"]
}
Loading