-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.vitest.ts
54 lines (48 loc) · 1.63 KB
/
setup.vitest.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import '@testing-library/jest-dom/extend-expect';
import { configure } from '@testing-library/react';
import { expect, vi } from 'vitest';
// Disable runtime css
import '@vanilla-extract/css/disableRuntimeStyles';
// Configuration:
// https://testing-library.com/docs/dom-testing-library/api-configuration
configure({
// Disable hidden check for better perf
// https://github.com/testing-library/dom-testing-library/issues/552#issuecomment-626718645
defaultHidden: true,
testIdAttribute: 'data-test-id',
// Don't log dom snapshot
// https://stackoverflow.com/a/64155473/1845423
getElementError(message) {
if (!message) return new Error('No error message provided');
const error = new Error(message);
error.name = 'TestingLibraryElementError';
error.stack = undefined;
return error;
},
});
vi.stubGlobal('fetch', () => ({
json: vi.fn(),
}));
vi.stubGlobal('location', {
pathname: '/',
});
// Fail if console logs
// When not running in watch or coverage mode
if (process.env.VITEST_MODE !== 'WATCH' && process.env.npm_lifecycle_event !== 'coverage') {
const { log } = console;
const reportError = (msg: string, type: 'log' | 'warn' | 'error') => {
log(msg);
const err = new Error(`console.${type} was called`);
expect.fail(
(err.stack as string)
.replace(/(.+setup\.vitest\.ts.+\n+)/g, '')
.replace(/.+file:\/\/\/.+\n/g, '')
.replace(/.+\/node_modules\/.+\n?/g, '')
);
};
vi.stubGlobal('console', {
log: (msg: string) => reportError(msg, 'log'),
warn: (msg: string) => reportError(msg, 'warn'),
error: (msg: string) => reportError(msg, 'error'),
});
}