11import type { PluginLanguageService } from '../test-utils'
22
3- import { join } from 'node:path'
3+ import ts from 'typescript'
4+ import { join , relative } from 'node:path'
45import {
56 getPluginLanguageService ,
67 getTsFiles ,
78 NEXT_TS_ERRORS ,
89} from '../test-utils'
910
11+ type PartialDiagnostic = Pick <
12+ ts . Diagnostic ,
13+ 'code' | 'messageText' | 'start' | 'length'
14+ >
15+
1016const fixturesDir = join ( __dirname , 'app/warn-no-type' )
1117
1218describe ( 'typescript-plugin - metadata - warn-no-type' , ( ) => {
@@ -20,66 +26,109 @@ describe('typescript-plugin - metadata - warn-no-type', () => {
2026 const hasTypeDir = join ( fixturesDir , 'metadata' , 'has-type' )
2127 const tsFiles = getTsFiles ( hasTypeDir )
2228
29+ const expectedDiagnostics = new Map < string , PartialDiagnostic [ ] > ( )
30+ const totalDiagnostics = new Map < string , PartialDiagnostic [ ] > ( )
31+
2332 for ( const tsFile of tsFiles ) {
24- const diagnostics = languageService . getSemanticDiagnostics ( tsFile )
25- expect ( { diagnostics, tsFile } ) . toEqual ( { diagnostics : [ ] , tsFile } )
33+ const relativePath = relative ( __dirname , tsFile )
34+ // When the test fails, comparing diagnostics to an empty array using
35+ // toEqual() results in terminal output that is too long and omits the
36+ // filename. Therefore, we filter out only the important properties.
37+ const diagnostics = languageService
38+ . getSemanticDiagnostics ( tsFile )
39+ . map ( ( diagnostic ) => ( {
40+ code : diagnostic . code ,
41+ messageText : diagnostic . messageText ,
42+ start : diagnostic . start ,
43+ length : diagnostic . length ,
44+ } ) )
45+
46+ expectedDiagnostics . set ( relativePath , [ ] )
47+ totalDiagnostics . set ( relativePath , diagnostics )
2648 }
49+
50+ expect ( totalDiagnostics ) . toEqual ( expectedDiagnostics )
2751 } )
2852
2953 it ( 'should not have diagnostics for generateMetadata with type' , ( ) => {
3054 const hasTypeDir = join ( fixturesDir , 'generate-metadata' , 'has-type' )
3155 const tsFiles = getTsFiles ( hasTypeDir )
3256
57+ const expectedDiagnostics = new Map < string , PartialDiagnostic [ ] > ( )
58+ const totalDiagnostics = new Map < string , PartialDiagnostic [ ] > ( )
59+
3360 for ( const tsFile of tsFiles ) {
34- const diagnostics = languageService . getSemanticDiagnostics ( tsFile )
35- expect ( { diagnostics, tsFile } ) . toEqual ( { diagnostics : [ ] , tsFile } )
61+ const relativePath = relative ( __dirname , tsFile )
62+ // When the test fails, comparing diagnostics to an empty array using
63+ // toEqual() results in terminal output that is too long and omits the
64+ // filename. Therefore, we filter out only the important properties.
65+ const diagnostics = languageService
66+ . getSemanticDiagnostics ( tsFile )
67+ . map ( ( diagnostic ) => ( {
68+ code : diagnostic . code ,
69+ messageText : diagnostic . messageText ,
70+ start : diagnostic . start ,
71+ length : diagnostic . length ,
72+ } ) )
73+
74+ expectedDiagnostics . set ( relativePath , [ ] )
75+ totalDiagnostics . set ( relativePath , diagnostics )
3676 }
77+
78+ expect ( totalDiagnostics ) . toEqual ( expectedDiagnostics )
3779 } )
3880
3981 it ( 'should have diagnostics for metadata with no type' , ( ) => {
4082 const noTypeDir = join ( fixturesDir , 'metadata' , 'no-type' )
4183 const tsFiles = getTsFiles ( noTypeDir )
4284
85+ const expectedDiagnostics = new Map < string , PartialDiagnostic [ ] > ( )
86+ const totalDiagnostics = new Map < string , PartialDiagnostic [ ] > ( )
87+
4388 for ( const tsFile of tsFiles ) {
89+ const relativePath = relative ( __dirname , tsFile )
4490 const diagnostics = languageService . getSemanticDiagnostics ( tsFile )
4591
46- expect ( { diagnostics, tsFile } ) . toEqual ( {
47- diagnostics : [
48- expect . objectContaining ( {
49- code : NEXT_TS_ERRORS . INVALID_METADATA_EXPORT ,
50- messageText :
51- 'The Next.js "metadata" export should be type of "Metadata" from "next".' ,
52- start : diagnostics [ 0 ] . file . getFullText ( ) . lastIndexOf ( 'metadata' ) ,
53- length : 'metadata' . length ,
54- } ) ,
55- ] ,
56- tsFile,
57- } )
92+ expectedDiagnostics . set ( relativePath , [
93+ expect . objectContaining ( {
94+ code : NEXT_TS_ERRORS . INVALID_METADATA_EXPORT ,
95+ messageText :
96+ 'The Next.js "metadata" export should be type of "Metadata" from "next".' ,
97+ // Use lastIndexOf to match export { ... }
98+ start : ts . sys . readFile ( tsFile ) . lastIndexOf ( 'metadata' ) ,
99+ length : 'metadata' . length ,
100+ } ) ,
101+ ] )
102+ totalDiagnostics . set ( relativePath , diagnostics )
58103 }
104+
105+ expect ( totalDiagnostics ) . toEqual ( expectedDiagnostics )
59106 } )
60107
61108 it ( 'should have diagnostics for generateMetadata with no type' , ( ) => {
62109 const noTypeDir = join ( fixturesDir , 'generate-metadata' , 'no-type' )
63110 const tsFiles = getTsFiles ( noTypeDir )
64111
112+ const expectedDiagnostics = new Map < string , PartialDiagnostic [ ] > ( )
113+ const totalDiagnostics = new Map < string , PartialDiagnostic [ ] > ( )
114+
65115 for ( const tsFile of tsFiles ) {
116+ const relativePath = relative ( __dirname , tsFile )
66117 const diagnostics = languageService . getSemanticDiagnostics ( tsFile )
67118 const type = tsFile . includes ( '-async-' ) ? 'Promise<Metadata>' : 'Metadata'
68119
69- expect ( { diagnostics, tsFile } ) . toEqual ( {
70- diagnostics : [
71- expect . objectContaining ( {
72- code : NEXT_TS_ERRORS . INVALID_METADATA_EXPORT ,
73- messageText : `The Next.js "generateMetadata" export should have a return type of "${ type } " from "next".` ,
74- // Use lastIndexOf to match export { ... }
75- start : diagnostics [ 0 ] . file
76- . getFullText ( )
77- . lastIndexOf ( 'generateMetadata' ) ,
78- length : 'generateMetadata' . length ,
79- } ) ,
80- ] ,
81- tsFile,
82- } )
120+ expectedDiagnostics . set ( relativePath , [
121+ expect . objectContaining ( {
122+ code : NEXT_TS_ERRORS . INVALID_METADATA_EXPORT ,
123+ messageText : `The Next.js "generateMetadata" export should have a return type of "${ type } " from "next".` ,
124+ // Use lastIndexOf to match export { ... }
125+ start : ts . sys . readFile ( tsFile ) . lastIndexOf ( 'generateMetadata' ) ,
126+ length : 'generateMetadata' . length ,
127+ } ) ,
128+ ] )
129+ totalDiagnostics . set ( relativePath , diagnostics )
83130 }
131+
132+ expect ( totalDiagnostics ) . toEqual ( expectedDiagnostics )
84133 } )
85134} )
0 commit comments