Improve assertGenericTypeAnnotationHasExactlyOneTypeParameter tests (#34942)

Summary:
https://github.com/facebook/react-native/pull/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: https://github.com/facebook/react-native/pull/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
This commit is contained in:
Antoine Doubovetzky
2022-10-19 01:38:28 -07:00
committed by Facebook GitHub Bot
parent aba6be694e
commit 790f40cfeb
@@ -11,7 +11,6 @@
'use-strict';
import {IncorrectlyParameterizedGenericParserError} from '../errors';
import {assertGenericTypeAnnotationHasExactlyOneTypeParameter} from '../parsers-commons';
const {
@@ -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: {
@@ -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',
@@ -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',
@@ -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],
@@ -156,7 +180,9 @@ describe('assertGenericTypeAnnotationHasExactlyOneTypeParameter', () => {
typeAnnotationWithTwoParams,
'Flow',
),
).toThrow(IncorrectlyParameterizedGenericParserError);
).toThrowErrorMatchingInlineSnapshot(
`"Module testModuleName: Generic 'typeAnnotationName' must have exactly one type parameter."`,
);
const typeAnnotationWithNoParams = {
typeParameters: {
@@ -173,7 +199,9 @@ describe('assertGenericTypeAnnotationHasExactlyOneTypeParameter', () => {
typeAnnotationWithNoParams,
'Flow',
),
).toThrow(IncorrectlyParameterizedGenericParserError);
).toThrowErrorMatchingInlineSnapshot(
`"Module testModuleName: Generic 'typeAnnotationName' must have exactly one type parameter."`,
);
});
});