我碰巧只是弄清楚了这一点,当我关闭所有选项卡寻找其他人调查过这个时,我看到了你的帖子。
无论如何你可以结帐:
https://github.com/vitest-dev/vitest/tree/main/test/global-setup
在测试出哪些可行之后,下面是我如何为我正在做的测试添加 BigInt 序列化:
// vite.config.js
export default defineConfig({
test: {
// ...
setupFiles: ['./src/utils/setup-teardown-hooks.js'],
// ...
},
});
进而:
// setup-teardown-hook.js
import { afterAll, beforeAll } from 'vitest';
beforeAll(() => {
global.BigInt.prototype.toJSON = function () {
return this.toString();
};
// or from the the original link's example
global.something = 'something';
// or
global.lol = '?';
});
换句话说,向global 添加内容似乎无法通过globalSetup 配置字段,而是通过setupFiles 字段。 beforeAll 挂钩在每个单独的测试文件/“套件”的开头被调用。