vitest-dev / vitest

Next generation testing framework powered by Vite.
https://vitest.dev
MIT License
12.96k stars 1.16k forks source link

dynamic import of `conventional-changelog-angular` throws Promise `then` incompatible receiver object module #3705

Closed ghiscoding closed 9 months ago

ghiscoding commented 1 year ago

Describe the bug

Calling await import('conventional-changelog-angular') works fine in real world usage, that is in Lerna-Lite, but throws an error when running unit test with Vitest. I have Renovate running once a week on the repo, here's the failing PR job

To give a little bit of a background, I maintain Lerna-Lite (a fork of Lerna but rewritten as ESM) and the latest version of conventional-changelog-angular (which is a breaking version), that came out a few weeks ago, migrated from the Q library to pure Promise (see their v6 changelog).

The code and library works fine in real world usage (I previewed a release in Lerna-Lite and it works fine and I added console logs to make sure of it), so it only fails in Vitest but not outside of it.

Reproduction

Here's a Stackblitz repro: https://stackblitz.com/edit/vitest-dev-vitest-conventional?file=test%2Fbasic.test.ts

Calling this simple test

test('conventional-changelog-angular', async () => {
  let config = await import('conventional-changelog-angular');
  expect(config).toBeTruthy();
});

will throw

TypeError: Method Promise.prototype.then called on incompatible receiver [object Module]

or in some cases it throws this other but similar error

TypeError: Promise.prototype.then called on incompatible Proxy

System Info

System:
    OS: Windows 10 10.0.19045
    CPU: (12) x64 Intel(R) Xeon(R) CPU E5-1650 v2 @ 3.50GHz  
    Memory: 18.79 GB / 31.93 GB
  Binaries:
    Node: 18.16.1 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.19 - C:\Program Files (x86)\Yarn\bin\yarn.CMD 
    npm: 7.0.11 - C:\Program Files\nodejs\npm.CMD
    pnpm: 8.6.1 - C:\Program Files\nodejs\pnpm.CMD
  Browsers:
    Chrome: 114.0.5735.199
    Edge: Spartan (44.19041.1266.0), Chromium (114.0.1823.58)
  npmPackages:
    @vitest/coverage-v8: ^0.32.2 => 0.32.2
    vitest: ^0.32.2 => 0.32.2

Used Package Manager

pnpm

Validations

stackblitz[bot] commented 1 year ago

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

ghiscoding commented 1 year ago

@sheremet-va Thanks for looking into this, however when running Stackblitz on Chrome with v0.33.0 (which is supposed to include the fix), I have the same error as before

TypeError: Method Promise.prototype.then called on incompatible receiver [object Module]

image

while running Stackblitz on Firefox end up with a different error

TypeError: Promise.prototype.then called on incompatible Proxy

image

So that doesn't help with my original issue on the Lerna-Lite project, note again that the previous version of conventional-changelog-angular doesn't have this problem but they're using the Q library instead of Promises so that's probably why.

are there extra steps that I need to do in order for this basic unit test to pass?

here's the updated Stackblitz to Vitest v0.33.0 https://stackblitz.com/edit/vitest-dev-vitest-conventional-71ykal

sheremet-va commented 1 year ago

For your case, you can just disable deps.interopDefault. But I will look into it more

ghiscoding commented 1 year ago

Thanks for the quick hack, that did the trick and I can confirm that it works in this Lerna-Lite PR 666... I wonder if I should merge it though since 666 is the number of the beast (demon) 👿 haha