无法运行 Vitest 测试并出现错误:“Vitest 在测试运行期间捕获 1 个未处理的错误”

Tal*_*ofe 10 javascript testing typescript vitest

我有一个函数,我尝试使用 Vitest 进行测试。

\n

这是我的功能(我测试导出的功能):

\n
import 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/**\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
Run Code Online (Sandbox Code Playgroud)\n

这是asyncExec函数:

\n
import { exec } from \'node:child_process\';\nimport util from \'node:util\';\n\nexport const asyncExec = util.promisify(exec);\n
Run Code Online (Sandbox Code Playgroud)\n

我写了以下测试:

\n
import { 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
Run Code Online (Sandbox Code Playgroud)\n

但后来我得到了错误:

\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\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\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\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xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\xe2\x8e\xaf\n\xe2\x80\x89ELIFECYCLE\xe2\x80\x89 Test failed. See above for more details.\n
Run Code Online (Sandbox Code Playgroud)\n

我已经遵循了: https: //vitest.dev/api/expect.html#to throwerror

\n

所以我不明白为什么它不起作用..?

\n
\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
Run Code Online (Sandbox Code Playgroud)\n

错误是:

\n
AssertionError: expected [Function] to throw an error\n
Run Code Online (Sandbox Code Playgroud)\n
\n

然后我将getStagedFilesDiff函数包装try {} catch {}如下:

\n
export 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
Run Code Online (Sandbox Code Playgroud)\n

然后测试成功完成。

\n

Tal*_*ofe 6

我设法通过执行以下操作解决了这个问题:

        it('should throw an error when "asyncExec" throws', async () => {
            vi.mocked(asyncExec).mockRejectedValueOnce(new Error());

            await expect(() => getStagedFilesDiff()).rejects.toThrowError();
        });
Run Code Online (Sandbox Code Playgroud)