diff --git a/.gitignore b/.gitignore index 4b9ad01..ebc671a 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,4 @@ *.swp *.swo coverage -__tests__/__temp/ +__temp diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 6a80cfc..a57131a 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -30,8 +30,8 @@ Use the [standard GitHub process](https://docs.github.com/en/pull-requests/colla 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. +While this repo now has an assortment of unit tests and integration tests, it still needs more integration tests with various scenarios and expected outcomes. +Test coverage improvements for existing files and untested files is needed as well. ### Building and Self-Build @@ -72,7 +72,7 @@ A useful resource as you dive deeper are the [unit tests](__tests__/). They're g - [Using the Language Service API](https://github.com/microsoft/TypeScript/wiki/Using-the-Language-Service-API) - _NOTE_: These are fairly short and unfortunately leave a lot to be desired... especially when you consider that this plugin is actually one of the simpler integrations out there. 1. At this point, you may be ready to read the more complicated bits of [`index`](src/index.ts) in detail and see how it interacts with the other modules. - - The integration tests [TBD] could be useful to review at this point as well. + - The [integration tests](__tests__/integration/) could be useful to review at this point as well. 1. Once you're pretty familiar with `index`, you can dive into some of the cache code in [`tscache`](src/tscache.ts) and [`rollingcache`](src/rollingcache.ts). 1. And finally, you can see some of the Rollup logging nuances in [`context`](src/context.ts) and [`rollupcontext`](src/rollupcontext.ts), and then the TS logging nuances in [`print-diagnostics`](src/print-diagnostics.ts), and [`diagnostics-format-host`](src/diagnostics-format-host.ts) - While these are necessary to the implementation, they are fairly ancillary to understanding and working with the codebase. diff --git a/__tests__/integration/errors.spec.ts b/__tests__/integration/errors.spec.ts new file mode 100644 index 0000000..cc40cbe --- /dev/null +++ b/__tests__/integration/errors.spec.ts @@ -0,0 +1,66 @@ +import { jest, afterAll, test, expect } from "@jest/globals"; +import * as path from "path"; +import { normalizePath as normalize } from "@rollup/pluginutils"; +import * as fs from "fs-extra"; +import { red } from "colors/safe"; + +import { RPT2Options } from "../../src/index"; +import * as helpers from "./helpers"; + +// increase timeout to 15s for whole file since CI occassionally timed out -- these are integration and cache tests, so longer timeout is warranted +jest.setTimeout(15000); + +const local = (x: string) => path.resolve(__dirname, x); +const cacheRoot = local("__temp/errors/rpt2-cache"); // don't use the one in node_modules +const onwarn = jest.fn(); + +afterAll(async () => { + // workaround: there seems to be some race condition causing fs.remove to fail, so give it a sec first (c.f. https://github.com/jprichardson/node-fs-extra/issues/532) + await new Promise(resolve => setTimeout(resolve, 1000)); + await fs.remove(cacheRoot); +}); + +async function genBundle(relInput: string, extraOpts?: RPT2Options) { + const input = normalize(local(`fixtures/errors/${relInput}`)); + return helpers.genBundle({ + input, + tsconfig: local("fixtures/errors/tsconfig.json"), + cacheRoot, + extraOpts: { include: [input], ...extraOpts }, // only include the input itself, not other error files (to only generate types and type-check the one file) + onwarn, + }); +} + +test("integration - tsconfig errors", async () => { + // TODO: move to parse-tsconfig unit tests? + expect(genBundle("semantic.ts", { + tsconfig: "non-existent-tsconfig", + })).rejects.toThrow("rpt2: failed to open 'undefined'"); // FIXME: bug: this should be "non-existent-tsconfig", not "undefined" +}); + +test("integration - semantic error", async () => { + expect(genBundle("semantic.ts")).rejects.toThrow(`semantic error TS2322: ${red("Type 'string' is not assignable to type 'number'.")}`); +}); + +test("integration - semantic error - abortOnError: false / check: false", async () => { + // either warning or not type-checking should result in the same bundle + const { output } = await genBundle("semantic.ts", { abortOnError: false }); + const { output: output2 } = await genBundle("semantic.ts", { check: false }); + expect(output).toEqual(output2); + + expect(output[0].fileName).toEqual("index.js"); + expect(output[1].fileName).toEqual("semantic.d.ts"); + expect(output[2].fileName).toEqual("semantic.d.ts.map"); + expect(output.length).toEqual(3); // no other files + expect(onwarn).toBeCalledTimes(1); +}); + +test("integration - syntax error", () => { + expect(genBundle("syntax.ts")).rejects.toThrow(`syntax error TS1005: ${red("';' expected.")}`); +}); + +test("integration - syntax error - abortOnError: false / check: false", () => { + const err = "Unexpected token (Note that you need plugins to import files that are not JavaScript)"; + expect(genBundle("syntax.ts", { abortOnError: false })).rejects.toThrow(err); + expect(genBundle("syntax.ts", { check: false })).rejects.toThrow(err); +}); diff --git a/__tests__/integration/fixtures/errors/semantic.ts b/__tests__/integration/fixtures/errors/semantic.ts new file mode 100644 index 0000000..4deda5e --- /dev/null +++ b/__tests__/integration/fixtures/errors/semantic.ts @@ -0,0 +1,3 @@ +export const sum = (a: number, b: number): number => { + return "a + b"; +} diff --git a/__tests__/integration/fixtures/errors/syntax.ts b/__tests__/integration/fixtures/errors/syntax.ts new file mode 100644 index 0000000..f0322fa --- /dev/null +++ b/__tests__/integration/fixtures/errors/syntax.ts @@ -0,0 +1,3 @@ +export const incorrectSyntax => { + return "a + b"; +} diff --git a/__tests__/integration/fixtures/errors/tsconfig.json b/__tests__/integration/fixtures/errors/tsconfig.json new file mode 100644 index 0000000..48610a1 --- /dev/null +++ b/__tests__/integration/fixtures/errors/tsconfig.json @@ -0,0 +1,3 @@ +{ + "extends": "../tsconfig.json", +} diff --git a/__tests__/integration/fixtures/no-errors/index.ts b/__tests__/integration/fixtures/no-errors/index.ts new file mode 100644 index 0000000..bd4654b --- /dev/null +++ b/__tests__/integration/fixtures/no-errors/index.ts @@ -0,0 +1,8 @@ +export function sum(a: number, b: number) { + return a + b; +} + +export { difference } from "./some-import" +export type { num } from "./type-only-import" + +export { identity } from "./some-js-import" diff --git a/__tests__/integration/fixtures/no-errors/some-import.ts b/__tests__/integration/fixtures/no-errors/some-import.ts new file mode 100644 index 0000000..63f4e68 --- /dev/null +++ b/__tests__/integration/fixtures/no-errors/some-import.ts @@ -0,0 +1,3 @@ +export function difference(a: number, b: number) { + return a - b; +} diff --git a/__tests__/integration/fixtures/no-errors/some-js-import.d.ts b/__tests__/integration/fixtures/no-errors/some-js-import.d.ts new file mode 100644 index 0000000..f78e726 --- /dev/null +++ b/__tests__/integration/fixtures/no-errors/some-js-import.d.ts @@ -0,0 +1,4 @@ +// TS needs a declaration in order to understand the import +// but this is ambient, and so should not be directly imported into rpt2, just found by TS + +export function identity(a: any): any; diff --git a/__tests__/integration/fixtures/no-errors/some-js-import.js b/__tests__/integration/fixtures/no-errors/some-js-import.js new file mode 100644 index 0000000..3ffcfb5 --- /dev/null +++ b/__tests__/integration/fixtures/no-errors/some-js-import.js @@ -0,0 +1,5 @@ +// should be filtered out by rpt2, but still bundled by Rollup itself (as this is ESM, no need for a plugin) + +export function identity(a) { + return a; +} diff --git a/__tests__/integration/fixtures/no-errors/tsconfig.json b/__tests__/integration/fixtures/no-errors/tsconfig.json new file mode 100644 index 0000000..48610a1 --- /dev/null +++ b/__tests__/integration/fixtures/no-errors/tsconfig.json @@ -0,0 +1,3 @@ +{ + "extends": "../tsconfig.json", +} diff --git a/__tests__/integration/fixtures/no-errors/type-only-import.ts b/__tests__/integration/fixtures/no-errors/type-only-import.ts new file mode 100644 index 0000000..5779bdf --- /dev/null +++ b/__tests__/integration/fixtures/no-errors/type-only-import.ts @@ -0,0 +1 @@ +export type num = number; diff --git a/__tests__/integration/fixtures/tsconfig.json b/__tests__/integration/fixtures/tsconfig.json new file mode 100644 index 0000000..3e6ee32 --- /dev/null +++ b/__tests__/integration/fixtures/tsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "forceConsistentCasingInFileNames": true, + "moduleResolution": "node", + "sourceMap": true, + "declaration": true, + "declarationMap": true, + "target": "ES6", + "strict": true, + }, +} diff --git a/__tests__/integration/helpers.ts b/__tests__/integration/helpers.ts new file mode 100644 index 0000000..bd72eac --- /dev/null +++ b/__tests__/integration/helpers.ts @@ -0,0 +1,41 @@ +import { rollup, RollupOptions, OutputAsset } from "rollup"; + +import rpt2, { RPT2Options } from "../../src/index"; + +type Params = { + input: string, + tsconfig: string, + cacheRoot: string, + extraOpts?: RPT2Options, + onwarn?: RollupOptions['onwarn'], +}; + +export async function genBundle ({ input, tsconfig, cacheRoot, extraOpts, onwarn }: Params) { + const bundle = await rollup({ + input, + plugins: [rpt2({ + tsconfig, + cacheRoot, + ...extraOpts, + })], + onwarn, + }); + + const esm = await bundle.generate({ + file: "./dist/index.js", + format: "esm", + exports: "named", + }); + + // Rollup has some deprecated properties like `get isAsset`, so enumerating them with, e.g. `.toEqual`, causes a bunch of warnings to be output + // delete the `isAsset` property for (much) cleaner logs + const { output: files } = esm; + for (const file of files) { + if ("isAsset" in file) { + const optIsAsset = file as Partial> & Omit; + delete optIsAsset["isAsset"]; + } + } + + return esm; +} diff --git a/__tests__/integration/no-errors.spec.ts b/__tests__/integration/no-errors.spec.ts new file mode 100644 index 0000000..283ab76 --- /dev/null +++ b/__tests__/integration/no-errors.spec.ts @@ -0,0 +1,91 @@ +import { jest, afterAll, test, expect } from "@jest/globals"; +import * as path from "path"; +import * as fs from "fs-extra"; + +import { RPT2Options } from "../../src/index"; +import * as helpers from "./helpers"; + +// increase timeout to 15s for whole file since CI occassionally timed out -- these are integration and cache tests, so longer timeout is warranted +jest.setTimeout(15000); + +const local = (x: string) => path.resolve(__dirname, x); +const cacheRoot = local("__temp/no-errors/rpt2-cache"); // don't use the one in node_modules + +afterAll(() => fs.remove(cacheRoot)); + +async function genBundle(relInput: string, extraOpts?: RPT2Options) { + return helpers.genBundle({ + input: local(`fixtures/no-errors/${relInput}`), + tsconfig: local("fixtures/no-errors/tsconfig.json"), + cacheRoot, + extraOpts, + }); +} + +test("integration - no errors", async () => { + const { output } = await genBundle("index.ts", { clean: true }); + + // populate the cache + await genBundle("index.ts"); + const { output: outputWithCache } = await genBundle("index.ts"); + expect(output).toEqual(outputWithCache); + + expect(output[0].fileName).toEqual("index.js"); + expect(output[1].fileName).toEqual("index.d.ts"); + expect(output[2].fileName).toEqual("index.d.ts.map"); + expect(output[3].fileName).toEqual("some-import.d.ts"); + expect(output[4].fileName).toEqual("some-import.d.ts.map"); + expect(output[5].fileName).toEqual("type-only-import.d.ts"); + expect(output[6].fileName).toEqual("type-only-import.d.ts.map"); + expect(output.length).toEqual(7); // no other files + + // JS file should be bundled by Rollup, even though rpt2 does not resolve it (as Rollup natively understands ESM) + expect(output[0].code).toEqual(expect.stringContaining("identity")); +}); + +test("integration - no errors - no declaration maps", async () => { + const noDeclarationMaps = { compilerOptions: { declarationMap: false } }; + const { output } = await genBundle("index.ts", { + tsconfigOverride: noDeclarationMaps, + clean: true, + }); + + expect(output[0].fileName).toEqual("index.js"); + expect(output[1].fileName).toEqual("index.d.ts"); + expect(output[2].fileName).toEqual("some-import.d.ts"); + expect(output[3].fileName).toEqual("type-only-import.d.ts"); + expect(output.length).toEqual(4); // no other files +}); + + +test("integration - no errors - no declarations", async () => { + const noDeclarations = { compilerOptions: { declaration: false, declarationMap: false } }; + const { output } = await genBundle("index.ts", { + tsconfigOverride: noDeclarations, + clean: true, + }); + + expect(output[0].fileName).toEqual("index.js"); + expect(output.length).toEqual(1); // no other files +}); + +test("integration - no errors - allowJs + emitDeclarationOnly", async () => { + const { output } = await genBundle("some-js-import.js", { + include: ["**/*.js"], + tsconfigOverride: { + compilerOptions: { + allowJs: true, + emitDeclarationOnly: true, + }, + }, + }); + + expect(output[0].fileName).toEqual("index.js"); + expect(output[1].fileName).toEqual("some-js-import.d.ts"); + expect(output[2].fileName).toEqual("some-js-import.d.ts.map"); + expect(output.length).toEqual(3); // no other files + + expect(output[0].code).toEqual(expect.stringContaining("identity")); + expect(output[0].code).not.toEqual(expect.stringContaining("sum")); // no TS files included + expect("source" in output[1] && output[1].source).toEqual(expect.stringContaining("identity")); +}); diff --git a/jest.config.js b/jest.config.js index e23474c..43aad89 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,6 +1,14 @@ -/** @type {import("@jest/types").Config.InitialOptions} */ +/** @type {import("ts-jest").InitialOptionsTsJest} */ const config = { + // ts-jest settings preset: "ts-jest", + globals: { + "ts-jest": { + tsconfig: "./tsconfig.test.json", + } + }, + + // jest settings injectGlobals: false, // use @jest/globals instead restoreMocks: true, // only use *.spec.ts files in __tests__, no auto-generated files diff --git a/tsconfig.test.json b/tsconfig.test.json new file mode 100644 index 0000000..80d0aac --- /dev/null +++ b/tsconfig.test.json @@ -0,0 +1,6 @@ +{ + "extends": "./tsconfig", + "compilerOptions": { + "esModuleInterop": true, // needed to parse some imports with ts-jest (since Rollup isn't used when importing during testing) + }, +}