@copperjs/copper
Version:
A lightweight chromium grid
59 lines • 2.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const sinon = require("sinon");
const proxyquire = require("proxyquire");
const chai_1 = require("chai");
const config_1 = require("./config");
const utils_1 = require("../common/utils");
const fetchStub = sinon.stub();
const { NodeServer } = proxyquire.noCallThru()('./server', { 'node-fetch': { default: fetchStub } });
describe('nodeServer', () => {
let nodeServer;
beforeEach(() => {
config_1.nodeConfig.value = {
registerInterval: 1,
registerRetries: 2,
deregisterInterval: 1,
deregisterRetries: 2,
};
nodeServer = new NodeServer({ port: 0 });
});
afterEach(() => {
fetchStub.reset();
config_1.nodeConfig.reset();
});
it('should be an instance of NodeServer', () => {
chai_1.expect(nodeServer).to.be.an.instanceof(NodeServer);
});
it('should register to hub', async () => {
fetchStub.resolves({});
await nodeServer.register();
chai_1.expect(fetchStub).to.have.been.calledWith(`http://${config_1.nodeConfig.value.hubHost}:${config_1.nodeConfig.value.hubPort}/grid/node`, {
method: 'POST',
body: JSON.stringify({ config: config_1.nodeConfig.value }),
headers: { 'Content-Type': 'application/json' },
});
});
it('should retry when failed registering to hub', async () => {
fetchStub.rejects(new Error('error'));
await nodeServer.register(2);
await utils_1.delay(2);
chai_1.expect(fetchStub.callCount).to.be.greaterThanOrEqual(2);
});
it('should deregister from hub', async () => {
fetchStub.resolves({});
await nodeServer.deregister();
chai_1.expect(fetchStub).to.have.been.calledWith(`http://${config_1.nodeConfig.value.hubHost}:${config_1.nodeConfig.value.hubPort}/grid/node`, {
method: 'DELETE',
body: JSON.stringify({ config: config_1.nodeConfig.value }),
headers: { 'Content-Type': 'application/json' },
});
});
it('should retry when failed deregistering from hub', async () => {
fetchStub.rejects(new Error('error'));
await nodeServer.deregister(2);
await utils_1.delay(2);
chai_1.expect(fetchStub.callCount).to.be.greaterThanOrEqual(2);
});
});
//# sourceMappingURL=server.test.js.map