Skip to content

Commit ddf8266

Browse files
committed
🎨 use type-tester to test type definitions
1 parent a3774a5 commit ddf8266

File tree

2 files changed

+10
-94
lines changed

2 files changed

+10
-94
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"rollup-plugin-json": "^3.1.0",
5959
"rollup-plugin-node-resolve": "^4.0.0",
6060
"rollup-watch": "^4.3.1",
61+
"type-tester": "^1.0.0",
6162
"typescript": "^3.2.4"
6263
},
6364
"repository": {

test/types.mjs

Lines changed: 9 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,20 @@
11
/*eslint-env node */
2-
import assert from "assert"
32
import path from "path"
3+
import { TypeTester } from "type-tester"
44
import ts from "typescript"
55

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)
87

98
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"],
3912
strict: true,
4013
})
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+
})
9415

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,
10419
})
10520
})

0 commit comments

Comments
 (0)