Skip to content

Commit 4e5ab74

Browse files
authored
test: 100% coverage for tslib.ts (error case) (#399)
- use Jest's module mocks to test the error case of `tslib.ts` when `tslib` fails to import - this took a bit of work to figure out bc, per the in-line comments, the ordering and module subpath were both _required_ - it was pretty confusing for a while until I realized what might be going wrong
1 parent 576558e commit 4e5ab74

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

__tests__/tslib.spec.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1-
import { test, expect } from "@jest/globals";
1+
import { test, expect, jest } from "@jest/globals";
22
import * as fs from "fs-extra";
33

4-
import { tslibVersion, tslibSource } from "../src/tslib";
4+
(global as any).console = { warn: jest.fn() };
5+
6+
// the error case _must_ come first because order matters for module mocks
7+
test("tslib - errors", async () => {
8+
jest.mock("tslib/package.json", () => undefined); // mock the module subpath bc we actually never import "tslib" directly. module mocks only work on exact match
9+
await expect(import("../src/tslib")).rejects.toThrow();
10+
expect(console.warn).toBeCalledTimes(1);
11+
});
512

613
test("tslib", async () => {
14+
jest.unmock("tslib/package.json");
15+
16+
const { tslibVersion, tslibSource } = await import("../src/tslib");
717
expect(tslibVersion).toEqual(require("tslib/package.json").version);
818

919
const tslibES6 = await fs.readFile(require.resolve("tslib/tslib.es6.js"), "utf8");

0 commit comments

Comments
 (0)