Skip to content

Commit

Permalink
Add "external" tests
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Mar 25, 2017
1 parent 677b36e commit 3ca104a
Showing 1 changed file with 118 additions and 0 deletions.
118 changes: 118 additions & 0 deletions test/afterEach.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
const expect = require('unexpected');
const pathModule = require('path');
const childProcess = require('child_process');

describe('in afterEach mode', function () {
const fs = expect.promise.promisifyAll(require('fs'));
const tmpDir = pathModule.resolve(__dirname, 'tmp');

before(() => fs.mkdirAsync(tmpDir).catch(() => {}));
after(() => fs.rmdirAsync(tmpDir).catch(() => {}));

const preamble =
"var httpception = require('../../');\n" +
"var expect = require('unexpected').use(require('unexpected-http'));\n";

expect.addAssertion('<function> when run through mocha <assertion>', (expect, subject) => {
expect.errorMode = 'nested';
const code = subject.toString().replace(/^[^{]+\{|\}\s*$/g, '');
expect.subjectOutput = function (output) {
output.code(code, 'javascript');
};
const tmpFileName = pathModule.resolve(tmpDir, 'httpception' + Math.round(10000000 * Math.random()) + '.js');
const testCommand = process.argv[0] + ' ' + pathModule.resolve(__dirname, '..', 'node_modules', '.bin', 'mocha') + ' ' + tmpFileName;

return fs.writeFileAsync(tmpFileName, preamble + code, 'utf-8')
.then(() => expect.promise.fromNode(cb => childProcess.exec(testCommand, cb.bind(null, null))))
.then(([stdout, stderr]) => expect.shift(stderr))
.finally(() => fs.unlinkAsync(tmpFileName));
});

it('should succeed when the correct HTTP request is made', function () {
return expect(() => {
/* eslint-disable */
it('should foo', function () {
httpception({ request: 'GET /', response: 200 });
return expect('GET /', 'to yield response', 200);
});
/* eslint-enable */
}, 'when run through mocha', expect.it('to contain', '✓ should foo').and('to contain', '1 passing'));
});

it('should fail with a diff when too few requests are made', function () {
return expect(() => {
/* eslint-disable */
it('should foo', function () {
httpception({ request: 'GET /', response: 200 });
});
/* eslint-enable */
}, 'when run through mocha to match', /"after each" hook for "should foo"[\s\S]*\/\/ missing:\n\/\/ GET \/\n/);
});

it('should fail with a diff when a request does not match the mocked out traffic', function () {
return expect(() => {
/* eslint-disable */
it('should foo', function () {
httpception({ request: 'GET /foo', response: 200 });
return expect('/bar', 'to yield response', 200);
});
/* eslint-enable */
}, 'when run through mocha to contain',
'GET /bar HTTP/1.1 // should be GET /foo\n' +
' //\n' +
' // -GET /bar HTTP/1.1\n' +
' // +GET /foo HTTP/1.1\n' +
'Host: localhost\n' +
'\n' +
'HTTP/1.1 200 OK\n'
);
});

it('should fail with a diff the first test out of two fails', function () {
return expect(() => {
/* eslint-disable */
it('should foo', function () {
httpception({ request: 'GET /foo', response: 200 });
return expect('/bar', 'to yield response', 200);
});

it('should bar', function () {
httpception({ request: 'GET /foo', response: 200 });
return expect('GET /bar', 'to yield response', 200);;
});
/* eslint-enable */
}, 'when run through mocha to contain',
'GET /bar HTTP/1.1 // should be GET /foo\n' +
' //\n' +
' // -GET /bar HTTP/1.1\n' +
' // +GET /foo HTTP/1.1\n' +
'Host: localhost\n' +
'\n' +
'HTTP/1.1 200 OK\n'
);
});

it('should fail with a diff the second test out of two fails', function () {
return expect(() => {
/* eslint-disable */
it('should bar', function () {
httpception({ request: 'GET /foo', response: 200 });
return expect('GET /bar', 'to yield response', 200);;
});

it('should foo', function () {
httpception({ request: 'GET /foo', response: 200 });
return expect('/bar', 'to yield response', 200);
});
/* eslint-enable */
}, 'when run through mocha to contain',
'GET /bar HTTP/1.1 // should be GET /foo\n' +
' //\n' +
' // -GET /bar HTTP/1.1\n' +
' // +GET /foo HTTP/1.1\n' +
'Host: localhost\n' +
'\n' +
'HTTP/1.1 200 OK\n'
);
});
});

0 comments on commit 3ca104a

Please sign in to comment.