Switched to using jsdom for tests.

pull/28/head
Nicolas Perriault 9 years ago
parent 98ee8f7463
commit 7da6e82fec

@ -18,9 +18,8 @@
"homepage": "https://github.com/mozilla/readability",
"devDependencies": {
"chai": "^2.1.*",
"chai-as-promised": "^4.3.*",
"html": "0.0.*",
"mocha": "^2.2.*",
"readable-proxy": "1.3.*"
"jsdom": "^3.1.2",
"mocha": "^2.2.*"
}
}

@ -1,11 +1,8 @@
var path = require("path");
var fs = require("fs");
var jsdom = require("jsdom");
var prettyPrint = require("html").prettyPrint;
var chai = require("chai");
var chaiAsPromised = require("chai-as-promised");
chai.should();
chai.use(chaiAsPromised);
var expect = chai.expect;
var expect = require("chai").expect;
var testPageRoot = path.join(__dirname, "test-pages");
var testPages = fs.readdirSync(testPageRoot).map(function(dir) {
@ -17,32 +14,30 @@ var testPages = fs.readdirSync(testPageRoot).map(function(dir) {
});
describe("Test page", function() {
var oldLibPath = process.env.READABILITY_LIB_PATH;
process.env.READABILITY_LIB_PATH = path.join(__dirname, "..", "Readability.js");
var scrape = require("readable-proxy").scrape;
testPages.forEach(function(testPage) {
describe(testPage.dir, function() {
it("should render as expected", function() {
// Allows up to 10 seconds for parsing to complete.
// XXX: Scraping is damn slow. Investigate.
this.timeout(10000);
it("should render as expected", function(done) {
var expected = fs.readFileSync(testPage.expected, {encoding: "utf-8"});
return scrape("file://" + testPage.source).catch(function(err) {
throw err;
}).then(function(result) {
// print Readability log messages
(result.consoleLogs || [])
.filter(function(logMessage) {
return logMessage.indexOf("Reader: (Readability)") === 0;
})
.forEach(function(logMessage) {
console.log("[LOG]", logMessage);
});
// normalize html
return prettyPrint(result.content);
}).should.eventually.become(prettyPrint(expected));
jsdom.env(
fs.readFileSync(testPage.source, {encoding: "utf-8"}),
[path.join(__dirname, "..", "Readability.js")],
function (errors, window) {
if (errors) {
throw new Error(errors);
}
var uri = {
spec: "http://fakehost/test/page.html",
host: "fakehost",
prePath: "http://fakehost",
scheme: "http",
pathBase: "http://fakehost/test"
};
var result = new window.Readability(uri, window.document).parse();
expect(prettyPrint(result.content)).eql(prettyPrint(expected))
done();
}
);
});
});
});
process.env.READABILITY_LIB_PATH = oldLibPath;
});

@ -1,5 +1,5 @@
<div id="readability-page-1" class="page"><section>
<p><strong>So finally you're <a href="file:///code/2013/testing-frontend-javascript-code-using-mocha-chai-and-sinon/">testing your frontend JavaScript code</a>? Great! The more you
<p><strong>So finally you're <a href="http://fakehost/code/2013/testing-frontend-javascript-code-using-mocha-chai-and-sinon/">testing your frontend JavaScript code</a>? Great! The more you
write tests, the more confident you are with your code… but how much precisely?
That's where <a href="http://en.wikipedia.org/wiki/Code_coverage">code coverage</a> might
help.</strong></p>
@ -24,7 +24,7 @@ matter of adding this simple line to your HTML test file:</p>
</code></pre>
<p>Source files: <a href="https://raw.github.com/alex-seville/blanket/master/dist/qunit/blanket.min.js">blanket.js</a>,
<a href="https://raw.github.com/alex-seville/blanket/master/src/adapters/mocha-blanket.js">mocha-blanket.js</a></p>
<p>As an example, let's reuse the silly <code>Cow</code> example we used <a href="file:///code/2013/testing-frontend-javascript-code-using-mocha-chai-and-sinon/">in a previous episode</a>:</p>
<p>As an example, let's reuse the silly <code>Cow</code> example we used <a href="http://fakehost/code/2013/testing-frontend-javascript-code-using-mocha-chai-and-sinon/">in a previous episode</a>:</p>
<pre><code>// cow.js
(function(exports) {
"use strict";
@ -98,7 +98,7 @@ Mocha:</p>
<li>The HTML test file <em>must</em> be served over HTTP for the adapter to be loaded.</li>
</ul>
<p>Running the tests now gives us something like this:</p>
<p><img alt="screenshot" src="file:///static/code/2013/blanket-coverage.png"></p>
<p><img alt="screenshot" src="http://fakehost/static/code/2013/blanket-coverage.png"></p>
<p>As you can see, the report at the bottom highlights that we haven't actually
tested the case where an error is raised in case a target name is missing.
We've been informed of that, nothing more, nothing less. We simply know we're

Loading…
Cancel
Save