{"id":672,"hash":"5481c53dfb702cb40ecf06e4214e9f911de3e4fd45565446d34b580ea5c064d9","pattern":"Fail to run Vitest test with an error: &quot;Vitest caught 1 unhandled error during the test run&quot;","full_message":"I have a function which I try to test using Vitest.\n\nThis is my function (I test the exported one):\n\nimport os from 'node:os';\n\nimport { AI_COMMIT_IGNORED_FILES } from '../constants/ai-commit';\nimport { asyncExec } from './os';\n\n/**\n * The function returns array of files paths that are in staged mode\n * @returns array of files paths in staged mode\n */\nconst getStagedFiles = async () => {\n    const gitCommand = 'git diff --name-only --cached --relative .';\n    const { stdout: filesOutput, stderr } = await asyncExec(gitCommand);\n\n    if (stderr) {\n        throw new Error(stderr);\n    }\n\n    const filesList = filesOutput.split(os.EOL).filter(Boolean);\n\n    return filesList;\n};\n\n/**\n * The function returns the \"git diff\" command output for relevant staged files in the commit\n * @returns \"git diff\" output as a string\n */\nexport const getStagedFilesDiff = async () => {\n    const stagedFiles = await getStagedFiles();\n    const filteredFiles = stagedFiles.filter((file) => !AI_COMMIT_IGNORED_FILES.includes(file));\n\n    const gitCommand = `git diff --staged -- ${filteredFiles.join(' ')}`;\n    const { stdout: diffOutput, stderr } = await asyncExec(gitCommand);\n\n    if (stderr) {\n        throw new Error(stderr);\n    }\n\n    return diffOutput;\n};\n\nAnd this is the asyncExec function:\n\nimport { exec } from 'node:child_process';\nimport util from 'node:util';\n\nexport const asyncExec = util.promisify(exec);\n\nI wrote the following test:\n\nimport { describe, it, expect, vi } from 'vitest';\n\nimport { asyncExec } from '@/utils/os';\nimport { getStagedFilesDiff } from '@/utils/git-info';\n\nvi.mock('@/utils/os');\n\ndescribe('[utils/git-info]', () => {\n    describe('getStagedFilesDiff()', () => {\n        it('should throw an error when \"asyncExec\" throws', () => {\n            vi.mocked(asyncExec).mockRejectedValueOnce(undefined);\n\n            expect(() => getStagedFilesDiff()).toThrowError();\n        });\n    });\n});\n\nBut then I got the error:\n\nVitest caught 1 unhandled error during the test run.\nThis might cause false positive tests. Resolve unhandled errors to make sure your tests are not affected.\n\nThis error originated in \"tests/utils/git-info.spec.ts\" test file. It doesn't mean the error was thrown inside the file itself, but while it was running.\n⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯\n ELIFECYCLE  Test failed. See above for more details.\n\nI have followed this: https://vitest.dev/api/expect.html#tothrowerror\n\nSo I don't understand why doesn't it work..?\n\nI have also tried:\n\n        it('should throw an error when \"asyncExec\" throws', async () => {\n            vi.mocked(asyncExec).mockRejectedValueOnce(undefined);\n\n            await expect(() => getStagedFilesDiff()).rejects.toThrowError(undefined);\n        });\n\nthe error is:\n\nAssertionError: expected [Function] to throw an error\n\nThen I wrapped the getStagedFilesDiff function with try {} catch {} as follows:\n\nexport const getStagedFilesDiff = async () => {\n    try {\n        const stagedFiles = await getStagedFiles();\n        const filteredFiles = stagedFiles.filter((file) => !AI_COMMIT_IGNORED_FILES.includes(file));\n\n        if (filteredFiles.length === 0) {\n            return null;\n        }\n\n        const gitCommand = `git diff --staged -- ${filteredFiles.join(' ')}`;\n        const { stdout: diffOutput, stderr } = await asyncExec(gitCommand);\n\n        if (stderr) {\n            throw new Error(`Failed to get \"git diff\" output of staged files with error:\\n${stderr}`);\n        }\n\n        return diffOutput;\n    } catch {\n        throw new Error();\n    }\n};\n\nThen the test completed successfully.","ecosystem":"npm","package_name":"typescript","package_version":null,"solution":"I managed to resolve this issue by doing:\n\n        it('should throw an error when \"asyncExec\" throws', async () => {\n            vi.mocked(asyncExec).mockRejectedValueOnce(new Error());\n\n            await expect(() => getStagedFilesDiff()).rejects.toThrowError();\n        });","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/76071613/fail-to-run-vitest-test-with-an-error-vitest-caught-1-unhandled-error-during-t","votes":15,"created_at":"2026-04-19T04:51:28.852332+00:00","updated_at":"2026-04-19T04:51:28.852332+00:00"}