Skip to content
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

test(type-utils): add tests for functions in typeFlagUtils #8956

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

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

import { getTypeFlags, isTypeFlagSet } from '../src';
import { expectToHaveParserServices } from './test-utils/expectToHaveParserServices';

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

function getType(code: string): ts.Type {
const { ast, services } = parseForESLint(code, {
project: './tsconfig.json',
filePath: path.join(rootDir, 'file.ts'),
tsconfigRootDir: rootDir,
});
expectToHaveParserServices(services);
const declaration = ast.body[0] as TSESTree.TSTypeAliasDeclaration;

return services.getTypeAtLocation(declaration.id);
}

describe('getTypeFlags', () => {
function runTestForAliasDeclaration(
code: string,
expected: ts.TypeFlags,
): void {
const type = getType(code);
const result = getTypeFlags(type);
expect(result).toBe(expected);
}

it.each([
['type Test = any;', 1],
['type Test = unknown;', 2],
['type Test = string;', 4],
['type Test = number;', 8],
['type Test = "text";', 128],
['type Test = 123;', 256],
['type Test = string | number', 12],
['type Test = "text" | 123', 384],
])('when code is "%s", type flags is %d', runTestForAliasDeclaration);
});

describe('isTypeFlagSet', () => {
function runTestForAliasDeclaration(
code: string,
flagsToCheck: ts.TypeFlags,
expected: boolean,
): void {
const type = getType(code);
const result = isTypeFlagSet(type, flagsToCheck);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the third argument 'isReceiver' in the function 'isTypeFlagSet' necessary?
Considering its usage in the callers of 'isTypeFlagSet', it doesn't seem to be utilized. Hence, I was wondering if it's safe to remove it.

expect(result).toBe(expected);
}

describe('is type flags set', () => {
function runTestIsTypeFlagSet(
code: string,
flagsToCheck: ts.TypeFlags,
): void {
runTestForAliasDeclaration(code, flagsToCheck, true);
}

it.each([
['type Test = any;', ts.TypeFlags.Any],
['type Test = string;', ts.TypeFlags.String],
['type Test = string | number;', ts.TypeFlags.String],
['type Test = string & { foo: string };', ts.TypeFlags.Intersection],
])(
'when code is "%s" and flagsToCheck is %d , returns true',
runTestIsTypeFlagSet,
);
});

describe('is not type flags set', () => {
function runTestIsNotTypeFlagSet(
code: string,
flagsToCheck: ts.TypeFlags,
): void {
runTestForAliasDeclaration(code, flagsToCheck, false);
}

it.each([
['type Test = string', ts.TypeFlags.Any],
['type Test = string | number;', ts.TypeFlags.Any],
['type Test = string & { foo: string }', ts.TypeFlags.String],
])(
'when code is "%s" and flagsToCheck is %d , returns false',
runTestIsNotTypeFlagSet,
);
});
});
});