vitest-dev / vitest

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

UnhandledRejection: process.memoryUsage #6500

Open ghost opened 1 month ago

ghost commented 1 month ago

Describe the bug

𝐇ello˙. 𝐀m 𝐈 doing something wrong˙? ಥ_ಥ

Run the following script:

"test:browser": "vitest run",

It runs correctly. (。◕‿‿◕。)


Run the following script:

"test:browser-flaky": "vitest run --browser.enabled=true",

Message output in console ⚆ _ ⚆ ``` bash ❯ npm run test:browser-flaky > test:browser-flaky > vitest run --browser.enabled=true RUN v2.1.1 /home/projects/vitest-dev-vitest-gmzfxl Browser runner started at http://localhost:5173/ Coverage enabled with v8 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ 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. ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Coverage Error ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Error: CDP is not supported by the provider "preview". ❯ receiverOnMessage node_modules/ws/lib/websocket.js:1220:20 ❯ dataMessage node_modules/ws/lib/receiver.js:596:14 ❯ getData node_modules/ws/lib/receiver.js:496:10 ❯ startLoop node_modules/ws/lib/receiver.js:167:16 ❯ _write node_modules/ws/lib/receiver.js:94:10 ❯ socketOnData node_modules/ws/lib/websocket.js:1355:35 ❯ _0x26b1b3/< ../../../blitz.28a8ce1b.js:40:516951 ❯ _0x4d0c70/< ../../../blitz.28a8ce1b.js:40:518489 ❯ _0x132427/< ../../../blitz.28a8ce1b.js:40:195670 ❯ _0x4f7512 ../../../blitz.28a8ce1b.js:40:195775 ❯ _0x4d0c70 ../../../blitz.28a8ce1b.js:40:518414 ❯ _0x3e05c5/< ../../../blitz.28a8ce1b.js:40:519146 ❯ _0x3e05c5 ../../../blitz.28a8ce1b.js:40:519125 ❯ _0x1a4990 ../../../blitz.28a8ce1b.js:40:518751 ❯ _0x38702d ../../../blitz.28a8ce1b.js:40:517760 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Rejection ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Error: CDP is not supported by the provider "preview". ❯ receiverOnMessage node_modules/ws/lib/websocket.js:1220:20 ❯ dataMessage node_modules/ws/lib/receiver.js:596:14 ❯ getData node_modules/ws/lib/receiver.js:496:10 ❯ startLoop node_modules/ws/lib/receiver.js:167:16 ❯ _write node_modules/ws/lib/receiver.js:94:10 ❯ socketOnData node_modules/ws/lib/websocket.js:1355:35 ❯ _0x26b1b3/< ../../../blitz.28a8ce1b.js:40:516951 ❯ _0x4d0c70/< ../../../blitz.28a8ce1b.js:40:518489 ❯ _0x132427/< ../../../blitz.28a8ce1b.js:40:195670 ❯ _0x4f7512 ../../../blitz.28a8ce1b.js:40:195775 ❯ _0x4d0c70 ../../../blitz.28a8ce1b.js:40:518414 ❯ _0x3e05c5/< ../../../blitz.28a8ce1b.js:40:519146 ❯ _0x3e05c5 ../../../blitz.28a8ce1b.js:40:519125 ❯ _0x1a4990 ../../../blitz.28a8ce1b.js:40:518751 ❯ _0x38702d ../../../blitz.28a8ce1b.js:40:517760 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Test Files no tests Tests no tests Errors 2 errors Start at 17:31:33 Duration 12.91s (transform 0ms, setup 0ms, collect 0ms, tests 0ms, environment 0ms, prepare 0ms) % @vitest/coverage-v8 does not work on Stackblitz. Report will be empty. % Coverage report from v8 -------------|---------|----------|---------|---------|------------------- File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s -------------|---------|----------|---------|---------|------------------- All files | 0 | 0 | 0 | 0 | ...t-gmzfxl | 0 | 0 | 0 | 0 | ...nfig.js | 0 | 0 | 0 | 0 | 1-31 ...zfxl/src | 0 | 0 | 0 | 0 | index.js | 0 | 0 | 0 | 0 | 1-2 ...test.js | 0 | 0 | 0 | 0 | 1-9 -------------|---------|----------|---------|---------|------------------- [UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "TypeError: this.server is undefined".] { code: 'ERR_UNHANDLED_REJECTION' } Node.js v18.20.3 ```

