Last active
April 5, 2019 14:49
-
-
Save jamesthomasonjr/d63a4fe6d53938e3f97e6039b4a19605 to your computer and use it in GitHub Desktop.
Shoving broken-link-checker into mocha
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { describe, it, Suite, suite, Test, run} from '@framework'; | |
import blc from 'broken-link-checker'; | |
import data from '@data/navigation.link.json'; | |
const feature: string = 'No links are broken'; | |
const cannotRunTests = () => { | |
describe(feature, () => { | |
it("Mocha is started with the delay flag so link checker tests can run", () => { | |
throw new Error("Mocha must be ran with the --delay flag for the link checker tests to work"); | |
}) | |
}); | |
}; | |
const runTests = () => { | |
// @TODO: Maybe get these from a config somewhere one day. | |
const debug: boolean = /* data.debug; */ false; | |
const color: boolean = /* data.color; */ false; | |
const verbose: boolean = /* data.verbose; */ false; | |
// Using new `Suite(feature)` instead of `suite(feature, () => {})` doesn't work, because it doesn't get added to the root mocha suite. | |
const topSuite: Suite = suite(feature, () => {}); // The top suite represents the site as a whole | |
let currentSuite: Suite = topSuite; // Each child suite represents a page (each test represents a link) | |
const checker = new blc.SiteChecker({}, { | |
junk: (result) => skippedLink(result), | |
link: (result) => testedLink(result), | |
html: (tree, robots, response, pageUrl) => newPage(pageUrl), | |
site: () => testSuiteComplete() | |
}); | |
const skippedLink = (result): void => { | |
const testName = getTestName(result); | |
recordSkippedLink(); | |
currentSuite.addTest(new Test(testName, () => Promise.resolve())); | |
}; | |
const testedLink = (result): void => { | |
const testName = getTestName(result); | |
if (result.excluded) { | |
skippedLink(result); | |
} else if (result.broken) { | |
recordBadLink(); | |
currentSuite.addTest(new Test(testName, () => Promise.reject(new Error(blc[result.brokenReason])))); | |
} else { | |
recordGoodLink(); | |
currentSuite.addTest(new Test(testName, () => Promise.resolve())); | |
} | |
}; | |
const newPage = (pageUrl: string): void => { | |
const suite = new Suite(`Page: ${pageUrl}`); | |
topSuite.addSuite(suite) | |
currentSuite = suite; | |
}; | |
const testSuiteComplete = (): void => { | |
if (verbose) { | |
console.log(); | |
} | |
console.log("Done checking for broken links."); | |
run(); | |
}; | |
// @TODO: Maybe update these three recordXLink methods to be easier on the eyes one day | |
const recordSkippedLink = (): void => { | |
if (debug) { | |
if (color) { | |
process.stdout.write('\x1b[33m'); | |
} | |
process.stdout.write('.'); | |
if (color) { | |
process.stdout.write('\x1b[0m'); | |
} | |
} | |
}; | |
const recordGoodLink = (): void => { | |
if (verbose) { | |
if (color) { | |
process.stdout.write('\x1b[32m'); | |
} | |
process.stdout.write('+'); | |
if (color) { | |
process.stdout.write('\x1b[0m'); | |
} | |
} | |
}; | |
const recordBadLink = (): void => { | |
if (verbose) { | |
if (color) { | |
process.stdout.write('\x1b[31m'); | |
} | |
process.stdout.write('-'); | |
if (color) { | |
process.stdout.write('\x1b[0m'); | |
} | |
} | |
}; | |
const getTestName = (result): string => { | |
if (result.html.text) { | |
return `text: ${result.html.text}\nhref: ${result.url.original}`; | |
} | |
return `href: ${result.url.original}`; | |
}; | |
console.log("Checking for broken links..."); | |
checker.enqueue(data.halDoc); | |
}; | |
if (!run) { | |
cannotRunTests(); | |
} else { | |
runTests(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment