From d8ca77a81f28028ca863c1429b1efb31040139ba Mon Sep 17 00:00:00 2001 From: "yuya.yoshioka" Date: Sun, 6 Oct 2024 11:45:24 +0900 Subject: [PATCH 1/2] add tests for getConstrainedTypeAtLocation --- .../getConstrainedTypeAtLocation.test.ts | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 packages/type-utils/tests/getConstrainedTypeAtLocation.test.ts diff --git a/packages/type-utils/tests/getConstrainedTypeAtLocation.test.ts b/packages/type-utils/tests/getConstrainedTypeAtLocation.test.ts new file mode 100644 index 000000000000..5464c4dca2b1 --- /dev/null +++ b/packages/type-utils/tests/getConstrainedTypeAtLocation.test.ts @@ -0,0 +1,48 @@ +import type { TSESTree } from '@typescript-eslint/types'; +import type { ParserServicesWithTypeInformation } from '@typescript-eslint/typescript-estree'; +import type * as ts from 'typescript'; + +import { getConstrainedTypeAtLocation } from '../src'; + +const node = {} as TSESTree.Node; + +describe('getConstrainedTypeAtLocation', () => { + describe('when the node has a generic constraint', () => { + it('returns the generic constraint type', () => { + const typeAtLocation = {} as ts.Type; + const constraintOfType = {} as ts.Type; + const typeChecker = { + getBaseConstraintOfType: (_: ts.Type) => constraintOfType, + } as ts.TypeChecker; + const program = { + getTypeChecker: () => typeChecker, + } as ts.Program; + const services = { + program, + getTypeAtLocation: (_: TSESTree.Node) => typeAtLocation, + } as ParserServicesWithTypeInformation; + + expect(getConstrainedTypeAtLocation(services, node)).toBe( + constraintOfType, + ); + }); + }); + + describe('when the node does not have a generic constraint', () => { + it('returns the node type', () => { + const typeAtLocation = {} as ts.Type; + const typeChecker = { + getBaseConstraintOfType: (_: ts.Type) => undefined, + } as ts.TypeChecker; + const program = { + getTypeChecker: () => typeChecker, + } as ts.Program; + const services = { + program, + getTypeAtLocation: (_: TSESTree.Node) => typeAtLocation, + } as ParserServicesWithTypeInformation; + + expect(getConstrainedTypeAtLocation(services, node)).toBe(typeAtLocation); + }); + }); +}); From 1c5b1d6531401728b47ee9516446545307aaf088 Mon Sep 17 00:00:00 2001 From: "yuya.yoshioka" Date: Wed, 16 Oct 2024 00:37:24 +0900 Subject: [PATCH 2/2] create mock function --- .../getConstrainedTypeAtLocation.test.ts | 57 +++++++++++-------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/packages/type-utils/tests/getConstrainedTypeAtLocation.test.ts b/packages/type-utils/tests/getConstrainedTypeAtLocation.test.ts index 5464c4dca2b1..f10c2b9635f2 100644 --- a/packages/type-utils/tests/getConstrainedTypeAtLocation.test.ts +++ b/packages/type-utils/tests/getConstrainedTypeAtLocation.test.ts @@ -6,41 +6,50 @@ import { getConstrainedTypeAtLocation } from '../src'; const node = {} as TSESTree.Node; +const mockType = (): ts.Type => { + return {} as ts.Type; +}; + +const mockServices = ({ + typeAtLocation, + baseConstraintOfType, +}: { + typeAtLocation: ts.Type; + baseConstraintOfType?: ts.Type; +}): ParserServicesWithTypeInformation => { + const typeChecker = { + getBaseConstraintOfType: (_: ts.Type) => baseConstraintOfType, + } as ts.TypeChecker; + const program = { + getTypeChecker: () => typeChecker, + } as ts.Program; + + return { + program, + getTypeAtLocation: (_: TSESTree.Node) => typeAtLocation, + } as ParserServicesWithTypeInformation; +}; + describe('getConstrainedTypeAtLocation', () => { describe('when the node has a generic constraint', () => { it('returns the generic constraint type', () => { - const typeAtLocation = {} as ts.Type; - const constraintOfType = {} as ts.Type; - const typeChecker = { - getBaseConstraintOfType: (_: ts.Type) => constraintOfType, - } as ts.TypeChecker; - const program = { - getTypeChecker: () => typeChecker, - } as ts.Program; - const services = { - program, - getTypeAtLocation: (_: TSESTree.Node) => typeAtLocation, - } as ParserServicesWithTypeInformation; + const typeAtLocation = mockType(); + const baseConstraintOfType = mockType(); + const services = mockServices({ + typeAtLocation, + baseConstraintOfType, + }); expect(getConstrainedTypeAtLocation(services, node)).toBe( - constraintOfType, + baseConstraintOfType, ); }); }); describe('when the node does not have a generic constraint', () => { it('returns the node type', () => { - const typeAtLocation = {} as ts.Type; - const typeChecker = { - getBaseConstraintOfType: (_: ts.Type) => undefined, - } as ts.TypeChecker; - const program = { - getTypeChecker: () => typeChecker, - } as ts.Program; - const services = { - program, - getTypeAtLocation: (_: TSESTree.Node) => typeAtLocation, - } as ParserServicesWithTypeInformation; + const typeAtLocation = mockType(); + const services = mockServices({ typeAtLocation }); expect(getConstrainedTypeAtLocation(services, node)).toBe(typeAtLocation); });