Skip to content

Commit

Permalink
Replace mocha with Jest (#63)
Browse files Browse the repository at this point in the history
* Replace require-dir to get jest test working

require-dir is not compatible with jest, but require-directory seems to work

aseemk/requireDir#53 (comment)
  • Loading branch information
stefanbuck authored and josephfrazier committed Apr 3, 2018
1 parent 8c4db67 commit 10e4242
Show file tree
Hide file tree
Showing 11 changed files with 2,639 additions and 587 deletions.
21 changes: 10 additions & 11 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"extends": "airbnb-base",
"plugins": ["mocha"],
"plugins": ["jest"],
"env": {
"node": true,
"mocha": true
"jest": true
},
"settings": {
"mocha/additionalSuiteNames": ["parallel"]
Expand All @@ -25,14 +25,13 @@
"no-restricted-syntax": 0,
"consistent-return": 0,
"array-callback-return": 0,
"mocha/no-exclusive-tests": 2,
"mocha/no-skipped-tests": 2,
"mocha/no-pending-tests": 2,
"mocha/handle-done-callback": 2,
"mocha/no-global-tests": 2,
"mocha/no-return-and-callback": 2,
"mocha/no-sibling-hooks": 2,
"mocha/no-identical-title": 2,
"mocha/no-top-level-hooks": 2
"jest/no-disabled-tests": 2,
"jest/no-focused-tests": 2,
"jest/no-identical-title": 2,
"jest/consistent-test-it": ["error", {"fn": "it"}],
"jest/prefer-to-be-undefined": 2,
"jest/prefer-to-be-null": 2,
"jest/prefer-to-have-length": 2,
"jest/valid-expect": 2
}
}
15 changes: 6 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"start": "node server.js",
"lint": "eslint .",
"pretest": "yarn lint",
"test": "mocha",
"test": "jest",
"watch-test": "mocha -w"
},
"author": "Stefan Buck",
Expand All @@ -31,23 +31,20 @@
"memory-cache": "^0.2.0",
"newrelic": "^2.2.2",
"promise-limit": "^2.6.0",
"require-dir": "^0.3.2"
"require-directory": "^2.1.1"
},
"repository": {
"type": "git",
"url": "https://github.com/octo-linker/live-resolver"
},
"devDependencies": {
"eslint": "^3.17.1",
"eslint": "^4.19.1",
"eslint-config-airbnb-base": "^11.1.1",
"eslint-plugin-import": "2.2.0",
"eslint-plugin-mocha": "^4.9.0",
"eslint-plugin-jest": "^21.15.0",
"fs-extra": "^4.0.2",
"jest": "^22.4.3",
"jsdom": "^11.4.0",
"lodash": "4.17.4",
"mocha": "3.2.0",
"mocha.parallel": "^0.15.0",
"sinon": "1.17.7",
"sinon-as-promised": "4.0.2"
"lodash": "4.17.4"
}
}
12 changes: 6 additions & 6 deletions src/plugins/bulk.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ const register = (server) => {
}));
}
}))
.then(values => values.map((item) => {
if (item && item.result && item.result.url) {
return item.result.url;
}
.then(values => values.map((item) => {
if (item && item.result && item.result.url) {
return item.result.url;
}

return null;
}));
return null;
}));
},
},
}]);
Expand Down
5 changes: 3 additions & 2 deletions src/plugins/java-resolver.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const Joi = require('joi');
const path = require('path');
const Boom = require('boom');
const requireDir = require('require-dir');
const requireDir = require('require-directory');
const insight = require('../utils/insight.js');

const mappingFiles = requireDir('../../mapping-files');
const mappingFiles = requireDir(module, path.join(__dirname, '../../mapping-files'));
const flatMappingList = Object.assign(...Object.values(mappingFiles));

