|
| 1 | +import { afterAll, test, expect } from "@jest/globals"; |
| 2 | +import * as path from "path"; |
| 3 | +import * as ts from "typescript"; |
| 4 | +import { remove } from "fs-extra"; |
| 5 | + |
| 6 | +import { makeStubbedContext } from "./fixtures/context"; |
| 7 | +import { setTypescriptModule } from "../src/tsproxy"; |
| 8 | +import { IOptions } from "../src/ioptions"; |
| 9 | +import { getOptionsOverrides, createFilter } from "../src/get-options-overrides"; |
| 10 | + |
| 11 | +setTypescriptModule(ts); |
| 12 | + |
| 13 | +const local = (x: string) => path.resolve(__dirname, x); |
| 14 | +const cacheDir = local("__temp/get-options-overrides"); |
| 15 | + |
| 16 | +afterAll(() => remove(cacheDir)); |
| 17 | + |
| 18 | +const defaultConfig: IOptions = { |
| 19 | + include: ["*.ts+(|x)", "**/*.ts+(|x)"], |
| 20 | + exclude: ["*.d.ts", "**/*.d.ts"], |
| 21 | + check: false, |
| 22 | + verbosity: 5, |
| 23 | + clean: false, |
| 24 | + cacheRoot: cacheDir, |
| 25 | + cwd: local(""), |
| 26 | + abortOnError: false, |
| 27 | + rollupCommonJSResolveHack: false, |
| 28 | + typescript: ts, |
| 29 | + objectHashIgnoreUnknownHack: false, |
| 30 | + tsconfigOverride: null, |
| 31 | + useTsconfigDeclarationDir: false, |
| 32 | + tsconfigDefaults: null, |
| 33 | + sourceMapCallback: (id: string, map: string): void => { |
| 34 | + console.log(id + map); |
| 35 | + }, |
| 36 | + transformers: [(ls: ts.LanguageService) => { |
| 37 | + console.log(ls); |
| 38 | + return {}; |
| 39 | + }], |
| 40 | +}; |
| 41 | + |
| 42 | +const forcedOptions: ts.CompilerOptions = { |
| 43 | + allowNonTsExtensions: true, |
| 44 | + importHelpers: true, |
| 45 | + inlineSourceMap: false, |
| 46 | + moduleResolution: ts.ModuleResolutionKind.NodeJs, |
| 47 | + noEmit: false, |
| 48 | + noEmitHelpers: false, |
| 49 | + noResolve: false, |
| 50 | + 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 |
| 51 | +}; |
| 52 | + |
| 53 | +const defaultPreParsedTsConfig: ts.ParsedCommandLine = { |
| 54 | + options: {}, |
| 55 | + fileNames: [], |
| 56 | + errors: [], |
| 57 | +}; |
| 58 | + |
| 59 | +test("getOptionsOverrides", () => { |
| 60 | + const config = { ...defaultConfig }; |
| 61 | + |
| 62 | + expect(getOptionsOverrides(config)).toStrictEqual(forcedOptions); |
| 63 | +}); |
| 64 | + |
| 65 | +test("getOptionsOverrides - preParsedTsConfig", () => { |
| 66 | + const config = { ...defaultConfig }; |
| 67 | + const preParsedTsConfig = { ...defaultPreParsedTsConfig }; |
| 68 | + |
| 69 | + expect(getOptionsOverrides(config, preParsedTsConfig)).toStrictEqual({ |
| 70 | + ...forcedOptions, |
| 71 | + declarationDir: undefined, |
| 72 | + module: ts.ModuleKind.ES2015, |
| 73 | + sourceRoot: undefined, |
| 74 | + }); |
| 75 | +}); |
| 76 | + |
| 77 | +test("getOptionsOverrides - preParsedTsConfig with options.module", () => { |
| 78 | + const config = { ...defaultConfig }; |
| 79 | + const preParsedTsConfig = { |
| 80 | + ...defaultPreParsedTsConfig, |
| 81 | + options: { |
| 82 | + module: ts.ModuleKind.AMD, |
| 83 | + }, |
| 84 | + }; |
| 85 | + |
| 86 | + expect(getOptionsOverrides(config, preParsedTsConfig)).toStrictEqual({ |
| 87 | + ...forcedOptions, |
| 88 | + declarationDir: undefined, |
| 89 | + sourceRoot: undefined, |
| 90 | + }); |
| 91 | +}); |
| 92 | + |
| 93 | +test("getOptionsOverrides - with declaration", () => { |
| 94 | + const config = { ...defaultConfig, useTsconfigDeclarationDir: true }; |
| 95 | + const preParsedTsConfig = { ...defaultPreParsedTsConfig }; |
| 96 | + |
| 97 | + expect(getOptionsOverrides(config, preParsedTsConfig)).toStrictEqual({ |
| 98 | + ...forcedOptions, |
| 99 | + module: ts.ModuleKind.ES2015, |
| 100 | + sourceRoot: undefined, |
| 101 | + }); |
| 102 | +}); |
| 103 | + |
| 104 | +test("getOptionsOverrides - with sourceMap", () => { |
| 105 | + const config = { ...defaultConfig }; |
| 106 | + const preParsedTsConfig = { |
| 107 | + ...defaultPreParsedTsConfig, |
| 108 | + options: { |
| 109 | + sourceMap: true, |
| 110 | + }, |
| 111 | + }; |
| 112 | + |
| 113 | + expect(getOptionsOverrides(config, preParsedTsConfig)).toStrictEqual({ |
| 114 | + ...forcedOptions, |
| 115 | + declarationDir: undefined, |
| 116 | + module: ts.ModuleKind.ES2015, |
| 117 | + }); |
| 118 | +}); |
| 119 | + |
| 120 | +test("createFilter", () => { |
| 121 | + const config = { ...defaultConfig }; |
| 122 | + const preParsedTsConfig = { ...defaultPreParsedTsConfig }; |
| 123 | + |
| 124 | + const stubbedContext = makeStubbedContext({}); |
| 125 | + const filter = createFilter(stubbedContext, config, preParsedTsConfig); |
| 126 | + |
| 127 | + expect(filter("src/test.ts")).toBe(true); |
| 128 | + expect(filter("src/test.js")).toBe(false); |
| 129 | + expect(filter("src/test.d.ts")).toBe(false); |
| 130 | +}); |
| 131 | + |
| 132 | +// not totally sure why this is failing |
| 133 | +test.skip("createFilter -- rootDirs", () => { |
| 134 | + const config = { ...defaultConfig }; |
| 135 | + const preParsedTsConfig = { |
| 136 | + ...defaultPreParsedTsConfig, |
| 137 | + options: { |
| 138 | + rootDirs: ["src", "lib"] |
| 139 | + }, |
| 140 | + }; |
| 141 | + |
| 142 | + const stubbedContext = makeStubbedContext({}); |
| 143 | + const filter = createFilter(stubbedContext, config, preParsedTsConfig); |
| 144 | + |
| 145 | + expect(filter("src/test.ts")).toBe(true); |
| 146 | + expect(filter("src/test.js")).toBe(false); |
| 147 | + expect(filter("src/test.d.ts")).toBe(false); |
| 148 | + expect(filter("lib/test.ts")).toBe(true); |
| 149 | + expect(filter("lib/test.js")).toBe(false); |
| 150 | + expect(filter("lib/test.d.ts")).toBe(false); |
| 151 | +}); |
0 commit comments