-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathutils.ts
69 lines (53 loc) · 1.72 KB
/
utils.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { readFileSync } from 'fs';
import { resolve } from 'path';
import { vi, afterAll, afterEach } from 'vitest';
import {
compile as svelteCompile,
preprocess as sveltePreprocess,
} from 'svelte/compiler';
export const CSS_PATTERN =
/div(\.svelte-\w{4,7})?\s*\{\s*color:\s*(red|#f00);?\s*\}/;
export const getTestAppFilename = () => resolve(__dirname, 'App.svelte');
export const preprocess = async (input: string, opts: any) =>
sveltePreprocess(input, opts, { filename: getTestAppFilename() });
const compile = async (input: string, opts: any) => {
const preprocessed = await exports.preprocess(input, opts);
const { js, css } = svelteCompile(preprocessed.toString?.(), {
css: 'injected',
});
return { js, css };
};
export const doesCompileThrow = async (input: string, opts: any) => {
let didThrow = false;
try {
await compile(input, opts);
} catch (err) {
didThrow = true;
}
return didThrow;
};
export const getFixturePath = (file: string) =>
resolve(__dirname, 'fixtures', file);
export const getFixtureContent = (file: string) =>
readFileSync(exports.getFixturePath(file)).toString().trim();
export function spyConsole({ silent = true } = {}) {
const warnSpy = vi.spyOn(global.console, 'warn');
const errorSpy = vi.spyOn(global.console, 'error');
const logSpy = vi.spyOn(global.console, 'log');
if (silent) {
warnSpy.mockImplementation(() => {});
errorSpy.mockImplementation(() => {});
logSpy.mockImplementation(() => {});
}
afterAll(() => {
warnSpy.mockRestore();
errorSpy.mockRestore();
logSpy.mockRestore();
});
afterEach(() => {
warnSpy.mockClear();
errorSpy.mockClear();
logSpy.mockClear();
});
return { warnSpy, errorSpy, logSpy };
}