vitest icon indicating copy to clipboard operation
vitest copied to clipboard

Empty test causes writableLength undefined error

Open natew opened this issue 3 years ago • 10 comments
trafficstars

Describe the bug

I get this even with nothing but an empty test('', () => {}) in my test file:

text|nate/dev⚡ ⇒ yarn test
$ test
$ vitest --config ../vite-plugin-internal/vite.config.ts

 DEV  v0.17.1 /Users/n8/tamagui/packages/text

 ✓ src/wrapChildrenInText.test.tsx (2)

Test Files  1 passed (1)
     Tests  2 passed (2)
      Time  803ms (in thread 1ms, 80323.73%)

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

Vitest caught 1 unhandled error during the test run. This might cause false positive tests.
Please, resolve all the errors to make sure your tests are not affected.

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Error ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
Error: Errors occurred while running tests. For more information, see serialized error.
 ❯ Object.runTests ../../node_modules/vitest/dist/chunk-vite-node-externalize.0fc8ed68.mjs:7046:17
 ❯ processTicksAndRejections node:internal/process/task_queues:96:5
 ❯ async file:/Users/n8/tamagui/node_modules/vitest/dist/chunk-vite-node-externalize.0fc8ed68.mjs:10428:9
 ❯ Vitest.runFiles ../../node_modules/vitest/dist/chunk-vite-node-externalize.0fc8ed68.mjs:10441:12
 ❯ Vitest.start ../../node_modules/vitest/dist/chunk-vite-node-externalize.0fc8ed68.mjs:10362:5
 ❯ startVitest ../../node_modules/vitest/dist/chunk-vite-node-externalize.0fc8ed68.mjs:11110:5
 ❯ CAC.start ../../node_modules/vitest/dist/cli.mjs:666:9

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
Serialized Error: {
  "errors": [
    [TypeError: Cannot read properties of undefined (reading 'writableLength')],
  ],
}
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 FAIL  Tests failed. Watching for file changes...
       press h to show help, press q to quit

Reproduction

git clone [email protected]:tamagui/tamagui.git
cd tamagui
./script/ci-prepare.sh
yarn
cd packages/text
yarn test

System Info

System:
    OS: macOS 12.4
    CPU: (8) arm64 Apple M1
    Memory: 81.06 MB / 16.00 GB
    Shell: 5.8 - /opt/homebrew/bin/zsh
  Binaries:
    Node: 16.14.0 - ~/Library/Caches/fnm_multishells/69747_1653603955297/bin/node
    Yarn: 3.2.1 - ~/Library/Caches/fnm_multishells/69747_1653603955297/bin/yarn
    npm: 8.3.1 - ~/Library/Caches/fnm_multishells/69747_1653603955297/bin/npm
    Watchman: 2022.06.27.00 - /opt/homebrew/bin/watchman
  Browsers:
    Brave Browser: 103.1.40.113
    Chrome: 102.0.5005.61
    Firefox: 100.0.2
    Safari: 15.5
    Safari Technology Preview: 16.0

Used Package Manager

yarn

Validations

natew avatar Jul 08 '22 06:07 natew

Can confirm this is something to do with the CLI output, using --reporter json doesn't produce the error.

natew avatar Jul 17 '22 23:07 natew

Same "unhandled error" on my Ubuntu 22.04.1 machine with pnpm. The test itself passes but it looks like there is a bug in the default reporter that makes the test suite fail:

~/projects/myproject$ pnpm test:unit:js:watch createError.test

> [email protected] test:unit:js:watch /home/laurent/projects/myproject
> FIRESTORE_EMULATOR_HOST=localhost:8080 vitest "createError.test"

[log]
[log]  DEV  v0.23.2 /home/laurent/projects/myproject/src
[log]
 ❯ ../tests/unit/Users/createError.test.ts (1)
   ⠙ [ beforeAll ]
   · throwError called after a batch commit error
