|
1 | 1 | /*eslint-env node */
|
2 |
| -import assert from "assert" |
3 | 2 | import path from "path"
|
| 3 | +import { TypeTester } from "type-tester" |
4 | 4 | import ts from "typescript"
|
5 | 5 |
|
6 |
| -const TARGET_FILE = path.resolve(__dirname, "../index.d.ts") |
7 |
| -const FIXTURE_FILE = path.resolve(__dirname, "fixtures/types.ts") |
| 6 | +const tester = new TypeTester(ts) |
8 | 7 |
|
9 | 8 | describe("TypeScript type definitions", () => {
|
10 |
| - describe("'index.d.ts'", () => { |
11 |
| - it("should have no error when it was compiled without 'lib.dom.d.ts'.", () => { |
12 |
| - const program = ts.createProgram([TARGET_FILE], { |
13 |
| - lib: ["lib.es5.d.ts"], |
14 |
| - strict: true, |
15 |
| - }) |
16 |
| - const [diagnostic] = [ |
17 |
| - ...program.getSyntacticDiagnostics(), |
18 |
| - ...program.getSemanticDiagnostics(), |
19 |
| - ] |
20 |
| - const source = program.getSourceFile(TARGET_FILE) |
21 |
| - |
22 |
| - if (diagnostic != null) { |
23 |
| - const { line } = source.getLineAndCharacterOfPosition( |
24 |
| - diagnostic.start |
25 |
| - ) |
26 |
| - assert.fail( |
27 |
| - `${ts.flattenDiagnosticMessageText( |
28 |
| - diagnostic.messageText, |
29 |
| - "\n" |
30 |
| - )} at L${line + 1}` |
31 |
| - ) |
32 |
| - } |
33 |
| - }) |
34 |
| - }) |
35 |
| - |
36 |
| - describe("'fixtures/types.ts'", () => { |
37 |
| - const program = ts.createProgram([TARGET_FILE, FIXTURE_FILE], { |
38 |
| - lib: ["lib.es5.d.ts", "lib.dom.d.ts"], |
| 9 | + describe("'index.d.ts' should have no error even if it was compiled without 'lib.dom.d.ts'.", () => { |
| 10 | + tester.verify([path.resolve(__dirname, "../index.d.ts")], { |
| 11 | + lib: ["lib.es5.d.ts"], |
39 | 12 | strict: true,
|
40 | 13 | })
|
41 |
| - const diagnostics = new Set([ |
42 |
| - ...program.getSyntacticDiagnostics(), |
43 |
| - ...program.getSemanticDiagnostics(), |
44 |
| - ]) |
45 |
| - const source = program.getSourceFile(FIXTURE_FILE) |
46 |
| - const scanner = ts.createScanner( |
47 |
| - source.languageVersion, |
48 |
| - false, |
49 |
| - source.languageVariant, |
50 |
| - source.getFullText() |
51 |
| - ) |
52 |
| - const expectedDiagnostics = new Map() |
53 |
| - |
54 |
| - // Find @expected comments |
55 |
| - let kind = 0 |
56 |
| - while ((kind = scanner.scan()) !== ts.SyntaxKind.EndOfFileToken) { |
57 |
| - if (kind === ts.SyntaxKind.SingleLineCommentTrivia) { |
58 |
| - const comment = scanner.getTokenText() |
59 |
| - const m = /^\/\/\s*@expected\s+(\d+)$/u.exec(comment) |
60 |
| - if (m != null) { |
61 |
| - const code = Number(m[1]) |
62 |
| - const { line } = source.getLineAndCharacterOfPosition( |
63 |
| - scanner.getTokenPos() |
64 |
| - ) |
65 |
| - |
66 |
| - // Find diagnostic |
67 |
| - let foundDiagnostic = null |
68 |
| - for (const d of diagnostics) { |
69 |
| - const { |
70 |
| - line: dLine, |
71 |
| - } = source.getLineAndCharacterOfPosition(d.start) |
72 |
| - |
73 |
| - if (d.code === code && dLine === line) { |
74 |
| - foundDiagnostic = d |
75 |
| - diagnostics.delete(d) |
76 |
| - break |
77 |
| - } |
78 |
| - } |
79 |
| - |
80 |
| - expectedDiagnostics.set( |
81 |
| - `TS${code} at L${line + 1}`, |
82 |
| - foundDiagnostic |
83 |
| - ) |
84 |
| - } |
85 |
| - } |
86 |
| - } |
87 |
| - |
88 |
| - // Expected errors |
89 |
| - for (const [key, d] of expectedDiagnostics) { |
90 |
| - it(`should have an error ${key}.`, () => { |
91 |
| - assert(d != null) |
92 |
| - }) |
93 |
| - } |
| 14 | + }) |
94 | 15 |
|
95 |
| - // Unexpected errors |
96 |
| - for (const d of diagnostics) { |
97 |
| - const { line } = source.getLineAndCharacterOfPosition(d.start) |
98 |
| - it(`should NOT have an error TS${d.code} at L${line + 1}.`, () => { |
99 |
| - assert.fail( |
100 |
| - ts.flattenDiagnosticMessageText(d.messageText, "\n") |
101 |
| - ) |
102 |
| - }) |
103 |
| - } |
| 16 | + tester.verify([path.resolve(__dirname, "fixtures/types.ts")], { |
| 17 | + lib: ["lib.es5.d.ts", "lib.dom.d.ts"], |
| 18 | + strict: true, |
104 | 19 | })
|
105 | 20 | })
|
0 commit comments