|
| 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