Skip to content

Commit

Permalink
test: 100% coverage for tslib.ts (error case) (#399)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
agilgur5 authored Aug 19, 2022
1 parent 576558e commit 4e5ab74
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions __tests__/tslib.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import { test, expect } from "@jest/globals";
import { test, expect, jest } from "@jest/globals";
import * as fs from "fs-extra";

import { tslibVersion, tslibSource } from "../src/tslib";
(global as any).console = { warn: jest.fn() };

// the error case _must_ come first because order matters for module mocks
test("tslib - errors", async () => {
jest.mock("tslib/package.json", () => undefined); // mock the module subpath bc we actually never import "tslib" directly. module mocks only work on exact match
await expect(import("../src/tslib")).rejects.toThrow();
expect(console.warn).toBeCalledTimes(1);
});

test("tslib", async () => {
jest.unmock("tslib/package.json");

const { tslibVersion, tslibSource } = await import("../src/tslib");
expect(tslibVersion).toEqual(require("tslib/package.json").version);

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

0 comments on commit 4e5ab74

Please sign in to comment.