@@ -2,7 +2,6 @@ import { readFile } from 'fs/promises';
2
2
import { join } from 'path' ;
3
3
import { temporaryFile } from 'tempy' ;
4
4
import { describe , expect , test } from 'vitest' ;
5
- import { builtinHandlers , builtinImporters } from 'react-docgen' ;
6
5
import withFixture from './utils/withFixture' ;
7
6
8
7
describe ( 'cli' , ( ) => {
@@ -172,236 +171,6 @@ describe('cli', () => {
172
171
} ) ;
173
172
} ) ;
174
173
175
- describe ( 'importer' , ( ) => {
176
- describe ( 'accepts the names of builtin importers' , ( ) => {
177
- test . each ( Object . keys ( builtinImporters ) ) ( '%s' , async ( importer ) => {
178
- await withFixture ( 'basic' , async ( { dir, run } ) => {
179
- const { stdout, stderr } = await run ( [
180
- `--importer=${ importer } ` ,
181
- `${ dir } /Component.js` ,
182
- ] ) ;
183
-
184
- expect ( stderr ) . toBe ( '' ) ;
185
- expect ( stdout ) . toContain ( 'Component' ) ;
186
- expect ( ( ) => JSON . parse ( stdout ) ) . not . toThrowError ( ) ;
187
- } ) ;
188
- } ) ;
189
- } ) ;
190
-
191
- describe ( 'custom importer' , ( ) => {
192
- test ( 'accepts an absolute local CommonJS path' , async ( ) => {
193
- await withFixture ( 'custom-importer-cjs' , async ( { dir, run } ) => {
194
- const { stdout, stderr } = await run ( [
195
- `--importer=${ join ( dir , 'importer.cjs' ) } ` ,
196
- `${ dir } /Component.js` ,
197
- ] ) ;
198
-
199
- expect ( stderr ) . toBe ( '' ) ;
200
- expect ( stdout ) . toContain ( '"displayName":"importer"' ) ;
201
- expect ( ( ) => JSON . parse ( stdout ) ) . not . toThrowError ( ) ;
202
- } ) ;
203
- } ) ;
204
-
205
- test ( 'accepts a relative local CommonJS path' , async ( ) => {
206
- await withFixture ( 'custom-importer-cjs' , async ( { dir, run } ) => {
207
- const { stdout, stderr } = await run ( [
208
- '--importer' ,
209
- './importer.cjs' ,
210
- `${ dir } /Component.js` ,
211
- ] ) ;
212
-
213
- expect ( stderr ) . toBe ( '' ) ;
214
- expect ( stdout ) . toContain ( '"displayName":"importer"' ) ;
215
- expect ( ( ) => JSON . parse ( stdout ) ) . not . toThrowError ( ) ;
216
- } ) ;
217
- } ) ;
218
-
219
- test ( 'accepts an absolute local ESM path' , async ( ) => {
220
- await withFixture ( 'custom-importer-esm' , async ( { dir, run } ) => {
221
- const { stdout, stderr } = await run ( [
222
- `--importer=${ join ( dir , 'importer.mjs' ) } ` ,
223
- `${ dir } /Component.js` ,
224
- ] ) ;
225
-
226
- expect ( stderr ) . toBe ( '' ) ;
227
- expect ( stdout ) . toContain ( '"displayName":"importer"' ) ;
228
- expect ( ( ) => JSON . parse ( stdout ) ) . not . toThrowError ( ) ;
229
- } ) ;
230
- } ) ;
231
-
232
- test ( 'accepts a relative local ESM path' , async ( ) => {
233
- await withFixture ( 'custom-importer-esm' , async ( { dir, run } ) => {
234
- const { stdout, stderr } = await run ( [
235
- '--importer' ,
236
- './importer.mjs' ,
237
- `${ dir } /Component.js` ,
238
- ] ) ;
239
-
240
- expect ( stderr ) . toBe ( '' ) ;
241
- expect ( stdout ) . toContain ( '"displayName":"importer"' ) ;
242
- expect ( ( ) => JSON . parse ( stdout ) ) . not . toThrowError ( ) ;
243
- } ) ;
244
- } ) ;
245
-
246
- test ( 'accepts a npm package' , async ( ) => {
247
- await withFixture ( 'custom-importer-npm' , async ( { dir, run } ) => {
248
- const { stdout, stderr } = await run ( [
249
- '--importer=test-react-docgen-importer' ,
250
- `${ dir } /Component.js` ,
251
- ] ) ;
252
-
253
- expect ( stderr ) . toBe ( '' ) ;
254
- expect ( stdout ) . toContain ( '"displayName":"importer"' ) ;
255
- expect ( ( ) => JSON . parse ( stdout ) ) . not . toThrowError ( ) ;
256
- } ) ;
257
- } ) ;
258
-
259
- test ( 'throws error when not found' , async ( ) => {
260
- await withFixture ( 'basic' , async ( { dir, run } ) => {
261
- const { stdout, stderr } = await run ( [
262
- '--importer=does-not-exist' ,
263
- `${ dir } /Component.js` ,
264
- ] ) ;
265
-
266
- expect ( stderr ) . toContain ( 'Unknown importer: "does-not-exist"' ) ;
267
- expect ( stdout ) . toBe ( '' ) ;
268
- } ) ;
269
- } ) ;
270
- } ) ;
271
- } ) ;
272
-
273
- describe ( 'handlers' , ( ) => {
274
- describe ( 'accepts the names of builtin handlers' , ( ) => {
275
- test . each ( Object . keys ( builtinHandlers ) ) ( '%s' , async ( importer ) => {
276
- await withFixture ( 'basic' , async ( { dir, run } ) => {
277
- const { stdout, stderr } = await run ( [
278
- `--handler=${ importer } ` ,
279
- `${ dir } /Component.js` ,
280
- ] ) ;
281
-
282
- expect ( stderr ) . toBe ( '' ) ;
283
- expect ( stdout ) . toContain ( 'Component' ) ;
284
- expect ( ( ) => JSON . parse ( stdout ) ) . not . toThrowError ( ) ;
285
- } ) ;
286
- } ) ;
287
- } ) ;
288
-
289
- describe ( 'multiple handlers' , ( ) => {
290
- test ( 'multiple handlers arguments' , async ( ) => {
291
- await withFixture ( 'basic' , async ( { dir, run } ) => {
292
- const { stdout, stderr } = await run ( [
293
- `--handler=displayNameHandler` ,
294
- `--handler=componentDocblockHandler` ,
295
- `--handler=componentMethodsHandler` ,
296
- `${ dir } /Component.js` ,
297
- ] ) ;
298
-
299
- expect ( stderr ) . toBe ( '' ) ;
300
- expect ( stdout ) . toContain ( '"displayName":"Component"' ) ;
301
- expect ( stdout ) . toContain ( '"description":""' ) ;
302
- expect ( stdout ) . toContain ( '"name":"otherMethod"' ) ;
303
- expect ( ( ) => JSON . parse ( stdout ) ) . not . toThrowError ( ) ;
304
- } ) ;
305
- } ) ;
306
-
307
- test ( 'multiple handlers comma separated' , async ( ) => {
308
- await withFixture ( 'basic' , async ( { dir, run } ) => {
309
- const { stdout, stderr } = await run ( [
310
- `--handler=displayNameHandler,componentDocblockHandler,componentMethodsHandler` ,
311
- `${ dir } /Component.js` ,
312
- ] ) ;
313
-
314
- expect ( stderr ) . toBe ( '' ) ;
315
- expect ( stdout ) . toContain ( '"displayName":"Component"' ) ;
316
- expect ( stdout ) . toContain ( '"description":""' ) ;
317
- expect ( stdout ) . toContain ( '"name":"otherMethod"' ) ;
318
- expect ( ( ) => JSON . parse ( stdout ) ) . not . toThrowError ( ) ;
319
- } ) ;
320
- } ) ;
321
- } ) ;
322
-
323
- describe ( 'custom handlers' , ( ) => {
324
- test ( 'accepts an absolute local CommonJS path' , async ( ) => {
325
- await withFixture ( 'custom-handler-cjs' , async ( { dir, run } ) => {
326
- const { stdout, stderr } = await run ( [
327
- `--handler=${ join ( dir , 'handler.cjs' ) } ` ,
328
- `${ dir } /Component.js` ,
329
- ] ) ;
330
-
331
- expect ( stderr ) . toBe ( '' ) ;
332
- expect ( stdout ) . toContain ( '"displayName":"testhandler"' ) ;
333
- expect ( ( ) => JSON . parse ( stdout ) ) . not . toThrowError ( ) ;
334
- } ) ;
335
- } ) ;
336
-
337
- test ( 'accepts a relative local CommonJS path' , async ( ) => {
338
- await withFixture ( 'custom-handler-cjs' , async ( { dir, run } ) => {
339
- const { stdout, stderr } = await run ( [
340
- '--handler' ,
341
- './handler.cjs' ,
342
- `${ dir } /Component.js` ,
343
- ] ) ;
344
-
345
- expect ( stderr ) . toBe ( '' ) ;
346
- expect ( stdout ) . toContain ( '"displayName":"testhandler"' ) ;
347
- expect ( ( ) => JSON . parse ( stdout ) ) . not . toThrowError ( ) ;
348
- } ) ;
349
- } ) ;
350
-
351
- test ( 'accepts an absolute local ESM path' , async ( ) => {
352
- await withFixture ( 'custom-handler-esm' , async ( { dir, run } ) => {
353
- const { stdout, stderr } = await run ( [
354
- `--handler=${ join ( dir , 'handler.mjs' ) } ` ,
355
- `${ dir } /Component.js` ,
356
- ] ) ;
357
-
358
- expect ( stderr ) . toBe ( '' ) ;
359
- expect ( stdout ) . toContain ( '"displayName":"testhandler"' ) ;
360
- expect ( ( ) => JSON . parse ( stdout ) ) . not . toThrowError ( ) ;
361
- } ) ;
362
- } ) ;
363
-
364
- test ( 'accepts a relative local ESM path' , async ( ) => {
365
- await withFixture ( 'custom-handler-esm' , async ( { dir, run } ) => {
366
- const { stdout, stderr } = await run ( [
367
- '--handler' ,
368
- './handler.mjs' ,
369
- `${ dir } /Component.js` ,
370
- ] ) ;
371
-
372
- expect ( stderr ) . toBe ( '' ) ;
373
- expect ( stdout ) . toContain ( '"displayName":"testhandler"' ) ;
374
- expect ( ( ) => JSON . parse ( stdout ) ) . not . toThrowError ( ) ;
375
- } ) ;
376
- } ) ;
377
-
378
- test ( 'accepts a npm package' , async ( ) => {
379
- await withFixture ( 'custom-handler-npm' , async ( { dir, run } ) => {
380
- const { stdout, stderr } = await run ( [
381
- '--handler=test-react-docgen-handler' ,
382
- `${ dir } /Component.js` ,
383
- ] ) ;
384
-
385
- expect ( stderr ) . toBe ( '' ) ;
386
- expect ( stdout ) . toContain ( '"displayName":"testhandler"' ) ;
387
- expect ( ( ) => JSON . parse ( stdout ) ) . not . toThrowError ( ) ;
388
- } ) ;
389
- } ) ;
390
-
391
- test ( 'throws error when not found' , async ( ) => {
392
- await withFixture ( 'basic' , async ( { dir, run } ) => {
393
- const { stdout, stderr } = await run ( [
394
- '--handler=does-not-exist' ,
395
- `${ dir } /Component.js` ,
396
- ] ) ;
397
-
398
- expect ( stderr ) . toContain ( 'Unknown handler: "does-not-exist"' ) ;
399
- expect ( stdout ) . toBe ( '' ) ;
400
- } ) ;
401
- } ) ;
402
- } ) ;
403
- } ) ;
404
-
405
174
describe ( 'pretty' , ( ) => {
406
175
test ( 'by default does not prettify output' , async ( ) => {
407
176
await withFixture ( 'basic' , async ( { dir, run } ) => {
0 commit comments