Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: 100% coverage in get-options-overrides (createFilter) #329

Merged
merged 2 commits into from
May 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}`);
agilgur5 marked this conversation as resolved.
Show resolved Hide resolved

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