Skip to content

Commit db8b4c0

Browse files
committed
test: add print-diagnostics spec
- test all the different categories and formattings - full unit test coverage now! (except for `index`/`tscache`, which integration tests cover)
1 parent 7e316b8 commit db8b4c0

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

CONTRIBUTING.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ Use the [standard GitHub process](https://docs.github.com/en/pull-requests/colla
3131
1. `npm run test:coverage` to run tests and output a test coverage report
3232

3333
While this repo now has an assortment of unit tests and integration tests, it still needs more integration tests with various scenarios and expected outcomes.
34-
Test coverage improvements for existing files and untested files is needed as well.
3534

3635
### Building and Self-Build
3736

__tests__/print-diagnostics.spec.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { test, expect } from "@jest/globals";
2+
import * as ts from "typescript";
3+
import { red } from "colors/safe";
4+
5+
import { makeContext } from "./fixtures/context";
6+
import { setTypescriptModule } from "../src/tsproxy";
7+
import { printDiagnostics } from "../src/print-diagnostics";
8+
9+
setTypescriptModule(ts);
10+
11+
const diagnostic = {
12+
flatMessage: "Compiler option 'include' requires a value of type Array.",
13+
formatted: "\x1B[91merror\x1B[0m\x1B[90m TS5024: \x1B[0mCompiler option 'include' requires a value of type Array.\n",
14+
category: ts.DiagnosticCategory.Error,
15+
code: 5024,
16+
type: 'config'
17+
};
18+
19+
test("print-diagnostics - categories", () => {
20+
const context = makeContext();
21+
22+
printDiagnostics(context, [diagnostic]);
23+
expect(context.error).toHaveBeenLastCalledWith(diagnostic.formatted);
24+
25+
printDiagnostics(context, [{ ...diagnostic, category: ts.DiagnosticCategory.Warning } ]);
26+
expect(context.warn).toHaveBeenLastCalledWith(diagnostic.formatted);
27+
28+
printDiagnostics(context, [{ ...diagnostic, category: ts.DiagnosticCategory.Suggestion } ]); // default case
29+
expect(context.warn).toHaveBeenLastCalledWith(diagnostic.formatted);
30+
31+
printDiagnostics(context, [{ ...diagnostic, category: ts.DiagnosticCategory.Message } ]);
32+
expect(context.info).toHaveBeenLastCalledWith(diagnostic.formatted);
33+
34+
// should match exactly, no more
35+
expect(context.error).toBeCalledTimes(1);
36+
expect(context.warn).toBeCalledTimes(2)
37+
expect(context.info).toBeCalledTimes(1);
38+
expect(context.debug).toBeCalledTimes(0);
39+
});
40+
41+
test("print-diagnostics - formatting / style", () => {
42+
const context = makeContext();
43+
const category = "error"; // string version
44+
45+
printDiagnostics(context, [diagnostic], false);
46+
expect(context.error).toHaveBeenLastCalledWith(`${diagnostic.type} ${category} TS${diagnostic.code}: ${red(diagnostic.flatMessage)}`);
47+
48+
const fileLine = "0"
49+
printDiagnostics(context, [{ ...diagnostic, fileLine }], false);
50+
expect(context.error).toHaveBeenLastCalledWith(`${fileLine}: ${diagnostic.type} ${category} TS${diagnostic.code}: ${red(diagnostic.flatMessage)}`);
51+
52+
// should match exactly, no more
53+
expect(context.error).toBeCalledTimes(2);
54+
expect(context.warn).toBeCalledTimes(0)
55+
expect(context.info).toBeCalledTimes(0);
56+
expect(context.debug).toBeCalledTimes(0);
57+
});

0 commit comments

Comments
 (0)