|
| 1 | +import { jest, afterAll, test, expect } from "@jest/globals"; |
| 2 | +import * as path from "path"; |
| 3 | +import { normalizePath as normalize } from "@rollup/pluginutils"; |
| 4 | +import * as fs from "fs-extra"; |
| 5 | +import { red } from "colors/safe"; |
| 6 | + |
| 7 | +import { RPT2Options } from "../../src/index"; |
| 8 | +import * as helpers from "./helpers"; |
| 9 | + |
| 10 | +// increase timeout to 15s for whole file since CI occassionally timed out -- these are integration and cache tests, so longer timeout is warranted |
| 11 | +jest.setTimeout(15000); |
| 12 | + |
| 13 | +const local = (x: string) => path.resolve(__dirname, x); |
| 14 | +const cacheRoot = local("__temp/errors/rpt2-cache"); // don't use the one in node_modules |
| 15 | +const onwarn = jest.fn(); |
| 16 | + |
| 17 | +afterAll(async () => { |
| 18 | + // 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) |
| 19 | + await new Promise(resolve => setTimeout(resolve, 1000)); |
| 20 | + await fs.remove(cacheRoot); |
| 21 | +}); |
| 22 | + |
| 23 | +async function genBundle(relInput: string, extraOpts?: RPT2Options) { |
| 24 | + const input = normalize(local(`fixtures/errors/${relInput}`)); |
| 25 | + return helpers.genBundle({ |
| 26 | + input, |
| 27 | + tsconfig: local("fixtures/errors/tsconfig.json"), |
| 28 | + cacheRoot, |
| 29 | + extraOpts: { include: [input], ...extraOpts }, // only include the input itself, not other error files (to only generate types and type-check the one file) |
| 30 | + onwarn, |
| 31 | + }); |
| 32 | +} |
| 33 | + |
| 34 | +test("integration - tsconfig errors", async () => { |
| 35 | + // TODO: move to parse-tsconfig unit tests? |
| 36 | + expect(genBundle("semantic.ts", { |
| 37 | + tsconfig: "non-existent-tsconfig", |
| 38 | + })).rejects.toThrow("rpt2: failed to open 'undefined'"); // FIXME: bug: this should be "non-existent-tsconfig", not "undefined" |
| 39 | +}); |
| 40 | + |
| 41 | +test("integration - semantic error", async () => { |
| 42 | + expect(genBundle("semantic.ts")).rejects.toThrow(`semantic error TS2322: ${red("Type 'string' is not assignable to type 'number'.")}`); |
| 43 | +}); |
| 44 | + |
| 45 | +test("integration - semantic error - abortOnError: false / check: false", async () => { |
| 46 | + // either warning or not type-checking should result in the same bundle |
| 47 | + const { output } = await genBundle("semantic.ts", { abortOnError: false }); |
| 48 | + const { output: output2 } = await genBundle("semantic.ts", { check: false }); |
| 49 | + expect(output).toEqual(output2); |
| 50 | + |
| 51 | + expect(output[0].fileName).toEqual("index.js"); |
| 52 | + expect(output[1].fileName).toEqual("semantic.d.ts"); |
| 53 | + expect(output[2].fileName).toEqual("semantic.d.ts.map"); |
| 54 | + expect(output.length).toEqual(3); // no other files |
| 55 | + expect(onwarn).toBeCalledTimes(1); |
| 56 | +}); |
| 57 | + |
| 58 | +test("integration - syntax error", () => { |
| 59 | + expect(genBundle("syntax.ts")).rejects.toThrow(`syntax error TS1005: ${red("';' expected.")}`); |
| 60 | +}); |
| 61 | + |
| 62 | +test("integration - syntax error - abortOnError: false / check: false", () => { |
| 63 | + const err = "Unexpected token (Note that you need plugins to import files that are not JavaScript)"; |
| 64 | + expect(genBundle("syntax.ts", { abortOnError: false })).rejects.toThrow(err); |
| 65 | + expect(genBundle("syntax.ts", { check: false })).rejects.toThrow(err); |
| 66 | +}); |
0 commit comments