Skip to content

Commit

Permalink
Make documentation tests run in the browser
Browse files Browse the repository at this point in the history
  • Loading branch information
sunesimonsen committed Apr 8, 2015
1 parent 43c35ba commit d0432b8
Show file tree
Hide file tree
Showing 8 changed files with 1,098 additions and 839 deletions.
2 changes: 1 addition & 1 deletion documentation/assertions/any/to-equal.md
Expand Up @@ -66,7 +66,7 @@ expected [ 0, 1, 2, 4, 5 ] to equal [ 1, 2, 3, 4 ]

A diff between two buffers.

```javascript
```javascript#skipBrowser:true
expect(
new Buffer('\x00\x01\x02Here is the thing I was talking about', 'utf-8'),
'to equal',
Expand Down
8 changes: 4 additions & 4 deletions documentation/assertions/function/to-throw.md
Expand Up @@ -30,7 +30,7 @@ expect(function () {

In case of a failing expectation you get the following output:

```javascript
```javascript#skipPhantom:true
expect(function () {
throw new Error('The error message!');
}, 'to throw', 'The error message');
Expand Down Expand Up @@ -59,7 +59,7 @@ expect(function () {

In case of a failing expectation you get the following output:

```javascript
```javascript#skipPhantom:true
expect(function () {
throw new Error('The error message!');
}, 'to throw', /catastrophic failure/);
Expand Down Expand Up @@ -87,7 +87,7 @@ expect(function () {

In case of a failing expectation you get the following output:

```javascript
```javascript#skipPhantom:true
expect(function () {
throw new Error('Another error');
}, 'to throw', function (e) {
Expand Down Expand Up @@ -117,7 +117,7 @@ expect(function () {

In case of a failing expectation you get the following output:

```javascript
```javascript#skipPhantom:true
expect(function () {
throw new Error('threw anyway');
}, 'not to throw');
Expand Down
6 changes: 4 additions & 2 deletions lib/Unexpected.js
Expand Up @@ -786,8 +786,10 @@ Unexpected.prototype.expect = function expect(subject, testDescriptionString) {
return promise;
} catch (e) {
if (e && e._isUnexpected) {
truncateStack(e, that.expect);
that.setErrorMessage(e);
var clonedError = cloneError(e);
truncateStack(clonedError, that.expect);
that.setErrorMessage(clonedError);
throw clonedError;
}
throw e;
}
Expand Down
41 changes: 32 additions & 9 deletions site/metalsmith-unexpected-markdown.js
Expand Up @@ -76,7 +76,7 @@ function extractTests(codeBlocks) {
evaluatedExampleIndex = index;
tests.push({
code: codeBlock.code,
async: !!flags.async
flags: flags
});
}
break;
Expand All @@ -102,9 +102,17 @@ function writeTestsToFile(exampleTests, done) {
pen.text('// THIS FILE IS AUTOGENERATED! DO NOT CHANGE IT MANUALLY.').nl();
pen.text('// It is built based on the examples in the documentation folder').nl();
pen.text('// when the documentation site gets build by running "make site-build".').nl();
pen.text('var expect = require("../").clone();').nl(2);
pen.text('it.skipIf = function (condition) {').nl();
pen.text(' (condition ? it.skip : it).apply(it, Array.prototype.slice.call(arguments, 1));').nl();
pen.text('};').nl(2);

pen.text('expect.addAssertion("to have message", function (expect, subject, value) {').nl();
pen.text('describe("documentation tests", function () {').nl();
pen.indentLines();
pen.i().text("var expect = typeof weknowhow === 'undefined' ? require('../lib/').clone() : weknowhow.expect.clone();").nl(2);
pen.i().text("var isBrowser = typeof weknowhow !== 'undefined';").nl();
pen.i().text("var isPhantom = typeof mochaPhantomJS !== 'undefined';").nl();

pen.i().text('expect.addAssertion("to have message", function (expect, subject, value) {').nl();
pen.indentLines();
pen.i().block(function () {
this.text('var message;').nl();
Expand All @@ -119,10 +127,8 @@ function writeTestsToFile(exampleTests, done) {
this.text('expect(message, "to equal", value);');
}).nl();
pen.outdentLines();
pen.text('});').nl(2);
pen.i().text('});').nl(2);

pen.text('describe("documentation tests", function () {').nl();
pen.indentLines();
pen.i().text('beforeEach(function () {').nl();
pen.indentLines();
pen.i().text('expect = expect.clone();').nl();
Expand All @@ -148,10 +154,22 @@ function writeTestsToFile(exampleTests, done) {
pen.nl();
}

if (test.async) {
var lines;
if (test.output) {
var conditions = [];
if (test.flags.skipPhantom) {
conditions.push('!isPhantom');
}
if (test.flags.skipBrowser) {
conditions.push('!isBrowser');
}

if (conditions.length > 0) {
pen.i().text('if (').text(conditions.join(' || ')).text(') {').nl();
pen.indentLines();
}

var lines;
if (test.flags.async) {
if (test.output) {
pen.i().text('promises.push(expect.promise(function () {').nl();
pen.indentLines();
pen.i().block('text', test.code).nl();
Expand Down Expand Up @@ -235,6 +253,11 @@ function writeTestsToFile(exampleTests, done) {
}
}

if (conditions.length > 0) {
pen.i().text('}').nl();
pen.outdentLines();
}

pen.nl();
});

Expand Down

0 comments on commit d0432b8

Please sign in to comment.