codecheck-type-coverage
Version:
Track missing type coverage in TypeScript projects to ensure type safety
152 lines (151 loc) • 4.47 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = require("../index");
const client_1 = require("@codechecks/client");
describe("type-coverage", () => {
const codeChecksMock = require("../__mocks__/@codechecks/client").codeChecks;
const typeCoverageMock = require("../__mocks__/type-coverage").lint;
beforeEach(() => jest.resetAllMocks());
it("should work not in PR context", async () => {
codeChecksMock.isPr.mockReturnValue(false);
typeCoverageMock.mockReturnValue({
correctCount: 2,
totalCount: 2,
anys: [],
program: undefined,
});
await index_1.typeCoverageWatcher({ tsconfigPath: "./tsconfig.json" });
expect(client_1.codeChecks.report).toBeCalledTimes(0);
expect(client_1.codeChecks.saveValue).toMatchInlineSnapshot(`
[MockFunction] {
"calls": Array [
Array [
"type-coverage",
Object {
"allUntypedSymbols": Array [],
"totalSymbols": 2,
"typedSymbols": 2,
},
],
],
"results": Array [
Object {
"isThrow": false,
"value": undefined,
},
],
}
`);
});
it("should work in PR context", async () => {
codeChecksMock.isPr.mockReturnValue(true);
codeChecksMock.getValue.mockReturnValue({
typedSymbols: 2,
totalSymbols: 4,
allUntypedSymbols: [
{ filename: "index.ts", character: 1, line: 10, symbol: "app" },
{ filename: "index.ts", character: 1, line: 15, symbol: "res" },
{ filename: "index.ts", character: 2, line: 15, symbol: "key" },
],
});
typeCoverageMock.mockReturnValue({
correctCount: 3,
totalCount: 4,
anys: [
{ file: "index.ts", character: 1, line: 15, text: "res" },
{ file: "index.ts", character: 3, line: 16, text: "key" },
],
program: undefined,
});
await index_1.typeCoverageWatcher({ tsconfigPath: "./tsconfig.json" });
expect(client_1.codeChecks.report).toMatchInlineSnapshot(`
[MockFunction] {
"calls": Array [
Array [
Object {
"longDescription": "New untyped symbols: 0",
"name": "Type Coverage",
"shortDescription": "Change: +25.00% Total: 75.00%",
"status": "success",
},
],
],
"results": Array [
Object {
"isThrow": false,
"value": undefined,
},
],
}
`);
});
it("should work in PR context 2", async () => {
codeChecksMock.isPr.mockReturnValue(true);
codeChecksMock.getValue.mockReturnValue({
typedSymbols: 4,
totalSymbols: 5,
allUntypedSymbols: [],
});
typeCoverageMock.mockReturnValue({
correctCount: 3,
totalCount: 4,
anys: [{ file: "index.ts", character: 1, line: 15, text: "res" }],
program: undefined,
});
await index_1.typeCoverageWatcher({ tsconfigPath: "./tsconfig.json" });
expect(client_1.codeChecks.report).toMatchInlineSnapshot(`
[MockFunction] {
"calls": Array [
Array [
Object {
"longDescription": "New untyped symbols: 1
| File | line:character | Symbol |
|:-----:|:-----:|:-----:|
| index.ts | 15:1 | res |",
"name": "Type Coverage",
"shortDescription": "Change: -5.00% Total: 75.00%",
"status": "success",
},
],
],
"results": Array [
Object {
"isThrow": false,
"value": undefined,
},
],
}
`);
});
it("should work in PR context without baseline", async () => {
codeChecksMock.isPr.mockReturnValue(true);
typeCoverageMock.mockReturnValue({
correctCount: 2,
totalCount: 2,
anys: [],
program: undefined,
});
await index_1.typeCoverageWatcher({ tsconfigPath: "./tsconfig.json" });
expect(client_1.codeChecks.report).toMatchInlineSnapshot(`
[MockFunction] {
"calls": Array [
Array [
Object {
"longDescription": "New untyped symbols: 0",
"name": "Type Coverage",
"shortDescription": "Change: +100.00% Total: 100.00%",
"status": "success",
},
],
],
"results": Array [
Object {
"isThrow": false,
"value": undefined,
},
],
}
`);
});
});
//# sourceMappingURL=index.spec.js.map