fix tests

This commit is contained in:
Dmitry Vasilev
2024-02-15 17:03:26 +08:00
parent 0d902418ee
commit fb07f81ff6
7 changed files with 26 additions and 10 deletions

2
src/effects.js vendored
View File

@@ -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,

View File

@@ -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)
}

View File

@@ -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(() => {

View File

@@ -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}

View File

@@ -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 => {

View File

@@ -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', () => {

View File

@@ -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}