Skip to content

Commit

Permalink
test: add unit tests for createFilter
Browse files Browse the repository at this point in the history
- increases coverage of get-options-overrides significantly
- couldn't figure out `rootDirs` -- either the code or my tests are
  wrong, so just skip that one for now

- refactor: move makeStubbedContext etc into a shared fixtures dir so
  that it can be used for both rollupcontext tests _and_
  createFilter tests
  - in my stylistic opinion, I prefer to put nearly _all_ consts like
    these into fixtures dir
  - configure jest to ignore test helpers in coverage reporting such as
    fixture files

- format: '' -> "", add semicolon in some places
  - I use single quotes and no semicolons, so missed that in a few
    places
    - lint didn't check for that and no prettier auto-format :/
  • Loading branch information
agilgur5 committed May 9, 2022
1 parent 3b8fe22 commit c90c930
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 55 deletions.
51 changes: 51 additions & 0 deletions __tests__/fixtures/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { PluginContext } from "rollup";

import { IContext } from "../../src/context";

const stub = (x: any) => x;

const contextualLogger = (data: any): IContext => {
return {
warn: (x: any) => {
data.warn = x;
},
error: (x: any) => {
data.error = x;
},
info: (x: any) => {
data.info = x;
},
debug: (x: any) => {
data.debug = x;
},
};
};

export function makeStubbedContext (data: any): PluginContext & IContext {
const { warn, error, info, debug } = contextualLogger(data);
return {
addWatchFile: stub as any,
getWatchFiles: stub as any,
cache: stub as any,
load: stub as any,
resolve: stub as any,
resolveId: stub as any,
isExternal: stub as any,
meta: stub as any,
emitAsset: stub as any,
emitChunk: stub as any,
emitFile: stub as any,
setAssetSource: stub as any,
getAssetFileName: stub as any,
getChunkFileName: stub as any,
getFileName: stub as any,
parse: stub as any,
warn: warn as any,
error: error as any,
info: info as any,
debug: debug as any,
moduleIds: stub as any,
getModuleIds: stub as any,
getModuleInfo: stub as any
};
};
42 changes: 38 additions & 4 deletions __tests__/get-options-overrides.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import * as path from "path";
import * as ts from "typescript";
import { remove } from "fs-extra";

import { makeStubbedContext } from "./fixtures/context";
import { IOptions } from "../src/ioptions";
import { getOptionsOverrides } from "../src/get-options-overrides";
import { getOptionsOverrides, createFilter } from "../src/get-options-overrides";

const local = (x: string) => path.resolve(__dirname, x);

Expand All @@ -21,8 +22,8 @@ const normalizePaths = (props: string[], x: any) => {
};

const defaultConfig: IOptions = {
include: [],
exclude: [],
include: ["*.ts+(|x)", "**/*.ts+(|x)"],
exclude: ["*.d.ts", "**/*.d.ts"],
check: false,
verbosity: 5,
clean: false,
Expand Down Expand Up @@ -113,7 +114,7 @@ test("getOptionsOverrides - with declaration", () => {
});

test("getOptionsOverrides - with sourceMap", () => {
const config = { ...defaultConfig }
const config = { ...defaultConfig };
const preParsedTsConfig = {
...defaultPreParsedTsConfig,
options: {
Expand All @@ -128,3 +129,36 @@ test("getOptionsOverrides - with sourceMap", () => {
},
);
});

test("createFilter", () => {
const config = { ...defaultConfig };
const preParsedTsConfig = { ...defaultPreParsedTsConfig };

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

// not totally sure why this is failing
test.skip("createFilter -- rootDirs", () => {
const config = { ...defaultConfig };
const preParsedTsConfig = {
...defaultPreParsedTsConfig,
options: {
rootDirs: ["src", "lib"]
},
};

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);
});
49 changes: 1 addition & 48 deletions __tests__/rollupcontext.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { jest, test, expect } from "@jest/globals";
import { PluginContext } from "rollup";

import { IContext } from "../src/context";
import { makeStubbedContext } from "./fixtures/context";
import { RollupContext } from "../src/rollupcontext";

(global as any).console = {
Expand All @@ -10,52 +9,6 @@ import { RollupContext } from "../src/rollupcontext";
info: jest.fn(),
};

const stub = (x: any) => x;
const contextualLogger = (data: any): IContext => {
return {
warn: (x: any) => {
data.warn = x;
},
error: (x: any) => {
data.error = x;
},
info: (x: any) => {
data.info = x;
},
debug: (x: any) => {
data.debug = x;
},
};
};
const makeStubbedContext = (data: any): PluginContext & IContext => {
const { warn, error, info, debug } = contextualLogger(data);
return {
addWatchFile: stub as any,
getWatchFiles: stub as any,
cache: stub as any,
load: stub as any,
resolve: stub as any,
resolveId: stub as any,
isExternal: stub as any,
meta: stub as any,
emitAsset: stub as any,
emitChunk: stub as any,
emitFile: stub as any,
setAssetSource: stub as any,
getAssetFileName: stub as any,
getChunkFileName: stub as any,
getFileName: stub as any,
parse: stub as any,
warn: warn as any,
error: error as any,
info: info as any,
debug: debug as any,
moduleIds: stub as any,
getModuleIds: stub as any,
getModuleInfo: stub as any
};
};

test("RollupContext", () => {
const data = {};
const stubbedContext = makeStubbedContext(data);
Expand Down
10 changes: 7 additions & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
/** @type {import('@jest/types').Config.InitialOptions} */
/** @type {import("@jest/types").Config.InitialOptions} */
const config = {
preset: 'ts-jest',
preset: "ts-jest",
injectGlobals: false, // use @jest/globals instead
restoreMocks: true,
// only use *.spec.ts files in __tests__, no auto-generated files
testMatch: ["**/__tests__/**/*.spec.ts?(x)"]
testMatch: ["**/__tests__/**/*.spec.ts?(x)"],
coveragePathIgnorePatterns: [
"node_modules", // default
"<rootDir>/__tests__/" // ignore any test helper files
],
};

module.exports = config;

0 comments on commit c90c930

Please sign in to comment.