const register = (server) => {
Expand Down
3 changes: 1 addition & 2 deletions test/config.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const assert = require('assert');
const config = require('../config.json');

describe('config.json', () => {
Expand All @@ -9,7 +8,7 @@ describe('config.json', () => {
for (const propKey of Object.keys(props)) {
const prop = props[propKey];
it(`has "${prop}" property`, () => {
assert(config[key][prop], `No property ${prop} found for ${key} `);
expect(config[key][prop]).toBeDefined();
});
}
});
Expand Down
26 changes: 12 additions & 14 deletions test/functional.js → test/functional.spec.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
const assert = require('assert');
const got = require('got');
const parallel = require('mocha.parallel');

parallel('functional', () => {
describe('functional', () => {
let server;

before((done) => {
beforeAll(() => (new Promise((resolve) => {
server = require('../server'); // eslint-disable-line global-require
server.events.once('start', done);
});
server.events.once('start', resolve);
})));

after(() => server.stop());
afterAll(() => server.stop());

function testUrl(path, expectedUrl) {
it(`resolves ${path} to ${expectedUrl}`, async () => {
const response = await got(server.info.uri + path);
const url = JSON.parse(response.body).url;
assert.deepStrictEqual(url, expectedUrl);
expect(url).toBe(expectedUrl);
});
}

Expand Down Expand Up @@ -49,11 +47,11 @@ parallel('functional', () => {


const body = JSON.parse(response.body);
assert.equal(body[0], 'https://github.com/sebastianbergmann/phpunit');
assert.equal(body[1], null);
assert.equal(body[2], 'https://nodejs.org/api/path.html');
assert.equal(body[3], 'https://github.com/request/request');
assert.equal(body[4], null);
assert.equal(body[5], 'https://github.com/jquery/jquery-dist');
expect(body[0]).toBe('https://github.com/sebastianbergmann/phpunit');
expect(body[1]).toBeNull();
expect(body[2]).toBe('https://nodejs.org/api/path.html');
expect(body[3]).toBe('https://github.com/request/request');
expect(body[4]).toBeNull();
expect(body[5]).toBe('https://github.com/jquery/jquery-dist');
});
});
34 changes: 11 additions & 23 deletions test/ping.spec.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,56 @@


const assert = require('assert');
const sinon = require('sinon');
require('sinon-as-promised');
const got = require('got');
const hapi = require('hapi');
const plugin = require('../src/plugins/ping.js');

jest.mock('got');

describe('ping', () => {
let server;

before(async () => {
beforeAll(async () => {
server = new hapi.Server();

await server.register(plugin);
return server.start();
});

after(() => server.stop());

beforeEach(() => {
this.sandbox = sinon.sandbox.create();
this.gotStub = this.sandbox.stub(got, 'get');
});

afterEach(() => {
this.sandbox.restore();
});
afterAll(() => server.stop());

it('performs an HTTP HEAD request', async () => {
this.gotStub.resolves();
got.get.mockResolvedValue();

const options = {
url: '/ping?url=http://awesomefooland.com',
};

await server.inject(options);
assert.equal(this.gotStub.callCount, 1);
assert.equal(this.gotStub.args[0][0], 'http://awesomefooland.com');
expect(got.get.mock.calls).toHaveLength(1);
expect(got.get.mock.calls[0][0]).toBe('http://awesomefooland.com');
});

it('returns 404 response if package can not be found', async () => {
const err = new Error('Some error');
err.code = 404;
this.gotStub.rejects(err);
got.get.mockRejectedValue(err);

const options = {
method: 'GET',
url: '/ping?url=http://awesomefooland.com',
};

const response = await server.inject(options);
assert.equal(response.statusCode, 404);
expect(response.statusCode).toBe(404);
});

it('returns project url', async () => {
this.gotStub.resolves();
got.get.mockResolvedValue();

const options = {
method: 'GET',
url: '/ping?url=http://awesomefooland.com',
};

const response = await server.inject(options);
assert.equal(response.statusCode, 200);
expect(response.statusCode).toBe(200);
});
});
5 changes: 2 additions & 3 deletions test/repository-url.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const assert = require('assert');
const findRepositoryUrl = require('../src/utils/repository-url');

describe('repository url', () => {
Expand All @@ -23,12 +22,12 @@ describe('repository url', () => {
}

it(`resolves ${type}`, () => {
assert.equal(findRepositoryUrl(node), 'https://github.com/john/doe');
expect(findRepositoryUrl(node)).toBe('https://github.com/john/doe');
});
});

const detailUrl = 'https://github.com/john/doe/tree/master/foo';
it(`resolves ${detailUrl}`, () => {
assert.equal(findRepositoryUrl(detailUrl), 'https://github.com/john/doe/tree/master/foo');
expect(findRepositoryUrl(detailUrl)).toBe('https://github.com/john/doe/tree/master/foo');
});
});
51 changes: 20 additions & 31 deletions test/universal-resolver.spec.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,43 @@
const assert = require('assert');
const sinon = require('sinon');
require('sinon-as-promised');
const got = require('got');
const cache = require('memory-cache');
const hapi = require('hapi');
const plugin = require('../src/plugins/universal-resolver.js');

jest.mock('got');

describe('resolver', () => {
let server;
const options = {
method: 'GET',
url: '/q/bower/foo',
};

before(async () => {
beforeAll(async () => {
server = new hapi.Server();

await server.register(plugin);
return server.start();
});

after(() => server.stop());

beforeEach(() => {
this.sandbox = sinon.sandbox.create();
this.gotStub = this.sandbox.stub(got, 'get');
afterAll(() => server.stop());

this.sandbox.stub(cache, 'get');
});

afterEach(() => {
this.sandbox.restore();
});
afterEach(() => got.get.mockClear());

describe('fetch', () => {
it('returns an error if registry fetch fails', async () => {
const err = new Error('Some error');
this.gotStub.rejects(err);
got.get.mockRejectedValue(err);

const response = await server.inject(options);
assert.equal(response.statusCode, 500);
assert.equal(response.result.error, 'Internal Server Error');
expect(response.statusCode).toBe(500);
expect(response.result.error).toBe('Internal Server Error');
});

it('fetch package information from registry', async () => {
this.gotStub.resolves();
got.get.mockResolvedValue();

await server.inject(options);
assert.equal(this.gotStub.callCount, 1);
assert.equal(this.gotStub.args[0][0], 'https://registry.bower.io/packages/foo');
expect(got.get.mock.calls).toHaveLength(1);
expect(got.get.mock.calls[0][0]).toBe('https://registry.bower.io/packages/foo');
});

it('escapes slashes in package names', async () => {
Expand All @@ -57,11 +46,11 @@ describe('resolver', () => {
url: '/q/npm/@angular/core',
};

this.gotStub.resolves();
got.get.mockResolvedValue();

await server.inject(optionsScopePackage);
assert.equal(this.gotStub.callCount, 1);
assert.equal(this.gotStub.args[0][0], 'https://registry.npmjs.org/@angular%2fcore');
expect(got.get.mock.calls).toHaveLength(1);
expect(got.get.mock.calls[0][0]).toBe('https://registry.npmjs.org/@angular%2fcore');
});
});

Expand All @@ -70,25 +59,25 @@ describe('resolver', () => {
it('when package can not be found', async () => {
const err = new Error('Some error');
err.statusCode = 404;
this.gotStub.rejects(err);
got.get.mockRejectedValue(err);

const response = await server.inject(options);
assert.equal(response.statusCode, 404);
assert.equal(response.result.message, 'Package not found');
expect(response.statusCode).toBe(404);
expect(response.result.message).toBe('Package not found');
});
});

describe('with 200', () => {
it('when no repository url is found', async () => {
this.gotStub.resolves({
got.get.mockResolvedValue({
body: JSON.stringify({
url: '',
}),
});

const response = await server.inject(options);
assert.equal(response.statusCode, 200);
assert.equal(response.result.url, 'https://bower.io/search/?q=foo');
expect(response.statusCode).toBe(200);
expect(response.result.url).toBe('https://bower.io/search/?q=foo');
});
});
});
Expand Down
Loading

0 comments on commit 10e4242

Please sign in to comment.