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: add initial unit test suite #321

Merged
merged 29 commits into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
aa04d77
some unit tests
brekk Jan 26, 2019
b217694
more
brekk Jan 27, 2019
ceae7fa
fix tests for windows
Apr 23, 2019
c3cfe41
flip that test
brekk Apr 23, 2019
b73370a
deps: upgrade Jest to v28
agilgur5 May 3, 2022
6719c0c
refactor: split jest config to jest.config.js
agilgur5 May 3, 2022
3c9b59c
clean: remove redundant jest config
agilgur5 May 3, 2022
08637a4
refactor: move tests into __tests__ dir
agilgur5 May 3, 2022
c2b9ffc
deps: import from @jest/globals instead of using globals
agilgur5 May 3, 2022
e9ddccc
fix(test): update get-options-overrides spec to match latest file
agilgur5 May 3, 2022
1c9b1cb
refactor(test): use obj spread in get-options-overrides
agilgur5 May 4, 2022
0049f16
fix(test): update host spec to match latest source
agilgur5 May 4, 2022
c197219
refactor(test): use async/await, remove truncate func in host spec
agilgur5 May 4, 2022
7d9e362
refactor: use consts in host spec instead of locals
agilgur5 May 4, 2022
30e5b32
fix(test): update rollupcontext spec to match latest source
agilgur5 May 7, 2022
20f66e9
lint: add __tests__ to lint dirs, fix lint errors
agilgur5 May 7, 2022
4dfb8ce
ci: add unit tests to matrix
agilgur5 May 7, 2022
dc1895f
test: add unit tests for createFilter
agilgur5 May 8, 2022
335b4bf
refactor: use consts, async/await, etc in rollingcache spec
agilgur5 May 8, 2022
bcce6d6
docs: add a testing section to CONTRIBUTING.md
agilgur5 May 9, 2022
d31a3fa
fix(test): undo tsTypes -> tsModules changes in source code
agilgur5 May 9, 2022
bf4419e
test: get host spec customTransformers test working
agilgur5 May 9, 2022
5e1b246
ci: add Node 14, 16, 18, and macOS, Windows to matrix
agilgur5 May 9, 2022
6584949
refactor: use __temp dir in options-overrides spec
agilgur5 May 9, 2022
a118655
test: 100% coverage for host.ts, refactor host.spec.ts
agilgur5 May 10, 2022
a61eace
refactor: remove remaining "dirty checking of instance as any"
agilgur5 May 10, 2022
2762e22
fix(test): get tests working on Ubuntu and Windows
agilgur5 May 9, 2022
500616e
refactor: use consts and shrink check-tsconfig
agilgur5 May 10, 2022
7e07a2e
refactor: remove normalizePaths func from get-options-overrides spec
agilgur5 May 10, 2022
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
10 changes: 6 additions & 4 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ on:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest
build-and-test:
name: Build & Test on Node ${{ matrix.node-version }} and ${{ matrix.os }}

runs-on: ${{ matrix.os }}
strategy:
matrix:
node-version: [10.x, 12.x]
node-version: [12.x, 14.x, 16.x, 18.x]
os: [ubuntu-latest, windows-latest, macOS-latest]

