@@ -33,6 +33,14 @@ const IGNORED_URLS = [
33
33
'https://portal.azure.com' ,
34
34
]
35
35
36
+ const rootBaseUrl = Cypress . config ( 'baseUrl' )
37
+
38
+ const isInternalUrl = ( url : string ) => {
39
+ // check against the base url
40
+ // and check if the url does not contain a file extension
41
+ return url . startsWith ( rootBaseUrl ) && ! url . includes ( '.' )
42
+ }
43
+
36
44
const isExternalUrl = ( url : string ) => {
37
45
return ! url . includes ( 'localhost' )
38
46
}
@@ -69,14 +77,11 @@ describe('Broken links test suite', () => {
69
77
70
78
links
71
79
. filter ( ( _i , link ) => {
80
+ const href = link . getAttribute ( 'href' )
81
+ const src = link . getAttribute ( 'src' )
82
+
72
83
return ! IGNORED_URLS . some (
73
- ( l ) =>
74
- //@ts -ignore
75
- ( link . getAttribute ( 'href' ) &&
76
- link . getAttribute ( 'href' ) ?. includes ( l ) ) ||
77
- //@ts -ignore
78
- ( link . getAttribute ( 'src' ) &&
79
- link . getAttribute ( 'src' ) . includes ( l ) ) ,
84
+ ( l ) => href ?. includes ( l ) || src ?. includes ( l ) ,
80
85
)
81
86
} )
82
87
. each ( ( link ) => {
@@ -89,6 +94,25 @@ describe('Broken links test suite', () => {
89
94
cy . log ( `link already checked` )
90
95
expect ( VISITED_SUCCESSFUL_LINKS [ url ] ) . to . be . true
91
96
} else {
97
+ // if the link is internal then check the link against the pages fixture (sitemap)
98
+ if ( isInternalUrl ( url ) ) {
99
+ // clean the url by removing the base url and query params
100
+ const rootBaseUrlRegex = new RegExp ( `^${ rootBaseUrl } ` )
101
+ let cleanUrl = url . replace ( rootBaseUrlRegex , '' )
102
+ const queryIndex = cleanUrl . indexOf ( '?' )
103
+ cleanUrl =
104
+ queryIndex !== - 1 ? cleanUrl . slice ( 0 , queryIndex ) : cleanUrl
105
+
106
+ cy . log ( `checking internal link: ${ cleanUrl } ` )
107
+ if ( ! pages . includes ( cleanUrl ) ) {
108
+ assert . fail ( `${ cleanUrl } is not part of the pages fixture` )
109
+ } else {
110
+ VISITED_SUCCESSFUL_LINKS [ url ] = true
111
+ }
112
+
113
+ return
114
+ }
115
+
92
116
cy . wait ( 25 )
93
117
94
118
req ( url ) . then ( ( res : Cypress . Response < any > ) => {
0 commit comments