Skip to content

Commit 790f40c

Browse files
Antoine Doubovetzkyfacebook-github-bot
authored andcommitted
Improve assertGenericTypeAnnotationHasExactlyOneTypeParameter tests (#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
1 parent aba6be6 commit 790f40c

File tree

1 file changed

+40
-12
lines changed

1 file changed

+40
-12
lines changed

packages/react-native-codegen/src/parsers/__tests__/parsers-commons-test.js

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
'use-strict';
1313

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

1716
const {
@@ -87,8 +86,25 @@ describe('unwrapNullable', () => {
8786
});
8887

8988
describe('assertGenericTypeAnnotationHasExactlyOneTypeParameter', () => {
89+
const moduleName = 'testModuleName';
90+
91+
it("doesn't throw any Error when typeAnnotation has exactly one typeParameter", () => {
92+
const typeAnnotation = {
93+
typeParameters: {
94+
type: 'TypeParameterInstantiation',
95+
params: [1],
96+
},
97+
};
98+
expect(() =>
99+
assertGenericTypeAnnotationHasExactlyOneTypeParameter(
100+
moduleName,
101+
typeAnnotation,
102+
'Flow',
103+
),
104+
).not.toThrow();
105+
});
106+
90107
it('throws an IncorrectlyParameterizedGenericParserError if typeParameters is null', () => {
91-
const moduleName = 'testModuleName';
92108
const typeAnnotation = {
93109
typeParameters: null,
94110
id: {
@@ -101,14 +117,16 @@ describe('assertGenericTypeAnnotationHasExactlyOneTypeParameter', () => {
101117
typeAnnotation,
102118
'Flow',
103119
),
104-
).toThrow(IncorrectlyParameterizedGenericParserError);
120+
).toThrowErrorMatchingInlineSnapshot(
121+
`"Module testModuleName: Generic 'typeAnnotationName' must have type parameters."`,
122+
);
105123
});
106124

107-
it("throws an error if typeAnnotation.typeParameters.type doesn't have the correct value depending on language", () => {
108-
const moduleName = 'testModuleName';
125+
it('throws an error if typeAnnotation.typeParameters.type is not TypeParameterInstantiation when language is Flow', () => {
109126
const flowTypeAnnotation = {
110127
typeParameters: {
111-
type: 'TypeParameterInstantiation',
128+
type: 'wrongType',
129+
params: [1],
112130
},
113131
id: {
114132
name: 'typeAnnotationName',
@@ -120,11 +138,16 @@ describe('assertGenericTypeAnnotationHasExactlyOneTypeParameter', () => {
120138
flowTypeAnnotation,
121139
'Flow',
122140
),
123-
).toThrow(Error);
141+
).toThrowErrorMatchingInlineSnapshot(
142+
`"assertGenericTypeAnnotationHasExactlyOneTypeParameter: Type parameters must be an AST node of type 'TypeParameterInstantiation'"`,
143+
);
144+
});
124145

146+
it('throws an error if typeAnnotation.typeParameters.type is not TSTypeParameterInstantiation when language is TypeScript', () => {
125147
const typeScriptTypeAnnotation = {
126148
typeParameters: {
127-
type: 'TypeParameterInstantiation',
149+
type: 'wrongType',
150+
params: [1],
128151
},
129152
typeName: {
130153
name: 'typeAnnotationName',
@@ -136,11 +159,12 @@ describe('assertGenericTypeAnnotationHasExactlyOneTypeParameter', () => {
136159
typeScriptTypeAnnotation,
137160
'TypeScript',
138161
),
139-
).toThrow(Error);
162+
).toThrowErrorMatchingInlineSnapshot(
163+
`"assertGenericTypeAnnotationHasExactlyOneTypeParameter: Type parameters must be an AST node of type 'TSTypeParameterInstantiation'"`,
164+
);
140165
});
141166

142167
it("throws an IncorrectlyParameterizedGenericParserError if typeParameters don't have 1 exactly parameter", () => {
143-
const moduleName = 'testModuleName';
144168
const typeAnnotationWithTwoParams = {
145169
typeParameters: {
146170
params: [1, 2],
@@ -156,7 +180,9 @@ describe('assertGenericTypeAnnotationHasExactlyOneTypeParameter', () => {
156180
typeAnnotationWithTwoParams,
157181
'Flow',
158182
),
159-
).toThrow(IncorrectlyParameterizedGenericParserError);
183+
).toThrowErrorMatchingInlineSnapshot(
184+
`"Module testModuleName: Generic 'typeAnnotationName' must have exactly one type parameter."`,
185+
);
160186

161187
const typeAnnotationWithNoParams = {
162188
typeParameters: {
@@ -173,7 +199,9 @@ describe('assertGenericTypeAnnotationHasExactlyOneTypeParameter', () => {
173199
typeAnnotationWithNoParams,
174200
'Flow',
175201
),
176-
).toThrow(IncorrectlyParameterizedGenericParserError);
202+
).toThrowErrorMatchingInlineSnapshot(
203+
`"Module testModuleName: Generic 'typeAnnotationName' must have exactly one type parameter."`,
204+
);
177205
});
178206
});
179207

0 commit comments

Comments
 (0)