[log]  ✓ ../tests/unit/Users/createError.test.ts (1) 353ms
[log]
[log] Test Files  1 passed (1)
[log]      Tests  1 passed (1)
[log]   Start at  11:06:01
[log]   Duration  1.19s (transform 335ms, setup 1ms, collect 210ms, tests 353ms)
[log]
[log] ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Errors ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
[log] 
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.
[error] 
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Rejection ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
[log]  ❯ home/laurent/projects/myproject/tests/unit/Users/createError.test.ts:7:74
[error] Error: TEST_WRITEBATCH_ERROR
[log]  ❯ _VitestMocker.callFunctionMock ../node_modules/.pnpm/[email protected]_fkbfcp6ijlbjqhnxhd6gffpwim/node_modules/vitest/dist/chunk-runtime-mocker.8d4a2494.mjs:79:21
[log]  ❯ _VitestMocker.requestWithMock ../node_modules/.pnpm/[email protected]_fkbfcp6ijlbjqhnxhd6gffpwim/node_modules/vitest/dist/chunk-runtime-mocker.8d4a2494.mjs:260:22
[log]  ❯ ../async /home/laurent/projects/myproject/tests/firestoreTesting.ts:5:31
[log]  ❯ VitestRunner.directRequest ../node_modules/.pnpm/[email protected]_fkbfcp6ijlbjqhnxhd6gffpwim/node_modules/vitest/dist/chunk-vite-node-client.da0a17ff.mjs:250:5
[log]  ❯ VitestRunner.cachedRequest ../node_modules/.pnpm/[email protected]_fkbfcp6ijlbjqhnxhd6gffpwim/node_modules/vitest/dist/chunk-vite-node-client.da0a17ff.mjs:127:12
[log]  ❯ _VitestMocker.request ../node_modules/.pnpm/[email protected]_fkbfcp6ijlbjqhnxhd6gffpwim/node_modules/vitest/dist/chunk-vite-node-client.da0a17ff.mjs:150:16
[log]  ❯ ../async /home/laurent/projects/myproject/src/home/laurent/projects/myproject/tests/unit/Users/createError.test.ts:12:31
[log]  ❯ VitestRunner.directRequest ../node_modules/.pnpm/[email protected]_fkbfcp6ijlbjqhnxhd6gffpwim/node_modules/vitest/dist/chunk-vite-node-client.da0a17ff.mjs:250:5
[log]  ❯ VitestRunner.cachedRequest ../node_modules/.pnpm/[email protected]_fkbfcp6ijlbjqhnxhd6gffpwim/node_modules/vitest/dist/chunk-vite-node-client.da0a17ff.mjs:127:12
[log]  ❯ _VitestMocker.request ../node_modules/.pnpm/[email protected]_fkbfcp6ijlbjqhnxhd6gffpwim/node_modules/vitest/dist/chunk-vite-node-client.da0a17ff.mjs:150:16
[log]  ❯ collectTests ../node_modules/.pnpm/[email protected]_fkbfcp6ijlbjqhnxhd6gffpwim/node_modules/vitest/dist/chunk-runtime-error.2723946b.mjs:339:9
[log]     337|         await importFromBrowser(filepath);
    338|       else
    339|         await import(filepath);
       |         ^
    340|       const defaultTasks = await defaultSuite.collect(file);
    341|       setHooks(file, getHooks(defaultTasks));
[log]  ❯ startTestsNode ../node_modules/.pnpm/[email protected]_fkbfcp6ijlbjqhnxhd6gffpwim/node_modules/vitest/dist/chunk-runtime-error.2723946b.mjs:768:17
[log]  ❯ ../async /home/laurent/projects/myproject/node_modules/.pnpm/[email protected]_fkbfcp6ijlbjqhnxhd6gffpwim/node_modules/vitest/dist/entry.mjs:83:9
[log]  ❯ Module.withEnv ../node_modules/.pnpm/[email protected]_fkbfcp6ijlbjqhnxhd6gffpwim/node_modules/vitest/dist/chunk-runtime-error.2723946b.mjs:282:5
[log]  ❯ run ../node_modules/.pnpm/[email protected]_fkbfcp6ijlbjqhnxhd6gffpwim/node_modules/vitest/dist/entry.mjs:54:5
[log]  ❯ ../async file:/home/laurent/projects/myproject/node_modules/.pnpm/[email protected]/node_modules/tinypool/dist/esm/worker.js:109:20
[log]
[log] ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
[log] 
 FAIL  Tests failed. Watching for file changes...
[log]        press h to show help, press q to quit
 ELIFECYCLE  Command failed with exit code 1.

