-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathformatter.test.ts
More file actions
78 lines (68 loc) · 2.05 KB
/
formatter.test.ts
File metadata and controls
78 lines (68 loc) · 2.05 KB
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
70
71
72
73
74
75
76
77
78
import dedent from 'dedent';
import ts from 'typescript';
import { describe, expect, test } from 'vitest';
import { formatDiagnostics, formatTime } from './formatter.js';
describe('formatDiagnostics', () => {
const file = ts.createSourceFile(
'/app/test.module.css',
dedent`
.a_1 { color: red; }
.a_2 { color: red; }
`,
ts.ScriptTarget.JSON,
undefined,
ts.ScriptKind.Unknown,
);
const host: ts.FormatDiagnosticsHost = {
getCurrentDirectory: () => '/app',
getCanonicalFileName: (fileName) => fileName,
getNewLine: () => '\n',
};
const diagnostics: ts.Diagnostic[] = [
{
file,
start: 1,
length: 3,
messageText: '`a_1` is not allowed',
category: ts.DiagnosticCategory.Error,
code: 0,
},
{
file,
start: 22,
length: 3,
messageText: '`a_2` is not allowed',
category: ts.DiagnosticCategory.Error,
code: 0,
},
];
test('formats diagnostics with color and context when pretty is true', () => {
const result = formatDiagnostics(diagnostics, host, true);
expect(result).toMatchInlineSnapshot(`
"[96mtest.module.css[0m:[93m1[0m:[93m2[0m - [91merror[0m[90m: [0m\`a_1\` is not allowed
[7m1[0m .a_1 { color: red; }
[7m [0m [91m ~~~[0m
[96mtest.module.css[0m:[93m2[0m:[93m2[0m - [91merror[0m[90m: [0m\`a_2\` is not allowed
[7m2[0m .a_2 { color: red; }
[7m [0m [91m ~~~[0m
"
`);
});
test('formats diagnostics without color and context when pretty is false', () => {
const result = formatDiagnostics(diagnostics, host, false);
expect(result).toMatchInlineSnapshot(`
"test.module.css(1,2): error: \`a_1\` is not allowed
test.module.css(2,2): error: \`a_2\` is not allowed
"
`);
});
});
test('formatTime', () => {
const date = new Date('2023-01-01T00:00:00Z');
expect(formatTime(date, true)).toMatchInlineSnapshot(`
"[[90m12:00:00 AM[0m]"
`);
expect(formatTime(date, false)).toMatchInlineSnapshot(`
"[12:00:00 AM]"
`);
});