@@ -13,7 +13,12 @@ import type {RouteConfig} from '@docusaurus/types';
13
13
type Params = Parameters < typeof handleBrokenLinks > [ 0 ] ;
14
14
15
15
// We don't need all the routes attributes for our tests
16
- type SimpleRoute = { path : string ; routes ?: SimpleRoute [ ] } ;
16
+ type SimpleRoute = {
17
+ path : string ;
18
+ routes ?: SimpleRoute [ ] ;
19
+ exact ?: boolean ;
20
+ strict ?: boolean ;
21
+ } ;
17
22
18
23
// Conveniently apply defaults to function under test
19
24
async function testBrokenLinks ( params : {
@@ -43,6 +48,52 @@ describe('handleBrokenLinks', () => {
43
48
} ) ;
44
49
} ) ;
45
50
51
+ it ( 'accepts valid non-exact link' , async ( ) => {
52
+ await testBrokenLinks ( {
53
+ routes : [ { path : '/page1' , exact : false } , { path : '/page2/' } ] ,
54
+ collectedLinks : {
55
+ '/page1' : {
56
+ links : [
57
+ '/page1' ,
58
+ '/page1/' ,
59
+ '/page2' ,
60
+ '/page2/' ,
61
+ '/page1/subPath' ,
62
+ '/page2/subPath' ,
63
+ ] ,
64
+ anchors : [ ] ,
65
+ } ,
66
+ '/page2/' : {
67
+ links : [
68
+ '/page1' ,
69
+ '/page1/' ,
70
+ '/page2' ,
71
+ '/page2/' ,
72
+ '/page1/subPath' ,
73
+ '/page2/subPath' ,
74
+ ] ,
75
+ anchors : [ ] ,
76
+ } ,
77
+ } ,
78
+ } ) ;
79
+ } ) ;
80
+
81
+ it ( 'accepts valid non-strict link' , async ( ) => {
82
+ await testBrokenLinks ( {
83
+ routes : [ { path : '/page1' , strict : false } , { path : '/page2/' } ] ,
84
+ collectedLinks : {
85
+ '/page1' : {
86
+ links : [ '/page1' , '/page1/' , '/page2' , '/page2/' ] ,
87
+ anchors : [ ] ,
88
+ } ,
89
+ '/page2/' : {
90
+ links : [ '/page1' , '/page1/' , '/page2' , '/page2/' ] ,
91
+ anchors : [ ] ,
92
+ } ,
93
+ } ,
94
+ } ) ;
95
+ } ) ;
96
+
46
97
it ( 'accepts valid link to uncollected page' , async ( ) => {
47
98
await testBrokenLinks ( {
48
99
routes : [ { path : '/page1' } , { path : '/page2' } ] ,
@@ -292,6 +343,76 @@ describe('handleBrokenLinks', () => {
292
343
` ) ;
293
344
} ) ;
294
345
346
+ it ( 'rejects broken link due to strict matching' , async ( ) => {
347
+ await expect ( ( ) =>
348
+ testBrokenLinks ( {
349
+ routes : [
350
+ { path : '/page1' , strict : true } ,
351
+ { path : '/page2/' , strict : true } ,
352
+ ] ,
353
+
354
+ collectedLinks : {
355
+ '/page1' : {
356
+ links : [ '/page1' , '/page1/' , '/page2' , '/page2/' ] ,
357
+ anchors : [ ] ,
358
+ } ,
359
+ '/page2/' : {
360
+ links : [ '/page1' , '/page1/' , '/page2' , '/page2/' ] ,
361
+ anchors : [ ] ,
362
+ } ,
363
+ } ,
364
+ } ) ,
365
+ ) . rejects . toThrowErrorMatchingInlineSnapshot ( `
366
+ "Docusaurus found broken links!
367
+
368
+ Please check the pages of your site in the list below, and make sure you don't reference any path that does not exist.
369
+ Note: it's possible to ignore broken links with the 'onBrokenLinks' Docusaurus configuration, and let the build pass.
370
+
371
+ Exhaustive list of all broken links found:
372
+ - Broken link on source page path = /page1:
373
+ -> linking to /page2
374
+ - Broken link on source page path = /page2/:
375
+ -> linking to /page2
376
+ "
377
+ ` ) ;
378
+ } ) ;
379
+
380
+ it ( 'rejects broken link due to strict exact matching' , async ( ) => {
381
+ await expect ( ( ) =>
382
+ testBrokenLinks ( {
383
+ routes : [
384
+ { path : '/page1' , exact : true , strict : true } ,
385
+ { path : '/page2/' , exact : true , strict : true } ,
386
+ ] ,
387
+
388
+ collectedLinks : {
389
+ '/page1' : {
390
+ links : [ '/page1' , '/page1/' , '/page2' , '/page2/' ] ,
391
+ anchors : [ ] ,
392
+ } ,
393
+ '/page2/' : {
394
+ links : [ '/page1' , '/page1/' , '/page2' , '/page2/' ] ,
395
+ anchors : [ ] ,
396
+ } ,
397
+ } ,
398
+ } ) ,
399
+ ) . rejects . toThrowErrorMatchingInlineSnapshot ( `
400
+ "Docusaurus found broken links!
401
+
402
+ Please check the pages of your site in the list below, and make sure you don't reference any path that does not exist.
403
+ Note: it's possible to ignore broken links with the 'onBrokenLinks' Docusaurus configuration, and let the build pass.
404
+
405
+ Exhaustive list of all broken links found:
406
+ - Broken link on source page path = /page1:
407
+ -> linking to /page1/
408
+ -> linking to /page2
409
+ - Broken link on source page path = /page2/:
410
+ -> linking to /page1/
411
+ -> linking to /page2
412
+ "
413
+ ` ) ;
414
+ } ) ;
415
+
295
416
it ( 'rejects broken link with anchor' , async ( ) => {
296
417
await expect ( ( ) =>
297
418
testBrokenLinks ( {
@@ -728,6 +849,10 @@ describe('handleBrokenLinks', () => {
728
849
const routes : SimpleRoute [ ] = [
729
850
...Array . from < SimpleRoute > ( { length : scale } ) . map ( ( _ , i ) => ( {
730
851
path : `/page${ i } ` ,
852
+ exact : true ,
853
+ } ) ) ,
854
+ ...Array . from < SimpleRoute > ( { length : scale } ) . map ( ( _ , i ) => ( {
855
+ path : `/page/nonExact/${ i } ` ,
731
856
} ) ) ,
732
857
...Array . from < SimpleRoute > ( { length : scale } ) . fill ( {
733
858
path : '/pageDynamic/:subpath1' ,
@@ -741,6 +866,7 @@ describe('handleBrokenLinks', () => {
741
866
links : [
742
867
...Array . from < SimpleRoute > ( { length : scale } ) . flatMap ( ( _2 , j ) => [
743
868
`/page${ j } ` ,
869
+ `/page/nonExact/${ j } ` ,
744
870
`/page${ j } ?age=42` ,
745
871
`/page${ j } #anchor${ j } ` ,
746
872
`/page${ j } ?age=42#anchor${ j } ` ,
@@ -770,9 +896,9 @@ describe('handleBrokenLinks', () => {
770
896
// See https://github.com/facebook/docusaurus/issues/9754
771
897
// See https://twitter.com/sebastienlorber/status/1749392773415858587
772
898
// We expect no more matchRoutes calls than number of dynamic route links
773
- expect ( matchRoutesMock ) . toHaveBeenCalledTimes ( scale ) ;
899
+ expect ( matchRoutesMock ) . toHaveBeenCalledTimes ( scale * 2 ) ;
774
900
// We expect matchRoutes to be called with a reduced number of routes
775
- expect ( routes ) . toHaveLength ( scale * 2 ) ;
776
- expect ( matchRoutesMock . mock . calls [ 0 ] ! [ 0 ] ) . toHaveLength ( scale ) ;
901
+ expect ( routes ) . toHaveLength ( scale * 3 ) ;
902
+ expect ( matchRoutesMock . mock . calls [ 0 ] ! [ 0 ] ) . toHaveLength ( scale * 2 ) ;
777
903
} ) ;
778
904
} ) ;
0 commit comments