Skip to content

Commit

Permalink
build(all): detect open handles
Browse files Browse the repository at this point in the history
  • Loading branch information
JozefFlakus committed Dec 28, 2018
1 parent 5dfaf42 commit 02e4709
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -11,7 +11,7 @@
"lint": "lerna run lint",
"pretest": "yarn lint",
"test": "yarn test:unit && yarn test:integration",
"test:unit": "SCOPE=unit jest --expand --coverage",
"test:unit": "SCOPE=unit jest --expand --coverage --detectOpenHandles",
"test:integration": "SCOPE=integration jest --expand",
"test:watch": "jest --expand --onlyChanged --watch",
"clean": "lerna run clean",
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/+internal/testing/http.helper.ts
Expand Up @@ -17,6 +17,7 @@ interface HttpResponseMockParams {

export interface HttpServerMocks {
listen?: jest.Mock;
close?: jest.Mock;
on?: jest.Mock;
}

Expand All @@ -37,5 +38,6 @@ export const createHttpResponse = (data: HttpResponseMockParams = {}) => ({
export const mockHttpServer = (mocks: HttpServerMocks = {}) =>
jest.spyOn(http, 'createServer').mockImplementation(jest.fn(() => ({
listen: mocks.listen || jest.fn(),
close: mocks.close || jest.fn(callback => callback()),
on: mocks.on || jest.fn(),
})));
28 changes: 20 additions & 8 deletions packages/core/src/server/specs/server.factory.spec.ts
Expand Up @@ -6,7 +6,19 @@ import { EffectFactory } from '../../effects/effects.factory';
import { mockHttpServer } from '../../+internal/testing';

describe('#marble', () => {
beforeEach(() => jest.restoreAllMocks());
let marbleServer: ReturnType<typeof marble>;

beforeEach(() => {
jest.restoreAllMocks();
});

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

test('creates http server and starts listening to given port', () => {
// given
Expand All @@ -17,7 +29,7 @@ describe('#marble', () => {

// when
mockHttpServer(mocks);
marble({
marbleServer = marble({
port,
hostname,
httpListener: app,
Expand All @@ -35,7 +47,7 @@ describe('#marble', () => {

// when
mockHttpServer(mocks);
marble({ httpListener: app });
marbleServer = marble({ httpListener: app });

// then
expect(mocks.listen.mock.calls[0][0]).toBe(undefined);
Expand All @@ -51,7 +63,7 @@ describe('#marble', () => {
const app = httpListener({ effects: [effect$] });

// when
const marbleServer = marble({ httpListener: app });
marbleServer = marble({ httpListener: app });

// then
expect(marbleServer.server).toBeDefined();
Expand All @@ -71,10 +83,10 @@ describe('#marble', () => {
.mockImplementation(jest.fn(() => jest.fn()));

// then
marble({ httpListener: app });
marbleServer = marble({ httpListener: app });
expect(injector.registerAll).not.toHaveBeenCalled();

marble({ httpListener: app, dependencies: [] });
marbleServer = marble({ httpListener: app, dependencies: [] });
expect(injector.registerAll).toHaveBeenCalledWith([]);
});

Expand All @@ -84,7 +96,7 @@ describe('#marble', () => {
const expectedEvent = EventType.LISTEN;

// then
marble({
marbleServer = marble({
httpListener: app,
httpEventsHandler: event$ => event$.pipe(
filter(event => event.type === expectedEvent),
Expand All @@ -99,7 +111,7 @@ describe('#marble', () => {
const expectedEvent = EventType.UPGRADE;

// then
const marbleServer = marble({
marbleServer = marble({
httpListener: app,
httpEventsHandler: event$ => event$.pipe(
filter(event => event.type === expectedEvent),
Expand Down

0 comments on commit 02e4709

Please sign in to comment.