From 4e5ab742ea5ed4acccdb24b0169930a4694f1d7f Mon Sep 17 00:00:00 2001 From: Anton Gilgur Date: Fri, 19 Aug 2022 17:00:41 -0400 Subject: [PATCH] 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 --- __tests__/tslib.spec.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/__tests__/tslib.spec.ts b/__tests__/tslib.spec.ts index 4b5489d7..b10d35f4 100644 --- a/__tests__/tslib.spec.ts +++ b/__tests__/tslib.spec.ts @@ -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");