From 790f40cfeb446b8a902ee48ecaa2dc8d4a0cd644 Mon Sep 17 00:00:00 2001 From: Antoine Doubovetzky Date: Wed, 19 Oct 2022 01:38:28 -0700 Subject: [PATCH] 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 [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 --- .../parsers/__tests__/parsers-commons-test.js | 52 ++++++++++++++----- 1 file changed, 40 insertions(+), 12 deletions(-) diff --git a/packages/react-native-codegen/src/parsers/__tests__/parsers-commons-test.js b/packages/react-native-codegen/src/parsers/__tests__/parsers-commons-test.js index 041737dddc1..35b1173c931 100644 --- a/packages/react-native-codegen/src/parsers/__tests__/parsers-commons-test.js +++ b/packages/react-native-codegen/src/parsers/__tests__/parsers-commons-test.js @@ -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."`, + ); }); });