rescript-lang / rescript-compiler

The compiler for ReScript.
https://rescript-lang.org
Other
6.68k stars 445 forks source link

Promise2 module produces an error if it loaded via async import() #6503

Open KonstantinKai opened 10 months ago

KonstantinKai commented 10 months ago

Hi, I've tried to cover some code with tests with rescript-vitest and found an interesting bug:

Here is my test file:

open Vitest

testAsync("async test", async _ => {
  await expect(Js.Promise2.resolve(1)->Js.Promise2.then(_ => Js.Promise2.resolve(2)))
  ->Expect.Promise.resolves
  ->Expect.Promise.toEqual(2)
})

When I run it, I've got the following error:


⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Errors ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

Vitest caught 2 unhandled errors during the test run.
This might cause false positive tests. Resolve unhandled errors to make sure your tests are not affected.

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Rejection ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
DataCloneError: function () { [native code] } could not be cloned.
 ❯ new DOMException node:internal/per_context/domexception:53:5
 ❯ post node_modules/vitest/dist/worker.js:49:14
 ❯ sendEvent node_modules/vitest/dist/vendor-index.b271ebe4.js:25:9
 ❯ node_modules/vitest/dist/vendor-rpc.cbd8e972.js:46:24
 ❯ withSafeTimers node_modules/vitest/dist/vendor-rpc.cbd8e972.js:20:20
 ❯ Proxy.safeSendCall node_modules/vitest/dist/vendor-rpc.cbd8e972.js:45:41
 ❯ VitestTestRunner.testRunner.onCollected node_modules/vitest/dist/vendor-index.3e351f42.js:66:11
 ❯ startTests node_modules/@vitest/runner/dist/index.js:841:51
 ❯ node_modules/vitest/dist/entry.js:103:7
 ❯ withEnv node_modules/vitest/dist/entry.js:73:5

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
Serialized Error: { code: 25, INDEX_SIZE_ERR: 1, DOMSTRING_SIZE_ERR: 2, HIERARCHY_REQUEST_ERR: 3, 
RONG_DOCUMENT_ERR: 4, INVALID_CHARACTER_ERR: 5, NO_DATA_ALLOWED_ERR: 6, NO_MODIFICATION_ALLOWED_ERR: 7, NOT_FOUND_ERR: 8, NOT_SUPPORTED_ERR: 9, INUSE_ATTRIBUTE_ERR: 10, INVALID_STATE_ERR: 11, SYNTAX_ERR: 12, INVALID_MODIFICATION_ERR: 13, NAMESPACE_ERR: 14, INVALID_ACCESS_ERR: 15, VALIDATION_ERR: 16, TYPE_MISMATCH_ERR: 17, SECURITY_ERR: 18, NETWORK_ERR: 19, ABORT_ERR: 20, URL_MISMATCH_ERR: 21, QUOTA_EXCEEDED_ERR: 22, TIMEOUT_ERR: 23, INVALID_NODE_TYPE_ERR: 24, DATA_CLONE_ERR: 25 }
This error originated in "tests/TestModule_test.bs.mjs" test file. It doesn't mean the error was thrown inside the file itself, but while it was running.

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Error ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
{ type: 'Unhandled Error', stacks: [] }
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 Test Files  no tests
      Tests  no tests
     Errors  2 errors
   Start at  12:15:37
   Duration  496ms (transform 32ms, setup 0ms, collect 0ms, tests 0ms, environment 0ms, prepare 0ms)

On opposite if I change the previous code to:

testAsync("async test", async _ => {
  await expect(Js.Promise2.resolve(1)->Js.Promise.then_(_ => Js.Promise2.resolve(2), _))
  ->Expect.Promise.resolves
  ->Expect.Promise.toEqual(2)
})

My test works fine


After deep research, I found that the problem occurs while importing the module js_promise2 via import()

Then I tried to reproduce this without rescript and finally, I got an error while importing

Here is STR:

Part of the source taken from: rescript js promise

// file: testable.mjs
var then = function (p, cont) {
  return Promise.resolve(p).then(cont);
};

export { then };
// file test.mjs
try {
  const mod = await import("./testable.mjs");
} catch (e) {
  // here is an error
  // typeof e === 'function'
  console.log('')
  console.log(e);
}

Run It with

node test.mjs # produces the following output

#[Function (anonymous)]

System info

System:
  OS: macOS 13.4.1
  CPU: (12) x64 Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
  Memory: 1.62 GB / 16.00 GB
  Shell: 5.9 - /bin/zsh
Binaries:
  Node: 18.18.0 - ~/.volta/tools/image/node/18.18.0/bin/node
  Yarn: 1.22.18 - ~/.volta/tools/image/yarn/1.22.18/bin/yarn
  npm: 8.19.1 - ~/.volta/tools/image/npm/8.19.1/bin/npm
  pnpm: 8.11.0 - ~/.volta/tools/image/pnpm/8.11.0/bin/pnpm
  Watchman: 2023.07.03.00 - /usr/local/bin/watchman
Browsers:
  Chrome: 118.0.5993.117
  Safari: 16.5.2
npmPackages:
  @vitejs/plugin-react: 4.0.0 => 4.0.0
  rescript: 11.0.0-rc.5 => 11.0.0-rc.5
  vite: ^4.4.9 => 4.5.0
  vitest: ^0.34.6 => 0.34.6
AlexMoutonNoble commented 7 months ago

thanks for writing this up @KonstantinKai