Skip to content

Commit

Permalink
Improve assertGenericTypeAnnotationHasExactlyOneTypeParameter tests (#…
Browse files Browse the repository at this point in the history
…34942)

Summary:
#34933 has been merged just after I pushed a new commit to the branch to improve tests of `assertGenericTypeAnnotationHasExactlyOneTypeParameter` function, but the last commit was not imported to the internal repository.

The `assertGenericTypeAnnotationHasExactlyOneTypeParameter` can throw different types of Error, and I believe that `.toThrow(Error)` is not specific enough. So I replaced it with `toThrowErrorMatchingInlineSnapshot()`.

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests
-->

[Internal] [Changed] - Improve assertGenericTypeAnnotationHasExactlyOneTypeParameter tests in parsers-commons

Pull Request resolved: #34942

Test Plan: Some test cases were ok because the assertGenericTypeAnnotationHasExactlyOneTypeParameter function threw an Error, but for the wrong reason. I've made sure that the error displayed in the inline snapshot is the one we expect.

Reviewed By: cortinico

Differential Revision: D40384993

Pulled By: cipolleschi

fbshipit-source-id: aaa943be1e808af2c5131f7d06baf24bc3bffa31
  • Loading branch information
Antoine Doubovetzky authored and facebook-github-bot committed Oct 19, 2022
1 parent aba6be6 commit 790f40c
Showing 1 changed file with 40 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

'use-strict';

import {IncorrectlyParameterizedGenericParserError} from '../errors';
import {assertGenericTypeAnnotationHasExactlyOneTypeParameter} from '../parsers-commons';

const {
Expand Down Expand Up @@ -87,8 +86,25 @@ describe('unwrapNullable', () => {
});

describe('assertGenericTypeAnnotationHasExactlyOneTypeParameter', () => {
const moduleName = 'testModuleName';

it("doesn't throw any Error when typeAnnotation has exactly one typeParameter", () => {
const typeAnnotation = {
typeParameters: {
type: 'TypeParameterInstantiation',
params: [1],
},
};
expect(() =>
assertGenericTypeAnnotationHasExactlyOneTypeParameter(
moduleName,
typeAnnotation,
'Flow',
),
).not.toThrow();
});

it('throws an IncorrectlyParameterizedGenericParserError if typeParameters is null', () => {
const moduleName = 'testModuleName';
const typeAnnotation = {
typeParameters: null,
id: {
Expand All @@ -101,14 +117,16 @@ describe('assertGenericTypeAnnotationHasExactlyOneTypeParameter', () => {
typeAnnotation,
'Flow',
),
).toThrow(IncorrectlyParameterizedGenericParserError);
).toThrowErrorMatchingInlineSnapshot(
`"Module testModuleName: Generic 'typeAnnotationName' must have type parameters."`,
);
});

it("throws an error if typeAnnotation.typeParameters.type doesn't have the correct value depending on language", () => {
const moduleName = 'testModuleName';
it('throws an error if typeAnnotation.typeParameters.type is not TypeParameterInstantiation when language is Flow', () => {
const flowTypeAnnotation = {
typeParameters: {
type: 'TypeParameterInstantiation',
type: 'wrongType',
params: [1],
},
id: {
name: 'typeAnnotationName',
Expand All @@ -120,11 +138,16 @@ describe('assertGenericTypeAnnotationHasExactlyOneTypeParameter', () => {
flowTypeAnnotation,
'Flow',
),
).toThrow(Error);
).toThrowErrorMatchingInlineSnapshot(
`"assertGenericTypeAnnotationHasExactlyOneTypeParameter: Type parameters must be an AST node of type 'TypeParameterInstantiation'"`,
);
});

it('throws an error if typeAnnotation.typeParameters.type is not TSTypeParameterInstantiation when language is TypeScript', () => {
const typeScriptTypeAnnotation = {
typeParameters: {
type: 'TypeParameterInstantiation',
type: 'wrongType',
params: [1],
},
typeName: {
name: 'typeAnnotationName',
Expand All @@ -136,11 +159,12 @@ describe('assertGenericTypeAnnotationHasExactlyOneTypeParameter', () => {
typeScriptTypeAnnotation,
'TypeScript',
),
).toThrow(Error);
).toThrowErrorMatchingInlineSnapshot(
`"assertGenericTypeAnnotationHasExactlyOneTypeParameter: Type parameters must be an AST node of type 'TSTypeParameterInstantiation'"`,
);
});

it("throws an IncorrectlyParameterizedGenericParserError if typeParameters don't have 1 exactly parameter", () => {
const moduleName = 'testModuleName';
const typeAnnotationWithTwoParams = {
typeParameters: {
params: [1, 2],
Expand All @@ -156,7 +180,9 @@ describe('assertGenericTypeAnnotationHasExactlyOneTypeParameter', () => {
typeAnnotationWithTwoParams,
'Flow',
),
).toThrow(IncorrectlyParameterizedGenericParserError);
).toThrowErrorMatchingInlineSnapshot(
`"Module testModuleName: Generic 'typeAnnotationName' must have exactly one type parameter."`,
);

const typeAnnotationWithNoParams = {
typeParameters: {
Expand All @@ -173,7 +199,9 @@ describe('assertGenericTypeAnnotationHasExactlyOneTypeParameter', () => {
typeAnnotationWithNoParams,
'Flow',
),
).toThrow(IncorrectlyParameterizedGenericParserError);
).toThrowErrorMatchingInlineSnapshot(
`"Module testModuleName: Generic 'typeAnnotationName' must have exactly one type parameter."`,
);
});
});

Expand Down

0 comments on commit 790f40c

Please sign in to comment.