@@ -18,117 +18,61 @@ describe('typescript-plugin - metadata - warn-no-type', () => {
1818 it ( 'should not have diagnostics for metadata with type' , ( ) => {
1919 const hasTypeDir = join ( fixturesDir , 'metadata' , 'has-type' )
2020 const tsFiles = getTsFiles ( hasTypeDir )
21- expect ( tsFiles . length ) . toBe ( 8 )
2221
23- const totalDiagnostics = [ ]
2422 for ( const tsFile of tsFiles ) {
2523 const diagnostics = languageService . getSemanticDiagnostics ( tsFile )
26- if ( diagnostics . length > 0 ) {
27- console . warn (
28- `${ tsFile } \n\nExpected 0 diagnostic but received ${ diagnostics . length } .`
29- )
30- }
31- totalDiagnostics . push ( ...diagnostics )
24+ expect ( diagnostics . length ) . toBe ( 0 )
3225 }
33-
34- expect ( totalDiagnostics . length ) . toBe ( 0 )
3526 } )
3627
3728 it ( 'should not have diagnostics for generateMetadata with type' , ( ) => {
3829 const hasTypeDir = join ( fixturesDir , 'generate-metadata' , 'has-type' )
3930 const tsFiles = getTsFiles ( hasTypeDir )
40- expect ( tsFiles . length ) . toBe ( 48 )
4131
42- const totalDiagnostics = [ ]
4332 for ( const tsFile of tsFiles ) {
4433 const diagnostics = languageService . getSemanticDiagnostics ( tsFile )
45- if ( diagnostics . length > 0 ) {
46- console . warn (
47- `${ tsFile } \n\nExpected 0 diagnostic but received ${ diagnostics . length } .`
48- )
49- }
50- totalDiagnostics . push ( ...diagnostics )
34+ expect ( diagnostics . length ) . toBe ( 0 )
5135 }
52-
53- expect ( totalDiagnostics . length ) . toBe ( 0 )
5436 } )
5537
5638 it ( 'should have diagnostics for metadata with no type' , ( ) => {
5739 const noTypeDir = join ( fixturesDir , 'metadata' , 'no-type' )
5840 const tsFiles = getTsFiles ( noTypeDir )
59- expect ( tsFiles . length ) . toBe ( 4 )
60-
61- const totalDiagnostics = [ ]
6241
6342 for ( const tsFile of tsFiles ) {
6443 const diagnostics = languageService . getSemanticDiagnostics ( tsFile )
65- if ( diagnostics . length !== 1 ) {
66- console . warn (
67- `${ tsFile } \n\nExpected 1 diagnostic but received ${ diagnostics . length } .`
68- )
69- } else {
70- const diagnostic = diagnostics [ 0 ]
71- // Use lastIndexOf to match export { ... }
72- const start = diagnostic . file . getFullText ( ) . lastIndexOf ( 'metadata' )
73-
74- if ( diagnostic . start !== start ) {
75- console . warn (
76- `${ tsFile } \n\nExpected start to be ${ start } but received ${ diagnostic . start } .`
77- )
78- }
79-
80- expect ( diagnostic ) . toMatchObject ( {
81- code : NEXT_TS_ERRORS . INVALID_METADATA_EXPORT ,
82- messageText :
83- 'The Next.js "metadata" export should be type of "Metadata" from "next".' ,
84- start,
85- length : 'metadata' . length ,
86- } )
87- }
44+ expect ( diagnostics . length ) . toBe ( 1 )
8845
89- totalDiagnostics . push ( ...diagnostics )
46+ const diagnostic = diagnostics [ 0 ]
47+ expect ( diagnostic ) . toMatchObject ( {
48+ code : NEXT_TS_ERRORS . INVALID_METADATA_EXPORT ,
49+ messageText :
50+ 'The Next.js "metadata" export should be type of "Metadata" from "next".' ,
51+ // Use lastIndexOf to match export { ... }
52+ start : diagnostic . file . getFullText ( ) . lastIndexOf ( 'metadata' ) ,
53+ length : 'metadata' . length ,
54+ } )
9055 }
91-
92- expect ( totalDiagnostics . length ) . toBe ( 4 )
9356 } )
9457
9558 it ( 'should have diagnostics for generateMetadata with no type' , ( ) => {
9659 const noTypeDir = join ( fixturesDir , 'generate-metadata' , 'no-type' )
9760 const tsFiles = getTsFiles ( noTypeDir )
98- expect ( tsFiles . length ) . toBe ( 24 )
99-
100- const totalDiagnostics = [ ]
10161
10262 for ( const tsFile of tsFiles ) {
10363 const diagnostics = languageService . getSemanticDiagnostics ( tsFile )
104- if ( diagnostics . length !== 1 ) {
105- console . warn (
106- `${ tsFile } \n\nExpected 1 diagnostic but received ${ diagnostics . length } .`
107- )
108- } else {
109- const diagnostic = diagnostics [ 0 ]
110- const isAsync = tsFile . includes ( 'async' )
111- // Use lastIndexOf to match export { ... }
112- const start = diagnostic . file
113- . getFullText ( )
114- . lastIndexOf ( 'generateMetadata' )
64+ expect ( diagnostics . length ) . toBe ( 1 )
11565
116- if ( diagnostic . start !== start ) {
117- console . warn (
118- `${ tsFile } \n\nExpected start to be ${ start } but received ${ diagnostic . start } .`
119- )
120- }
66+ const diagnostic = diagnostics [ 0 ]
67+ const type = tsFile . includes ( '-async-' ) ? 'Promise<Metadata>' : 'Metadata'
12168
122- expect ( diagnostic ) . toMatchObject ( {
123- code : NEXT_TS_ERRORS . INVALID_METADATA_EXPORT ,
124- messageText : `The Next.js "generateMetadata" export should have a return type of ${ isAsync ? '"Promise<Metadata>"' : '"Metadata"' } from "next".` ,
125- start,
126- length : 'generateMetadata' . length ,
127- } )
128- }
129- totalDiagnostics . push ( ...diagnostics )
69+ expect ( diagnostic ) . toMatchObject ( {
70+ code : NEXT_TS_ERRORS . INVALID_METADATA_EXPORT ,
71+ messageText : `The Next.js "generateMetadata" export should have a return type of "${ type } " from "next".` ,
72+ // Use lastIndexOf to match export { ... }
73+ start : diagnostic . file . getFullText ( ) . lastIndexOf ( 'generateMetadata' ) ,
74+ length : 'generateMetadata' . length ,
75+ } )
13076 }
131-
132- expect ( totalDiagnostics . length ) . toBe ( 24 )
13377 } )
13478} )
0 commit comments