Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add print-diagnostics spec #405

Merged
merged 1 commit into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ Use the [standard GitHub process](https://docs.github.com/en/pull-requests/colla
1. `npm run test:coverage` to run tests and output a test coverage report

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.
Test coverage improvements for existing files and untested files is needed as well.

### Building and Self-Build

Expand Down
57 changes: 57 additions & 0 deletions __tests__/print-diagnostics.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { test, expect } from "@jest/globals";
import * as ts from "typescript";
import { red } from "colors/safe";

import { makeContext } from "./fixtures/context";
import { setTypescriptModule } from "../src/tsproxy";
import { printDiagnostics } from "../src/print-diagnostics";

setTypescriptModule(ts);

const diagnostic = {
flatMessage: "Compiler option 'include' requires a value of type Array.",
formatted: "\x1B[91merror\x1B[0m\x1B[90m TS5024: \x1B[0mCompiler option 'include' requires a value of type Array.\n",
category: ts.DiagnosticCategory.Error,
code: 5024,
type: 'config'
};

test("print-diagnostics - categories", () => {
const context = makeContext();

printDiagnostics(context, [diagnostic]);
expect(context.error).toHaveBeenLastCalledWith(diagnostic.formatted);

printDiagnostics(context, [{ ...diagnostic, category: ts.DiagnosticCategory.Warning } ]);
expect(context.warn).toHaveBeenLastCalledWith(diagnostic.formatted);

printDiagnostics(context, [{ ...diagnostic, category: ts.DiagnosticCategory.Suggestion } ]); // default case
expect(context.warn).toHaveBeenLastCalledWith(diagnostic.formatted);

printDiagnostics(context, [{ ...diagnostic, category: ts.DiagnosticCategory.Message } ]);
expect(context.info).toHaveBeenLastCalledWith(diagnostic.formatted);

// should match exactly, no more
expect(context.error).toBeCalledTimes(1);
expect(context.warn).toBeCalledTimes(2)
expect(context.info).toBeCalledTimes(1);
expect(context.debug).toBeCalledTimes(0);
});

test("print-diagnostics - formatting / style", () => {
const context = makeContext();
const category = "error"; // string version

printDiagnostics(context, [diagnostic], false);
expect(context.error).toHaveBeenLastCalledWith(`${diagnostic.type} ${category} TS${diagnostic.code}: ${red(diagnostic.flatMessage)}`);

const fileLine = "0"
printDiagnostics(context, [{ ...diagnostic, fileLine }], false);
expect(context.error).toHaveBeenLastCalledWith(`${fileLine}: ${diagnostic.type} ${category} TS${diagnostic.code}: ${red(diagnostic.flatMessage)}`);

// should match exactly, no more
expect(context.error).toBeCalledTimes(2);
expect(context.warn).toBeCalledTimes(0)
expect(context.info).toBeCalledTimes(0);
expect(context.debug).toBeCalledTimes(0);
});