-
Notifications
You must be signed in to change notification settings - Fork 70
test: add initial unit test suite #321
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
Merged
Merged
Changes from 20 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
aa04d77
some unit tests
brekk b217694
more
brekk ceae7fa
fix tests for windows
c3cfe41
flip that test
brekk b73370a
deps: upgrade Jest to v28
agilgur5 6719c0c
refactor: split jest config to jest.config.js
agilgur5 3c9b59c
clean: remove redundant jest config
agilgur5 08637a4
refactor: move tests into __tests__ dir
agilgur5 c2b9ffc
deps: import from @jest/globals instead of using globals
agilgur5 e9ddccc
fix(test): update get-options-overrides spec to match latest file
agilgur5 1c9b1cb
refactor(test): use obj spread in get-options-overrides
agilgur5 0049f16
fix(test): update host spec to match latest source
agilgur5 c197219
refactor(test): use async/await, remove truncate func in host spec
agilgur5 7d9e362
refactor: use consts in host spec instead of locals
agilgur5 30e5b32
fix(test): update rollupcontext spec to match latest source
agilgur5 20f66e9
lint: add __tests__ to lint dirs, fix lint errors
agilgur5 4dfb8ce
ci: add unit tests to matrix
agilgur5 dc1895f
test: add unit tests for createFilter
agilgur5 335b4bf
refactor: use consts, async/await, etc in rollingcache spec
agilgur5 bcce6d6
docs: add a testing section to CONTRIBUTING.md
agilgur5 d31a3fa
fix(test): undo tsTypes -> tsModules changes in source code
agilgur5 bf4419e
test: get host spec customTransformers test working
agilgur5 5e1b246
ci: add Node 14, 16, 18, and macOS, Windows to matrix
agilgur5 6584949
refactor: use __temp dir in options-overrides spec
agilgur5 a118655
test: 100% coverage for host.ts, refactor host.spec.ts
agilgur5 a61eace
refactor: remove remaining "dirty checking of instance as any"
agilgur5 2762e22
fix(test): get tests working on Ubuntu and Windows
agilgur5 500616e
refactor: use consts and shrink check-tsconfig
agilgur5 7e07a2e
refactor: remove normalizePaths func from get-options-overrides spec
agilgur5 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,7 @@ | |
/build-self | ||
/build | ||
.rpt2_cache | ||
*.swp | ||
*.swo | ||
coverage | ||
__tests__/__temp/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { test, expect } from "@jest/globals"; | ||
import * as ts from "typescript"; | ||
|
||
import { checkTsConfig } from "../src/check-tsconfig"; | ||
|
||
test("checkTsConfig", () => { | ||
expect(() => | ||
checkTsConfig({ | ||
fileNames: [], | ||
errors: [], | ||
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({ | ||
fileNames: [], | ||
errors: [], | ||
options: { module: ts.ModuleKind.ES2015 }, | ||
}), | ||
).toBeFalsy(); | ||
|
||
expect( | ||
checkTsConfig({ | ||
fileNames: [], | ||
errors: [], | ||
options: { module: ts.ModuleKind.ESNext }, | ||
}), | ||
).toBeFalsy(); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
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"); | ||
|
||
expect((proxy as any).prefix).toEqual("=>"); | ||
}); | ||
|
||
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"); | ||
|
||
expect((proxy as any).prefix).toEqual(""); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { test, expect } from "@jest/globals"; | ||
import * as ts from "typescript"; | ||
|
||
import { formatHost } from "../src/diagnostics-format-host"; | ||
|
||
test("formatHost", () => { | ||
const current = formatHost.getCurrentDirectory(); | ||
expect(current.substr(current.lastIndexOf("/"))).toEqual("/rollup-plugin-typescript2"); | ||
|
||
expect(formatHost.getCanonicalFileName("package.json")).toEqual("package.json"); | ||
expect(formatHost.getNewLine()).toEqual(ts.sys.newLine); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
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 { IOptions } from "../src/ioptions"; | ||
import { getOptionsOverrides, createFilter } from "../src/get-options-overrides"; | ||
|
||
const local = (x: string) => path.resolve(__dirname, x); | ||
|
||
afterAll(() => remove(local("fixtures/options"))); | ||
|
||
const normalizePaths = (props: string[], x: any) => { | ||
for (const prop of props) { | ||
if (!x[prop]) continue | ||
|
||
x[prop] = x[prop].substr(x[prop].lastIndexOf("/") + 1); | ||
} | ||
|
||
return x; | ||
}; | ||
|
||
const defaultConfig: IOptions = { | ||
include: ["*.ts+(|x)", "**/*.ts+(|x)"], | ||
exclude: ["*.d.ts", "**/*.d.ts"], | ||
check: false, | ||
verbosity: 5, | ||
clean: false, | ||
cacheRoot: local("fixtures/options"), | ||
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: "placeholder", // normalized | ||
} | ||
|
||
const defaultPreParsedTsConfig: ts.ParsedCommandLine = { | ||
options: {}, | ||
fileNames: [], | ||
errors: [], | ||
}; | ||
|
||
test("getOptionsOverrides", () => { | ||
const config = { ...defaultConfig }; | ||
expect(normalizePaths(["outDir"], getOptionsOverrides(config))).toStrictEqual( | ||
{ | ||
...forcedOptions, | ||
}, | ||
); | ||
}); | ||
|
||
test("getOptionsOverrides - preParsedTsConfig", () => { | ||
const config = { ...defaultConfig }; | ||
const preParsedTsConfig = { ...defaultPreParsedTsConfig }; | ||
expect(normalizePaths(["outDir"], 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(normalizePaths(["outDir"], getOptionsOverrides(config, preParsedTsConfig))).toStrictEqual( | ||
{ | ||
...forcedOptions, | ||
declarationDir: undefined, | ||
sourceRoot: undefined, | ||
}, | ||
); | ||
}); | ||
|
||
test("getOptionsOverrides - with declaration", () => { | ||
const config = { ...defaultConfig, useTsconfigDeclarationDir: true }; | ||
const preParsedTsConfig = { ...defaultPreParsedTsConfig }; | ||
expect(normalizePaths(["outDir"], 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(normalizePaths(["outDir"], 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); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.