8000 test(type-utils): add tests for containsAllTypesByName (#9711) · jakebailey/typescript-eslint@fd57da9 · GitHub
[go: up one dir, main page]

Skip to content

Commit fd57da9

Browse files
authored
test(type-utils): add tests for containsAllTypesByName (typescript-eslint#9711)
* test: add test case * test: disable automatic single run inference for tests
1 parent 213e3c2 commit fd57da9

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import { parseForESLint } from '@typescript-eslint/parser';
2+
import type { TSESTree } from '@typescript-eslint/typescript-estree';
3+
import path from 'path';
4+
import type * as ts from 'typescript';
5+
6+
import { containsAllTypesByName } from '../src';
7+
import { expectToHaveParserServices } from './test-utils/expectToHaveParserServices';
8+
9+
describe('containsAllTypesByName', () => {
10+
const rootDir = path.join(__dirname, 'fixtures');
11+
12+
function getType(code: string): ts.Type {
13+
const { ast, services } = parseForESLint(code, {
14+
disallowAutomaticSingleRunInference: true,
15+
project: './tsconfig.json',
16+
filePath: path.join(rootDir, 'file.ts'),
17+
tsconfigRootDir: rootDir,
18+
});
19+
expectToHaveParserServices(services);
20+
const declaration = ast.body[0] as TSESTree.TSTypeAliasDeclaration;
21+
return services.getTypeAtLocation(declaration.id);
22+
}
23+
24+
describe('allowAny', () => {
25+
function runTestForAliasDeclaration(
26+
code: string,
27+
allowAny: boolean,
28+
expected: boolean,
29+
): void {
30+
const type = getType(code);
31+
const result = containsAllTypesByName(type, allowAny, new Set());
32+
expect(result).toBe(expected);
33+
}
34+
35+
describe('is true', () => {
36+
function runTest(code: string, expected: boolean): void {
37+
runTestForAliasDeclaration(code, true, expected);
38+
}
39+
40+
it.each([
41+
['type Test = unknown;', false],
42+
['type Test = any;', false],
43+
['type Test = string;', false],
44+
])('when code is "%s" expected is %s', runTest);
45+
});
46+
47+
describe('is false', () => {
48+
function runTest(code: string, expected: boolean): void {
49+
runTestForAliasDeclaration(code, false, expected);
50+
}
51+
52+
it.each([
53+
['type Test = unknown;', true],
54+
['type Test = any;', true],
55+
['type Test = string;', false],
56+
])('when code is "%s" expected is %s', runTest);
57+
});
58+
});
59+
60+
describe('matchAnyInstead', () => {
61+
function runTestForAliasDeclaration(
62+
code: string,
63+
matchAnyInstead: boolean,
64+
expected: boolean,
65+
): void {
66+
const type = getType(code);
67+
const result = containsAllTypesByName(
68+
type,
69+
false,
70+
new Set(['Promise', 'Object']),
71+
matchAnyInstead,
72+
);
73+
expect(result).toBe(expected);
74+
}
75+
76+
describe('is true', () => {
77+
function runTest(code: string, expected: boolean): void {
78+
runTestForAliasDeclaration(code, true, expected);
79+
}
80+
81+
it.each([
82+
[`type Test = Promise<void> & string`, true],
83+
['type Test = Promise<void> | string', true],
84+
['type Test = Promise<void> | Object', true],
85+
])('when code is "%s" expected is %s', runTest);
86+
});
87+
88+
describe('is false', () => {
89+
function runTest(code: string, expected: boolean): void {
90+
runTestForAliasDeclaration(code, false, expected);
91+
}
92+
93+
it.each([
94+
['type Test = Promise<void> & string', false],
95+
['type Test = Promise<void> | string', false],
96+
['type Test = Promise<void> | Object', true],
97+
])('when code is "%s" expected is %s', runTest);
98+
});
99+
});
100+
});

0 commit comments

Comments
 (0)
0