55 * LICENSE file in the root directory of this source tree.
66 */
77
8- import remark from 'remark' ;
8+ import { remark } from 'remark' ;
99import dedent from 'dedent' ;
10- import { jest } from '@jest/globals' ;
10+ import { jest , describe , beforeEach , test , expect } from '@jest/globals' ;
1111
12- jest . unstable_mockModule ( '../lib.js' , ( ) => ( {
13- fetch : jest . fn ( ) ,
12+ const mockFetch = jest . fn ( ) as jest . MockedFunction <
13+ ( url : string , method : unknown , options ?: object ) => Promise < number >
14+ > ;
15+
16+ jest . unstable_mockModule ( '../lib.ts' , ( ) => ( {
17+ fetch : mockFetch ,
1418} ) ) ;
1519
16- const { fetch} = await import ( '../lib.js' ) ;
17- const plugin = ( await import ( '../' ) ) . default ;
20+ const plugin = ( await import ( '../index.ts' ) ) . default ;
1821
19- const processMarkdown = ( md , opts ) => {
22+ function processMarkdown ( md : string , opts = { } ) {
2023 return remark ( ) . use ( plugin , opts ) . process ( md ) ;
21- } ;
24+ }
2225
2326describe ( 'remark-lint-no-dead-urls' , ( ) => {
24- beforeEach ( ( ) => fetch . mockReset ( ) ) ;
27+ beforeEach ( ( ) => mockFetch . mockReset ( ) ) ;
2528
26- test ( 'works with no URLs' , ( ) => {
29+ test ( 'works with no URLs' , async ( ) => {
2730 const lint = processMarkdown ( dedent `
2831 # Title
2932
3033 No URLs in here.
3134 ` ) ;
3235
33- return lint . then ( vFile => {
34- expect ( fetch ) . toHaveBeenCalledTimes ( 0 ) ;
35- expect ( vFile . messages . length ) . toBe ( 0 ) ;
36- } ) ;
36+ const vFile = await lint ;
37+ expect ( mockFetch ) . toHaveBeenCalledTimes ( 0 ) ;
38+ expect ( vFile . messages . length ) . toBe ( 0 ) ;
3739 } ) ;
3840
39- test ( 'works a good, bad a local link' , ( ) => {
40- fetch . mockReturnValueOnce ( 200 ) . mockReturnValueOnce ( 404 ) ;
41+ test ( 'works a good, bad a local link' , async ( ) => {
42+ mockFetch . mockResolvedValueOnce ( 200 ) . mockResolvedValueOnce ( 404 ) ;
4143
4244 const lint = processMarkdown (
4345 dedent `
@@ -51,17 +53,16 @@ describe('remark-lint-no-dead-urls', () => {
5153 `
5254 ) ;
5355
54- return lint . then ( vFile => {
55- expect ( fetch ) . toHaveBeenCalledTimes ( 2 ) ;
56- expect ( vFile . messages . length ) . toBe ( 1 ) ;
57- expect ( vFile . messages [ 0 ] . reason ) . toBe (
58- 'Link to https://github.com/unified/oops is broken'
59- ) ;
60- } ) ;
56+ const vFile = await lint ;
57+ expect ( mockFetch ) . toHaveBeenCalledTimes ( 2 ) ;
58+ expect ( vFile . messages . length ) . toBe ( 1 ) ;
59+ expect ( vFile . messages [ 0 ] . reason ) . toBe (
60+ 'Link to https://github.com/unified/oops is broken'
61+ ) ;
6162 } , 15000 ) ;
6263
63- test ( 'works with definitions and images' , ( ) => {
64- fetch . mockReturnValueOnce ( 200 ) . mockReturnValueOnce ( 404 ) ;
64+ test ( 'works with definitions and images' , async ( ) => {
65+ mockFetch . mockResolvedValueOnce ( 200 ) . mockResolvedValueOnce ( 404 ) ;
6566
6667 const lint = processMarkdown (
6768 dedent `
@@ -80,27 +81,25 @@ describe('remark-lint-no-dead-urls', () => {
8081 }
8182 ) ;
8283
83- return lint . then ( vFile => {
84- expect ( fetch ) . toHaveBeenCalledTimes ( 2 ) ;
85- expect ( vFile . messages . length ) . toBe ( 1 ) ;
86- expect ( vFile . messages [ 0 ] . reason ) . toBe ( 'Link to /oops/broken is broken' ) ;
87- } ) ;
84+ const vFile = await lint ;
85+ expect ( mockFetch ) . toHaveBeenCalledTimes ( 2 ) ;
86+ expect ( vFile . messages . length ) . toBe ( 1 ) ;
87+ expect ( vFile . messages [ 0 ] . reason ) . toBe ( 'Link to /oops/broken is broken' ) ;
8888 } ) ;
8989
90- test ( 'skips URLs with unsupported protocols' , ( ) => {
90+ test ( 'skips URLs with unsupported protocols' , async ( ) => {
9191 const lint = processMarkdown ( dedent `
9292 [Send me an email.](mailto:me@me.com)
9393 [Look at this file.](ftp://path/to/file.txt)
9494 [Special schema.](flopper://a/b/c)
9595 ` ) ;
9696
97- return lint . then ( vFile => {
98- expect ( fetch ) . toHaveBeenCalledTimes ( 0 ) ;
99- expect ( vFile . messages . length ) . toBe ( 0 ) ;
100- } ) ;
97+ const vFile = await lint ;
98+ expect ( mockFetch ) . toHaveBeenCalledTimes ( 0 ) ;
99+ expect ( vFile . messages . length ) . toBe ( 0 ) ;
101100 } ) ;
102101
103- test ( 'localhost' , ( ) => {
102+ test ( 'localhost' , async ( ) => {
104103 const lint = processMarkdown (
105104 dedent `
106105 - [http://localhost](http://localhost)
@@ -114,12 +113,11 @@ describe('remark-lint-no-dead-urls', () => {
114113 `
115114 ) ;
116115
117- return lint . then ( vFile => {
118- expect ( vFile . messages . length ) . toBe ( 0 ) ;
119- } ) ;
116+ const vFile = await lint ;
117+ expect ( vFile . messages . length ) . toBe ( 0 ) ;
120118 } ) ;
121119
122- test ( 'local IP 127.0.0.1' , ( ) => {
120+ test ( 'local IP 127.0.0.1' , async ( ) => {
123121 const lint = processMarkdown (
124122 dedent `
125123 - [http://127.0.0.1](http://127.0.0.1)
@@ -133,23 +131,21 @@ describe('remark-lint-no-dead-urls', () => {
133131 `
134132 ) ;
135133
136- return lint . then ( vFile => {
137- expect ( vFile . messages . length ) . toBe ( 0 ) ;
138- } ) ;
134+ const vFile = await lint ;
135+ expect ( vFile . messages . length ) . toBe ( 0 ) ;
139136 } ) ;
140137
141138 test . each ( [
142139 '[Ignore this](http://www.url-to-ignore.com)' ,
143140 '[Ignore this](http://www.url-to-ignore.com/somePath)' ,
144141 '[Ignore this](http://www.url-to-ignore.com/somePath?withQuery=wow)' ,
145142 '[its complicated](http://url-to-ignore.com/somePath/maybe)' ,
146- ] ) ( 'skipUrlPatterns for content: %s' , markdownContent => {
143+ ] ) ( 'skipUrlPatterns for content: %s' , async markdownContent => {
147144 const lint = processMarkdown ( markdownContent , {
148145 skipUrlPatterns : [ / ^ h t t p : \/ \/ ( .* ) u r l - t o - i g n o r e \. c o m / ] ,
149146 } ) ;
150147
151- return lint . then ( vFile => {
152- expect ( vFile . messages . length ) . toBe ( 0 ) ;
153- } ) ;
148+ const vFile = await lint ;
149+ expect ( vFile . messages . length ) . toBe ( 0 ) ;
154150 } ) ;
155151} ) ;
0 commit comments