Skip to content

Commit

Permalink
Merge pull request #4 from papandreou/feature/jest
Browse files Browse the repository at this point in the history
Start testing against jest too
  • Loading branch information
gustavnikolaj committed Oct 11, 2017
2 parents 4d0d481 + 2c3fdf2 commit 39c3641
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 119 deletions.
5 changes: 5 additions & 0 deletions integration-test/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
rootDir: require('path').resolve(__dirname, '..'),
testEnvironment: 'node',
testMatch: ['<rootDir>/integration-test/tmp/httpception*.js']
};
213 changes: 213 additions & 0 deletions integration-test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
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|jest) <assertion>', (expect, subject) => {
expect.errorMode = 'nested';
const isMocha = expect.alternations[0] === 'mocha';
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');
var testCommand;
if (isMocha) {
testCommand = process.argv[0] + ' ' + pathModule.resolve(__dirname, '..', 'node_modules', '.bin', 'mocha') + ' ' + tmpFileName;
} else {
// jest
testCommand =
process.argv[0] + ' ' +
pathModule.resolve(__dirname, '..', 'node_modules', '.bin', 'jest') +
' --config ' + pathModule.resolve(__dirname, 'jest.config.js') +
' ' + tmpFileName;
}

return fs.writeFileAsync(tmpFileName, preamble + code, 'utf-8')
.then(() => expect.promise.fromNode(cb => childProcess.exec(testCommand, cb.bind(null, null))))
.then((output) => expect.shift({ stdout: output[1], stderr: output[2] }))
.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 to satisfy', {
stdout: expect.it('to contain', '✓ should foo').and('to contain', '1 passing')
}).and('when run through jest to satisfy', {
stderr: expect.it('to contain', '✓ should foo').and('to contain', '1 passed, 1 total')
});
});

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 satisfy', {
stdout: /"after each" hook for "should foo"[\s\S]*\/\/ missing:\n\/\/ GET \/\n/
}).and('when run through jest to satisfy', {
stderr: expect.it('to contain', '✕ should foo').and('to contain', '1 failed, 1 total')
});
});

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 satisfy', {
stdout: expect.it(
'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'
)
}).and('when run through jest to satisfy', {
stderr: expect.it(
'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'
).and('to contain', '1 failed, 1 total')
});
});

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 satisfy', {
stdout: expect.it(
'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'
)
}).and('when run through jest to satisfy', {
stderr: expect.it(
'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'
).and('to contain', '1 failed, 1 total')
});
});

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 satisfy', {
stdout: expect.it(
'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'
)
}).and('when run through jest to satisfy', {
stderr: expect.it(
'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'
).and('to contain', '1 failed, 1 total')
});
});

it('should error with more than one req', function () {
return expect(() => {
/* eslint-disable */
it('should foo', function () {
httpception([
{ request: 'GET /foo', response: 200 },
{ request: 'GET /bar', response: 200 },
{ request: 'GET /baz', response: 200 }
]);

return expect('/foo', 'to yield response', 200).then(
() => expect('/foo', 'to yield response', 200)
);
});
/* eslint-enable */
}, 'when run through mocha to satisfy', {
stdout: expect.it(
'to contain',
'GET /foo HTTP/1.1 // should be GET /bar\n' +
' //\n' +
' // -GET /foo HTTP/1.1\n' +
' // +GET /bar HTTP/1.1'
)
}).and('when run through jest to satisfy', {
stderr: expect.it(
'to contain',
' GET /foo HTTP/1.1 // should be GET /bar\n' +
' //\n' +
' // -GET /foo HTTP/1.1\n' +
' // +GET /bar HTTP/1.1'
).and('to contain', '1 failed, 1 total')
});
});
});
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
"main": "lib/httpception.js",
"scripts": {
"lint": "eslint .",
"test": "mocha",
"test": "npm run test-mocha && npm run test-jest && npm run test-integration",
"test-integration": "mocha integration-test/test.js --timeout 10000",
"test-mocha": "mocha --opts test/mocha/mocha.opts",
"test-jest": "jest --config test/jest/jest.config.js",
"travis": "npm test && npm run lint && npm run coverage && (<coverage/lcov.info coveralls || true)",
"coverage": "NODE_ENV=development nyc --reporter=lcov --reporter=text --all -- mocha && echo google-chrome coverage/lcov-report/index.html"
},
Expand All @@ -19,6 +22,7 @@
"coveralls": "^2.12.0",
"eslint": "^3.18.0",
"eslint-config-onelint": "^2.0.0",
"jest": "^20.0.4",
"mocha": "^3.2.0",
"nyc": "^10.1.2",
"unexpected-http": "^5.6.0"
Expand Down
118 changes: 0 additions & 118 deletions test/afterEach.js

This file was deleted.

File renamed without changes.
6 changes: 6 additions & 0 deletions test/jest/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
rootDir: require('path').resolve(__dirname, '../..'),
testEnvironment: 'node',
testPathIgnorePatterns: ['/node_modules/', '/integration-test/'],
collectCoverageFrom: ['**/*.js', '!(test|coverage|node_modules)/**']
};
1 change: 1 addition & 0 deletions test/mocha/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test/**/*.test.js

0 comments on commit 39c3641

Please sign in to comment.