/
githubmirror
/
docusaurus
Обзор
Документация
Войти
/
githubmirror
/
docusaurus
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
test/snapshotErrorCause.ts
46 строк
1 KB
Sébastien Lorber
chore: migration from Jest to Vitest (#12010)
14 май 2026, 18:34
Не верифицирован
14 май 2026, 18:34
be553e0
Код
Авторство
О чём код?
/** * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ import type {SnapshotSerializer} from 'vitest'; function getErrorName(error: Error): string { return error.name !== 'Error' ? error.name : (typeof error.constructor === 'function' && error.constructor.name) || 'Object'; } function serializeCausalChain(e: Error): string { let error = e; let message = ''; while ('cause' in error) { error = error.cause as Error; if (error instanceof Error) { message += `\nCause: [${getErrorName(error)}: ${error.message}]`; } else { if (typeof error === 'string') { message += `\nCause: [Error: ${error}]`; } break; } } return message; } // Because Vitest doesn't Snapshot Error.cause automatically // see https://github.com/vitest-dev/vitest/issues/10339 const snapshotSerializer: SnapshotSerializer = { test: (val: unknown): boolean => { return !!((val as Error)?.cause && (val as Error)?.cause instanceof Error); }, serialize(error: Error) { return `[${getErrorName(error)}: ${error.message}]${serializeCausalChain(error)}`; }, }; export default snapshotSerializer;