Skip to content

Commit

Permalink
Added duration override for stall for testing. Added more unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
akash5474 committed Mar 18, 2018
1 parent 8cbb15c commit 8e8ffc9
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
5 changes: 3 additions & 2 deletions lib/create-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ async function createServer(assets, indexHtml, files = [], options = {}) {
const {
externals = null,
proxy = null,
stall = null,
publicPath = '/',
} = options;

Expand Down Expand Up @@ -53,10 +52,12 @@ async function createServer(assets, indexHtml, files = [], options = {}) {
} minutes.`
);

// Duration option to avoid timoeuts while unit testing
const duration = options.test && options.test.duration || 1000;
const stall = Math.min(options.stall, 5);

await new Promise(resolve =>
setTimeout(resolve, 1000 * 60 * stall)
setTimeout(resolve, duration * 60 * stall)
);
}

Expand Down
66 changes: 64 additions & 2 deletions test/lib/create-server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const chai = require('chai');
const chaiHttp = require('chai-http');
const loaderUtils = require('loader-utils');
const { RawSource } = require('webpack-sources');
const proxyquire = require('proxyquire');
const sinon = require('sinon');

const createServer = require('../../lib/create-server');

Expand Down Expand Up @@ -42,7 +44,7 @@ describe('lib/create-server.js', () => {
assets[testCSS] = new RawSource(testCSSFile);
});

afterEach(() => server.close());
afterEach(() => { if (server) server.close(); });

it('serves index.html file', async () => {
const result = await createServer(assets, indexHtml, []);
Expand Down Expand Up @@ -155,6 +157,66 @@ describe('lib/create-server.js', () => {
expect(htmlRes.text).to.be.equal(indexHtmlFile);
});

xit('applies proxy option', async () => {});
it('applies proxy option', async () => {
const proxyMiddlewareStub = sinon.stub().returns((req, res, next) => { next() });
const createServer = proxyquire('../../lib/create-server', {
'http-proxy-middleware': proxyMiddlewareStub
});

const proxy = {
'/v1/api': {
target: 'https://test-site.com'
},

'/v2/api': {
target: 'https://test-site-2.com'
}
};

const result = await createServer(assets, indexHtml, [], { proxy });
server = result.server;

expect(proxyMiddlewareStub.callCount).to.equal(2);

Object.entries(proxy).forEach(([urlPath, opts], idx) => {
const call = proxyMiddlewareStub.getCall(idx);

expect(call.calledWithExactly(opts)).to.equal(true);
});

});

it('catches and logs errors', async () => {
const expressStub = sinon.stub().throws();
const logErrorStub = sinon.stub();

const createServer = proxyquire('../../lib/create-server', {
express: expressStub,
'./util/logging': { logError: logErrorStub }
});

try {
const result = await createServer();
} catch (err) {
expect(err).to.be.an('error');
}

expect(expressStub.threw()).to.equal(true);
expect(expressStub.exceptions.length).to.equal(1);
expect(logErrorStub.callCount).to.equal(2);
});

it('honors stall option and prints log message', async () => {
const logInfoStub = sinon.stub();

const createServer = proxyquire('../../lib/create-server', {
'./util/logging': { logInfo: logInfoStub }
});

const result = await createServer(assets, indexHtml, [], { stall: 5, test: { duration: 1 } });
server = result.server;

expect(logInfoStub.callCount).to.equal(1);
});
});
});

0 comments on commit 8e8ffc9

Please sign in to comment.