steps:
- uses: actions/checkout@v2
Expand All @@ -30,3 +31,4 @@ jobs:
- run: npm run build-self
env:
CI: true
- run: npm run test:coverage
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@
/build-self
/build
.rpt2_cache
*.swp
*.swo
coverage
__tests__/__temp/
22 changes: 18 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,25 @@ Attach plugin output with `verbosity` option set to 3 (this will list all files

## Developing

Use the [standard GitHub process](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/getting-started/about-collaborative-development-models#fork-and-pull-model) of forking, making a branch, and creating a PR when ready. Fix all linting problems (`npm run lint`) and fix any failed checks on the PR. Use an editor that supports [`editorconfig`](https://editorconfig.org/), or match the settings from [`.editorconfig`](./.editorconfig) manually.
Use the [standard GitHub process](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/getting-started/about-collaborative-development-models#fork-and-pull-model) of forking, making a branch, creating a PR when ready, and fixing any failing checks on the PR.

Fastest way to test changes is to do a self build; the plugin is part of its own build system:
### Linting and Style

1. Use an editor that supports [`editorconfig`](https://editorconfig.org/), or match the settings from [`.editorconfig`](./.editorconfig) manually.
1. Fix all linting problems with `npm run lint`.

### Testing

1. `npm test` to verify that all tests pass
1. `npm run test:watch` to run tests in watch mode while developing
1. `npm run test:coverage` to run tests and output a test coverage report

While this repo now has an assortment of unit tests, it still badly needs integration tests with various scenarios and expected outcomes.
Test coverage improvements for existing files and untested is needed as well.

### Building and Self-Build

One can test changes by doing a self-build; the plugin is part of its own build system.

1. make changes
1. run `npm run build` (uses last released version on npm)
Expand All @@ -28,5 +44,3 @@ Fastest way to test changes is to do a self build; the plugin is part of its own
1. run `npm run build-self` _again_ to make sure plugin built by new version can still build itself

If `build-self` breaks at some point, fix the problem and restart from the `build` step (a known good copy).

This repo badly needs unit tests and integration tests with various scenarios and expected outcomes though.
28 changes: 28 additions & 0 deletions __tests__/check-tsconfig.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { test, expect } from "@jest/globals";
import * as ts from "typescript";

import { setTypescriptModule } from "../src/tsproxy";
import { checkTsConfig } from "../src/check-tsconfig";

setTypescriptModule(ts);

const defaultConfig = { fileNames: [], errors: [], options: {} };

test("checkTsConfig", () => {
expect(() => checkTsConfig({
...defaultConfig,
options: { module: ts.ModuleKind.None },
})).toThrow(
`Incompatible tsconfig option. Module resolves to 'None'. This is incompatible with rollup, please use 'module: "ES2015"' or 'module: "ESNext"'.`,
);

expect(checkTsConfig({
...defaultConfig,
options: { module: ts.ModuleKind.ES2015 },
})).toBeFalsy();

expect(checkTsConfig({
...defaultConfig,
options: { module: ts.ModuleKind.ESNext },
})).toBeFalsy();
});
49 changes: 49 additions & 0 deletions __tests__/context.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { jest, test, expect } from "@jest/globals";

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

(global as any).console = {log: jest.fn()};

test("ConsoleContext", () => {
const proxy = new ConsoleContext(6, "=>");

proxy.warn("test");
expect(console.log).toHaveBeenLastCalledWith("=>test");

proxy.error("test2");
expect(console.log).toHaveBeenLastCalledWith("=>test2");

proxy.info("test3");
expect(console.log).toHaveBeenLastCalledWith("=>test3");

proxy.debug("test4");
expect(console.log).toHaveBeenLastCalledWith("=>test4");

proxy.warn(() => "ftest");
expect(console.log).toHaveBeenLastCalledWith("=>ftest");

proxy.error(() => "ftest2");
expect(console.log).toHaveBeenLastCalledWith("=>ftest2");

proxy.info(() => "ftest3");
expect(console.log).toHaveBeenLastCalledWith("=>ftest3");

proxy.debug(() => "ftest4");
expect(console.log).toHaveBeenLastCalledWith("=>ftest4");
});

test("ConsoleContext 0 verbosity", () => {
const proxy = new ConsoleContext(-100);

proxy.warn("no-test");
expect(console.log).not.toHaveBeenLastCalledWith("no-test");

proxy.info("no-test2");
expect(console.log).not.toHaveBeenLastCalledWith("no-test2");

proxy.debug("no-test3");
expect(console.log).not.toHaveBeenLastCalledWith("no-test3");

proxy.error("no-test4");
expect(console.log).not.toHaveBeenLastCalledWith("no-test4");
});
13 changes: 13 additions & 0 deletions __tests__/diagnostics-format-host.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { test, expect } from "@jest/globals";
import * as ts from "typescript";

import { setTypescriptModule } from "../src/tsproxy";
import { formatHost } from "../src/diagnostics-format-host";

setTypescriptModule(ts);

test("formatHost", () => {
expect(formatHost.getCurrentDirectory()).toEqual(process.cwd());
expect(formatHost.getCanonicalFileName("package.json")).toEqual("package.json");
expect(formatHost.getNewLine()).toEqual(ts.sys.newLine);
});
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
};
};
151 changes: 151 additions & 0 deletions __tests__/get-options-overrides.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import { afterAll, test, expect } from "@jest/globals";
import * as path from "path";
import * as ts from "typescript";
import { remove } from "fs-extra";

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

setTypescriptModule(ts);

const local = (x: string) => path.resolve(__dirname, x);
const cacheDir = local("__temp/get-options-overrides");

afterAll(() => remove(cacheDir));

const defaultConfig: IOptions = {
include: ["*.ts+(|x)", "**/*.ts+(|x)"],
exclude: ["*.d.ts", "**/*.d.ts"],
check: false,
verbosity: 5,
clean: false,
cacheRoot: cacheDir,
cwd: local(""),
abortOnError: false,
rollupCommonJSResolveHack: false,
typescript: ts,
objectHashIgnoreUnknownHack: false,
tsconfigOverride: null,
useTsconfigDeclarationDir: false,
tsconfigDefaults: null,
sourceMapCallback: (id: string, map: string): void => {
console.log(id + map);
},
transformers: [(ls: ts.LanguageService) => {
console.log(ls);
return {};
}],
};

const forcedOptions: ts.CompilerOptions = {
allowNonTsExtensions: true,
importHelpers: true,
inlineSourceMap: false,
moduleResolution: ts.ModuleResolutionKind.NodeJs,
noEmit: false,
noEmitHelpers: false,
noResolve: false,
outDir: `${cacheDir}/placeholder`, // TODO: fix get-options-overrides.ts on Windows by normalizing the path: https://github.com/ezolenko/rollup-plugin-typescript2/pull/321#discussion_r869710856
};

const defaultPreParsedTsConfig: ts.ParsedCommandLine = {
options: {},
fileNames: [],
errors: [],
};

test("getOptionsOverrides", () => {
const config = { ...defaultConfig };

expect(getOptionsOverrides(config)).toStrictEqual(forcedOptions);
});

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

expect(getOptionsOverrides(config, preParsedTsConfig)).toStrictEqual({
...forcedOptions,
declarationDir: undefined,
module: ts.ModuleKind.ES2015,
sourceRoot: undefined,
});
});

