From e8240ae505a6c4d0b7faa997e3c2d75d293c594a Mon Sep 17 00:00:00 2001 From: Anton Gilgur Date: Thu, 26 May 2022 11:51:56 -0400 Subject: [PATCH] test: 100% coverage in `get-options-overrides` (`createFilter`) (#329) * test: 100% coverage in get-options-overrides (createFilter) - get the skipped test to work by using absolute paths, as that is what the `filter` function expects (and is how it is used in this codebase) - use the same helper func on the main `createFilter` test as well to ensure that we're consistently testing the same paths - (though `**/*` basically matches _everything_) - add a test for project references as well - and remove the `**/*` from the include for this so it doesn't match everything - this also tests the single string include code path as well - add a test for the `context.debug()` statements as well - couldn't get to 100% Funcs coverage without this - used a simplified mock instead of actually using `RollupContext` so that this doesn't rely on that file/unit to work * quick fix for Windows? --- __tests__/get-options-overrides.spec.ts | 68 ++++++++++++++++++++----- 1 file changed, 56 insertions(+), 12 deletions(-) diff --git a/__tests__/get-options-overrides.spec.ts b/__tests__/get-options-overrides.spec.ts index fbd736c..65f29a5 100644 --- a/__tests__/get-options-overrides.spec.ts +++ b/__tests__/get-options-overrides.spec.ts @@ -1,6 +1,7 @@ -import { afterAll, test, expect } from "@jest/globals"; +import { afterAll, test, expect, jest } from "@jest/globals"; import * as path from "path"; import * as ts from "typescript"; +import { normalizePath as normalize } from "@rollup/pluginutils"; import { remove } from "fs-extra"; import { makeStubbedContext } from "./fixtures/context"; @@ -13,6 +14,9 @@ setTypescriptModule(ts); const local = (x: string) => path.resolve(__dirname, x); const cacheDir = local("__temp/get-options-overrides"); +// filter expects an absolute path and resolves include/exclude to process.cwd() by default: https://github.com/ezolenko/rollup-plugin-typescript2/pull/321#discussion_r873077874 +const filtPath = (relPath: string) => normalize(`${process.cwd()}/${relPath}`); + afterAll(() => remove(cacheDir)); const defaultConfig: IOptions = { @@ -124,13 +128,25 @@ test("createFilter", () => { const stubbedContext = makeStubbedContext({}); const filter = createFilter(stubbedContext, config, preParsedTsConfig); - expect(filter("src/test.ts")).toBe(true); - expect(filter("src/test.js")).toBe(false); - expect(filter("src/test.d.ts")).toBe(false); + expect(filter(filtPath("src/test.ts"))).toBe(true); + expect(filter(filtPath("src/test.js"))).toBe(false); + expect(filter(filtPath("src/test.d.ts"))).toBe(false); }); -// not totally sure why this is failing -test.skip("createFilter -- rootDirs", () => { +test("createFilter - context.debug", () => { + const config = { ...defaultConfig }; + const preParsedTsConfig = { ...defaultPreParsedTsConfig }; + + // test context.debug() statements + const debug = jest.fn(x => x()); + const data = { set debug(x: any) { debug(x) } }; + const stubbedContext = makeStubbedContext(data); + + createFilter(stubbedContext, config, preParsedTsConfig); + expect(debug.mock.calls.length).toBe(2); +}); + +test("createFilter - rootDirs", () => { const config = { ...defaultConfig }; const preParsedTsConfig = { ...defaultPreParsedTsConfig, @@ -142,10 +158,38 @@ test.skip("createFilter -- rootDirs", () => { const stubbedContext = makeStubbedContext({}); const filter = createFilter(stubbedContext, config, preParsedTsConfig); - expect(filter("src/test.ts")).toBe(true); - expect(filter("src/test.js")).toBe(false); - expect(filter("src/test.d.ts")).toBe(false); - expect(filter("lib/test.ts")).toBe(true); - expect(filter("lib/test.js")).toBe(false); - expect(filter("lib/test.d.ts")).toBe(false); + expect(filter(filtPath("src/test.ts"))).toBe(true); + expect(filter(filtPath("src/test.js"))).toBe(false); + expect(filter(filtPath("src/test.d.ts"))).toBe(false); + + expect(filter(filtPath("lib/test.ts"))).toBe(true); + expect(filter(filtPath("lib/test.js"))).toBe(false); + expect(filter(filtPath("lib/test.d.ts"))).toBe(false); + + expect(filter(filtPath("not-src/test.ts"))).toBe(false); +}); + +test("createFilter - projectReferences", () => { + // test string include and also don't match with "**" + const config = { ...defaultConfig, include: "*.ts+(|x)" }; + const preParsedTsConfig = { + ...defaultPreParsedTsConfig, + projectReferences: [ + { path: "src" }, + { path: "lib" }, + ], + }; + + const stubbedContext = makeStubbedContext({}); + const filter = createFilter(stubbedContext, config, preParsedTsConfig); + + expect(filter(filtPath("src/test.ts"))).toBe(true); + expect(filter(filtPath("src/test.js"))).toBe(false); + expect(filter(filtPath("src/test.d.ts"))).toBe(false); + + expect(filter(filtPath("lib/test.ts"))).toBe(true); + expect(filter(filtPath("lib/test.js"))).toBe(false); + expect(filter(filtPath("lib/test.d.ts"))).toBe(false); + + expect(filter(filtPath("not-src/test.ts"))).toBe(false); });