UNPKG

1.88 kBMarkdownView Raw
1# Check Broken Links
2
3[![Build Status](https://travis-ci.org/actuallymentor/check-broken-links.svg?branch=master)](https://travis-ci.org/actuallymentor/check-broken-links)
4
5An npm package that checks an array of supplied links for broken links.
6
7```js
8const check = require( 'check-broken-links' )
9const containsBroken = [
10 'https://www.iCONTAINbrokenlinks.com',
11 'https://www.iAMbroken.com',
12 'https://www.iamfine.com'
13]
14check( 'https://base.url/', containsBroken ).then( brokenlinks => {
15 console.log( brokenlinks )
16 /*
17 { top: [ { url: 'https://www.iAMbroken.com', err: [Object] } ],
18 crawled:
19 [ { url: 'https://iwasinside.com/iCONTAINbrokenlinks ',
20 err: 404 },
21 { url: 'https://iwasalsoinside.com/iCONTAINbrokenlinks',
22 err: 404 }
23 ] }
24 */
25} )
26```
27
28The base url for the relative links has a required trailing slash.
29
30## Mocha testing example
31
32Let's say you want to check if your project has any broken links:
33
34```js
35// Get polyfill so we can use full ES6 in the tests
36import 'babel-polyfill'
37
38// Get the expect functionality
39import { expect } from 'chai'
40
41// Display results as table
42import 'console.table'
43
44// We do not use arrow syntax here because that would break the this.timeout
45describe( 'Links in the project', function( ) {
46
47 // Set the timeouts high so that all links can be checked without many or slow requests crashing the test
48 this.timeout( process.env.maxtimeout || ( 1000 * 60 * 5 ) )
49
50 // The current setup uses mocha in a promise fashion
51 // You could also have the callback be done => {}, but then need to call done() after the expect()
52 it( 'All return 200', () => {
53 return ThisPromiseReturnsAllLinks()
54 .then( brokenlinkarray => {
55 if ( brokenlinkarray.length > 0 ) console.table( brokenlinkarray )
56 expect( brokenlinkarray.length ).to.equal( 0 )
57 } )
58 } )
59} )
60```