As mentioned by @natew using --reporter json makes it work:

~/projects/myproject$ pnpm test:unit:js:watch createError.test --reporter json

> [email protected] test:unit:js:watch /home/laurent/projects/myproject
> FIRESTORE_EMULATOR_HOST=localhost:8080 vitest "createError.test" "--reporter" "json"

(node:25979) ExperimentalWarning: The Fetch API is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
(node:25979) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously (rejection id: 1)
[log] {
  "numTotalTestSuites": 1,
  "numPassedTestSuites": 1,
  "numFailedTestSuites": 0,
  "numPendingTestSuites": 0,
  "numTotalTests": 1,
  "numPassedTests": 1,
  "numFailedTests": 0,
  "numPendingTests": 0,
  "numTodoTests": 0,
  "startTime": 1663233046282,
  "success": true,
  "testResults": [
    {
      "assertionResults": [
        {
          "ancestorTitles": [
            ""
          ],
          "fullName": " throwError called after a batch commit error",
          "status": "passed",
          "title": "throwError called after a batch commit error",
          "duration": 18,
          "failureMessages": []
        }
      ],
      "startTime": 1663233047183,
      "endTime": 1663233047201,
      "status": "passed",
      "message": "",
      "name": "/home/laurent/projects/myproject/tests/unit/Users/createError.test.ts"
    }
  ]
}

laurentpayot avatar Sep 15 '22 09:09 laurentpayot

By the way skipping my test was not enough to prevent the suite from failing. I had to comment a vi.mock() (internally calling a vi.importActual()) not to fail. Hope it helps.

laurentpayot avatar Sep 15 '22 09:09 laurentpayot

UPDATE: Replacing

vi.fn().mockReturnValue(Promise.reject(new Error("TEST_ERROR")))

by

vi.fn().mockRejectedValue(new Error("TEST_ERROR"))

fixed the issue for me. Weird.

laurentpayot avatar Sep 19 '22 09:09 laurentpayot

The test itself passes but it looks like there is a bug in the default reporter that makes the test suite fail:

Vitest doesn't throw the error: TEST_WRITEBATCH_ERROR, you have some package that does it.

json reporter works, because it gives you errors inside a json 😄 And you need to process them yourself.

sheremet-va avatar Sep 19 '22 12:09 sheremet-va

Cannot reproduce with reproduction tests, I am getting:

failed to load config from /Users/sheremet/local-git/tamagui/packages/vite-plugin-internal/src/vite.config.ts

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Error ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
Error: Cannot find module '/Users/sheremet/local-git/tamagui/node_modules/@tamagui/vite-plugin/dist/cjs'. Please verify that the package.json has a valid "main" entry

sheremet-va avatar Sep 19 '22 12:09 sheremet-va

Hello @natew. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with need reproduction will be closed if they have no activity within 3 days.

github-actions[bot] avatar Sep 19 '22 12:09 github-actions[bot]

Vitest doesn't throw the error: TEST_WRITEBATCH_ERROR, you have some package that does it.

@sheremet-va my bad, I should have mentionned that TEST_WRITEBATCH_ERROR actually is a part of the mock I wrote (like in the example below for TEST_SETDOC_ERROR):

vi.mock('@firebase/firestore', async () => {
    const firestore: typeof import('@firebase/firestore') =
        await vi.importActual('@firebase/firestore')
    return {
        ...firestore,
        setDoc: vi.fn().mockRejectedValue(new Error("TEST_SETDOC_ERROR"))
    }
})

I’m testing the behavior of some functions when firestore.setDoc() they internally use returns whatever error. So the TEST_XXXXXXX_ERROR triggering is actually the normal behavior of the test :wink:

Once more, using the json reporter leads to a correct suite behavior. There must be a weird bug in the default reporter :thinking:

laurentpayot avatar Sep 20 '22 05:09 laurentpayot

Note that my example above does not produce the bug. It was when I was (incorrectly?) using the following:

vi.fn().mockReturnValue(Promise.reject(new Error("TEST_SETDOC_ERROR")))

laurentpayot avatar Sep 20 '22 05:09 laurentpayot

Still, I am not able to reproduce the error with reproduction steps that were provided.

sheremet-va avatar Sep 20 '22 06:09 sheremet-va