Skip to content

Commit

Permalink
test: 100% coverage in get-options-overrides (createFilter) (#329)
Browse files Browse the repository at this point in the history
* 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?
  • Loading branch information
agilgur5 authored May 26, 2022
1 parent 197061b commit e8240ae
Showing 1 changed file with 56 additions and 12 deletions.
68 changes: 56 additions & 12 deletions __tests__/get-options-overrides.spec.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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 = {
Expand Down Expand Up @@ -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);
});

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);
});

// not totally sure why this is failing
test.skip("createFilter -- rootDirs", () => {
test("createFilter - rootDirs", () => {
const config = { ...defaultConfig };
const preParsedTsConfig = {
...defaultPreParsedTsConfig,
Expand All @@ -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);
});

0 comments on commit e8240ae

Please sign in to comment.