From fb07f81ff6d0109733fe13758b3ded37c4a6657a Mon Sep 17 00:00:00 2001 From: Dmitry Vasilev Date: Thu, 15 Feb 2024 17:03:26 +0800 Subject: [PATCH] fix tests --- src/effects.js | 2 +- src/eval.js | 3 ++- src/runtime/record_io.js | 2 +- src/runtime/runtime.js | 2 +- test/run_utils.js | 10 ++++++++++ test/test.js | 4 ++-- test/utils.js | 13 +++++++++---- 7 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/effects.js b/src/effects.js index e9b9913..f49351e 100644 --- a/src/effects.js +++ b/src/effects.js @@ -181,7 +181,7 @@ export const apply_side_effects = (prev, next, ui) => { next.eval_modules_state != null ) { const s = next.eval_modules_state - s.promise.__original_then(result => { + s.promise.then(result => { exec('eval_modules_finished', next, /* becomes prev_state */ result, diff --git a/src/eval.js b/src/eval.js index d3f0aaf..8f8f331 100644 --- a/src/eval.js +++ b/src/eval.js @@ -515,7 +515,8 @@ export const eval_modules = ( } if(is_async) { - return result.__original_then(make_result) + // convert app_window.Promise to host Promise + return Promise.resolve(result).then(make_result) } else { return make_result(result) } diff --git a/src/runtime/record_io.js b/src/runtime/record_io.js index 9ad9eb1..409190e 100644 --- a/src/runtime/record_io.js +++ b/src/runtime/record_io.js @@ -75,7 +75,7 @@ const make_patched_method = (window, original, name, use_context) => { ? new original(...args) : original.apply(this, args) - if(value instanceof cxt.window.Promise) { + if(value?.[Symbol.toStringTag] == 'Promise') { // TODO use __original_then, not finally which calls // patched 'then'? value = value.finally(() => { diff --git a/src/runtime/runtime.js b/src/runtime/runtime.js index 0295968..a232b19 100644 --- a/src/runtime/runtime.js +++ b/src/runtime/runtime.js @@ -262,7 +262,7 @@ const __await_start = (cxt, promise) => { const children_copy = cxt.children const result = {children_copy, promise} - if(promise instanceof cxt.window.Promise) { + if(promise?.[Symbol.toStringTag] == 'Promise') { result.promise = promise.then( (value) => { result.status = {ok: true, value} diff --git a/test/run_utils.js b/test/run_utils.js index df7c261..12a92db 100644 --- a/test/run_utils.js +++ b/test/run_utils.js @@ -1,3 +1,13 @@ +/* + For node.js tests + + It forces node.js to load Response (which is loaded lazily) + + Without this, `Response` loading code would be executed in record_io.js and + break test by calling `now()` +*/ +globalThis.Response + export const run = tests => { // Runs test, return failure or null if not failed const run_test = t => { diff --git a/test/test.js b/test/test.js index b77fd3a..5f98e04 100644 --- a/test/test.js +++ b/test/test.js @@ -1059,7 +1059,7 @@ export const tests = [ 'a': 'Object.assign(globalThis, {test_import: true})', }) assert_equal(i.active_calltree_node.ok, true) - assert_equal(globalThis.test_import, true) + assert_equal(globalThis.app_window.test_import, true) }), test('modules bare import', () => { @@ -1068,7 +1068,7 @@ export const tests = [ 'a': 'Object.assign(globalThis, {test_import: true})', }) assert_equal(i.active_calltree_node.ok, true) - assert_equal(globalThis.test_import, true) + assert_equal(globalThis.app_window.test_import, true) }), test('bug parser pragma external', () => { diff --git a/test/utils.js b/test/utils.js index a3a9626..a55991c 100644 --- a/test/utils.js +++ b/test/utils.js @@ -11,12 +11,17 @@ Object.assign(globalThis, // for convenince, to type just `log` instead of `console.log` log: console.log, - // For test env, set globalThis.app_window to just globalThis - app_window: globalThis, } ) export const patch_builtin = new Function(` +if(globalThis.process != null ) { + globalThis.app_window = globalThis +} else { + const iframe = globalThis.document.createElement('iframe') + globalThis.document.body.appendChild(iframe) + globalThis.app_window = iframe.contentWindow +} let originals = globalThis.app_window.__builtins_originals let patched = globalThis.app_window.__builtins_patched if(originals == null) { @@ -170,9 +175,9 @@ export const test_deferred_calls_state = code => { export const stringify = val => JSON.stringify(val, (key, value) => { - if(value instanceof Set){ + if(value?.[Symbol.toStringTag] == 'Set'){ return [...value] - } else if (value instanceof Map) { + } else if(value?.[Symbol.toStringTag] == 'Map'){ return Object.fromEntries([...value.entries()]) } else if(value instanceof Error) { return {message: value.message}