Run the following script:

"test:browser-error": "vitest run --browser.enabled=true --coverage.enabled=false"

Message output in console ⚆ _ ⚆ ``` bash ❯ npm run test:browser-error > test:browser-error > vitest run --browser.enabled=true --coverage.enabled=false RUN v2.1.1 /home/projects/vitest-dev-vitest-gmzfxl Browser runner started at http://localhost:5173/ · src/index.test.js (1) · config needs to be object ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Errors ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Vitest caught 1 unhandled error during the test run. This might cause false positive tests. Resolve unhandled errors to make sure your tests are not affected. ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Rejection ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ TypeError: process.memoryUsage is not a function ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Test Files (1) Tests (1) Errors 1 error Start at 17:34:48 Duration 7.55s (transform 0ms, setup 0ms, collect 61ms, tests 0ms, environment 0ms, prepare 40ms) ```

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-gmzfxl?file=package.json

System Info

System:
    OS: Linux 6.8 Ubuntu 24.04.1 LTS 24.04.1 LTS (Noble Numbat)
    CPU: (1) Intel Celeron 352
    Memory: 1.66 GB / 15.35 GB
    Container: Yes
    Shell: 5.2.21 - /bin/bash
  Binaries:
    Node: 22.8.0 - ~/.nvm/versions/node/v22.8.0/bin/node
    npm: 10.8.2 - ~/.nvm/versions/node/v22.8.0/bin/npm

Used Package Manager

npm

Validations

ghost commented 1 month ago

When I erase the line "logHeapUsage": true, in configVitest.json, at least one of the error messages disappear. But I wanted to keep this line in place.

hi-ogawa commented 1 month ago

I'm not sure if logHeapUsage can be supported for browser mode since it's relying on Node's process.memoryUsage. At least we could prevent the runtime error if we guarded this by typeof process.memoryUsage === 'function':

https://github.com/vitest-dev/vitest/blob/318747da56911c6d47911bc26ee7baa3930d0eb5/packages/vitest/src/runtime/runners/test.ts#L80-L82


The error Error: CDP is not supported by the provider "preview" is probably only on stackblitz? It forces preview provider, but this happens after @vitest/coverage-v8 config checks, so it misses validations:

https://github.com/vitest-dev/vitest/blob/16aa76c261c1559e88cb6a39b71923d0638750c1/packages/vitest/src/node/config/resolveConfig.ts#L225-L235

https://github.com/vitest-dev/vitest/blob/318747da56911c6d47911bc26ee7baa3930d0eb5/packages/vitest/src/node/config/resolveConfig.ts#L769-L771

ghost commented 1 month ago

Maybe Performance.measureUserAgentSpecificMemory() is equivalent to Node.js's process.memoryUsage because it estimates the memory used by the iframes and workers of a web page?

async function getMemoryUsage() {
  if (typeof performance !== 'undefined' && 'measureUserAgentSpecificMemory' in performance) {
    try {
      const memorySample = await performance.measureUserAgentSpecificMemory();
      return memorySample.bytes;
    } catch (error) {
      console.error('Error measuring memory:', error);
      return null;
    }
  } else {
    console.warn('Performance.measureUserAgentSpecificMemory is not supported in this browser.');
    return null;
  }
}

if (this.config.logHeapUsage) {
  getMemoryUsage().then(heapUsed => {
    if (heapUsed !== null) {
      test.result!.heap = heapUsed;
      console.log('Heap Usage:', heapUsed);
    }
  });
}