Goal
Jest test react-native application that using realm sync as database completely local without using internet connection nor hit realm sync server.
My App Logic
User login with realm user credential and sync data from device to the realm server
How I did it
First I reroute the routine that opening the realm connection so that when the code runs in the jest-test environment it will open the local file realm instead of realm sync.
Then by mocking realm-network-transport
module I intercept any request attempt to the remote realm server (mongo stitch server) so it will serve the response from a designated static response I prepared.
It is also applied to user function call as at the end realm function call will use HTTP request using realm-network-transport
module
The Problem
All is working fine prior to using realmjs v10.1.2,but after using v10.1.2 only authentication routine is works, function call throwing error with this workaround (things do works normal in normal run)
The error reported is
JS value must be of type 'object', got (undefined)
at func (.../node_modules/realm/lib/user.js:34:37)
...
the code pointed by the error is
callFunction(name, args, service = undefined) {
return promisify(cb => this._callFunction(name, this._cleanArgs(args), service, cb));
},
i tried to console.log this, name, args, and service
, it yield
{} clientAvailableOutletPosid [...function argument...] undefined
which i guess will go to /node_modules/realm/src/js_user.hpp
line 354
template<typename T>
void UserClass<T>::call_function(ContextType ctx, ObjectType this_object, Arguments& args, ReturnValue &) {
args.validate_count(4);
auto user = get_internal<T, UserClass<T>>(ctx, this_object);
auto name = Value::validated_to_string(ctx, args[0], "name");
auto call_args_js = Value::validated_to_array(ctx, args[1], "args");
auto service = Value::is_undefined(ctx, args[2])
? util::none
: util::Optional<std::string>(Value::validated_to_string(ctx, args[2], "service"));
auto callback = Value::validated_to_function(ctx, args[3], "callback");
auto call_args_bson = Value::to_bson(ctx, call_args_js);
user->m_app->call_function(
*user,
name,
call_args_bson.operator const bson::BsonArray&(),
service,
Function::wrap_callback_error_first(ctx, this_object, callback,
[] (ContextType ctx, const util::Optional<bson::Bson>& result) {
REALM_ASSERT_RELEASE(result);
return Value::from_bson(ctx, *result);
}));
}
Where to go from here?
question from:
https://stackoverflow.com/questions/65895893/reroute-realm-network-for-jest-test-locally-error-when-call-realm-function