test("getOptionsOverrides - preParsedTsConfig with options.module", () => {
const config = { ...defaultConfig };
const preParsedTsConfig = {
...defaultPreParsedTsConfig,
options: {
module: ts.ModuleKind.AMD,
},
};

expect(getOptionsOverrides(config, preParsedTsConfig)).toStrictEqual({
...forcedOptions,
declarationDir: undefined,
sourceRoot: undefined,
});
});

test("getOptionsOverrides - with declaration", () => {
const config = { ...defaultConfig, useTsconfigDeclarationDir: true };
const preParsedTsConfig = { ...defaultPreParsedTsConfig };

expect(getOptionsOverrides(config, preParsedTsConfig)).toStrictEqual({
...forcedOptions,
module: ts.ModuleKind.ES2015,
sourceRoot: undefined,
});
});

test("getOptionsOverrides - with sourceMap", () => {
const config = { ...defaultConfig };
const preParsedTsConfig = {
...defaultPreParsedTsConfig,
options: {
sourceMap: true,
},
};

expect(getOptionsOverrides(config, preParsedTsConfig)).toStrictEqual({
...forcedOptions,
declarationDir: undefined,
module: ts.ModuleKind.ES2015,
});
});

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", () => {
agilgur5 marked this conversation as resolved.
Show resolved Hide resolved
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);
});
Loading