🧪
vitest や jest で特定の警告メッセージを出力しない方法
方法
以下のように、セットアップ用のファイルでconsole.error
を上書きし、特定のメッセージの場合に何も処理を行わないようにすることで、警告メッセージが出力されなくなる。
vitest.setup.ts または jest.setup.ts
// 例:「Each child in a list should have a unique 'key' prop」を出力しないようにする
const originalError = console.error;
beforeAll(() => {
console.error = (...args: Parameters<typeof console.error>) => {
const message = args[0];
if (
typeof message === "string" &&
message.includes('Each child in a list should have a unique "key" prop')
) {
return;
}
originalError.call(console, ...args);
};
});
afterAll(() => {
console.error = originalError;
});
すべてのテストファイルでこの処理を適用したくない場合は、個別のテストファイルで、上記の処理を行う。
参考
Discussion