Thanks to visit codestin.com
Credit goes to github.com

Skip to content

test(type-utils): add tests for isSymbolFromDefaultLibrary #9385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions packages/type-utils/tests/isSymbolFromDefaultLibrary.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { parseForESLint } from '@typescript-eslint/parser';
import type { TSESTree } from '@typescript-eslint/typescript-estree';
import path from 'path';
import type * as ts from 'typescript';

import { isSymbolFromDefaultLibrary } from '../src';
import { expectToHaveParserServices } from './test-utils/expectToHaveParserServices';

describe('isSymbolFromDefaultLibrary', () => {
const rootDir = path.join(__dirname, 'fixtures');

function getTypes(code: string): {
program: ts.Program;
symbol: ts.Symbol | undefined;
} {
const { services, ast } = parseForESLint(code, {
project: './tsconfig.json',
filePath: path.join(rootDir, 'file.ts'),
tsconfigRootDir: rootDir,
});
expectToHaveParserServices(services);
const declaration = ast.body[0] as TSESTree.TSTypeAliasDeclaration;
const type = services.getTypeAtLocation(declaration.id);
return { program: services.program, symbol: type.getSymbol() };
}

function runTestForAliasDeclaration(code: string, expected: boolean): void {
const { program, symbol } = getTypes(code);
const result = isSymbolFromDefaultLibrary(program, symbol);
expect(result).toBe(expected);
}

describe('is symbol from default library', () => {
function runTest(code: string): void {
runTestForAliasDeclaration(code, true);
}

it.each([
['type Test = Array<number>;'],
['type Test = Map<string,number>;'],
['type Test = Promise<void>'],
['type Test = Error'],
['type Test = Object'],
])('when code is %s, returns true', runTest);
});

describe('is not symbol from default library', () => {
function runTest(code: string): void {
runTestForAliasDeclaration(code, false);
}

it.each([
['const test: Array<number> = [1,2,3];'],
['type Test = number;'],
['interface Test { bar: string; };'],
])('when code is %s, returns false', runTest);
});
});
Loading