Tal*_*ofe 10 javascript testing typescript vitest
我有一个函数,我尝试使用 Vitest 进行测试。
\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/**\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};\nRun Code Online (Sandbox Code Playgroud)\n这是asyncExec函数:
import { exec } from \'node:child_process\';\nimport util from \'node:util\';\n\nexport const asyncExec = util.promisify(exec);\nRun Code Online (Sandbox Code Playgroud)\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});\nRun 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.\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LE\xe2\x80\x89 Test failed. See above for more details.\nRun Code Online (Sandbox Code Playgroud)\n我已经遵循了: https: //vitest.dev/api/expect.html#to throwerror
\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 });\nRun Code Online (Sandbox Code Playgroud)\n错误是:
\nAssertionError: expected [Function] to throw an error\nRun Code Online (Sandbox Code Playgroud)\n然后我将getStagedFilesDiff函数包装try {} catch {}如下:
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};\nRun Code Online (Sandbox Code Playgroud)\n然后测试成功完成。
\n我设法通过执行以下操作解决了这个问题:
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)
| 归档时间: |
|
| 查看次数: |
6864 次 |
| 最近记录: |