Skip to content
This repository has been archived by the owner on Jan 18, 2023. It is now read-only.

Commit

Permalink
reach 100% code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
nahtnam committed Aug 25, 2019
1 parent 29c2d44 commit 0e049b7
Show file tree
Hide file tree
Showing 14 changed files with 355 additions and 112 deletions.
1 change: 1 addition & 0 deletions src/utils/glob.ts
Expand Up @@ -9,6 +9,7 @@ const ls: (dir: string, filelist: string[]) => any = (dir, filelist = []): any[]
? ls(path.join(dir, file), filelist)
: filelist.concat(path.join(dir, file))[0]));

/* istanbul ignore next */
const flatten = (arr: any[]): any[] => arr.reduce((acc: any, val: any): any[] => (Array.isArray(val) ? acc.concat(flatten(val)) : acc.concat(val)), []);

export default (url: string, glob: string): string[] => {
Expand Down
86 changes: 44 additions & 42 deletions tests/plugins/error-handler.ts
Expand Up @@ -20,57 +20,59 @@ afterEach(async () => {
server.close();
});

describe('error handler', () => {
describe('with standard error', () => {
beforeAll(() => {
error = () => { throw new Error('message'); };
});
describe('plugins', () => {
describe('error handler', () => {
describe('with standard error', () => {
beforeAll(() => {
error = () => { throw new Error('message'); };
});

it('returns a 500 error', async () => {
expect.assertions(2);
const req = await fetch(server.url);
const res = await req.json();
expect(req.status).toStrictEqual(500);
expect(res).toMatchObject({
error: 'Internal Server Error',
message: 'An internal server error occurred',
statusCode: 500,
it('returns a 500 error', async () => {
expect.assertions(2);
const req = await fetch(server.url);
const res = await req.json();
expect(req.status).toStrictEqual(500);
expect(res).toMatchObject({
error: 'Internal Server Error',
message: 'An internal server error occurred',
statusCode: 500,
});
});
});
});

describe('with a custom error created with createError', () => {
beforeAll(() => {
error = () => { throw createError(400, 'custom error message'); };
});
describe('with a custom error created with createError', () => {
beforeAll(() => {
error = () => { throw createError(400, 'custom error message'); };
});

it('returns a 500 error', async () => {
expect.assertions(2);
const req = await fetch(server.url);
const res = await req.json();
expect(req.status).toStrictEqual(400);
expect(res).toMatchObject({
error: 'Bad Request',
message: 'custom error message',
statusCode: 400,
it('returns a 500 error', async () => {
expect.assertions(2);
const req = await fetch(server.url);
const res = await req.json();
expect(req.status).toStrictEqual(400);
expect(res).toMatchObject({
error: 'Bad Request',
message: 'custom error message',
statusCode: 400,
});
});
});
});

describe('with error handler disabled', () => {
beforeAll(() => {
disableErrorHandler = true;
error = () => { throw new Error('message'); };
});
describe('with error handler disabled', () => {
beforeAll(() => {
disableErrorHandler = true;
error = () => { throw new Error('message'); };
});

it('returns an internal server error in text', async () => {
expect.assertions(2);
const spy = jest.spyOn(console, 'error').mockImplementation();
const req = await fetch(server.url);
const res = await req.text();
expect(req.status).toStrictEqual(500);
expect(res).toBe('Internal Server Error');
spy.mockRestore();
it('returns an internal server error in text', async () => {
expect.assertions(2);
const spy = jest.spyOn(console, 'error').mockImplementation();
const req = await fetch(server.url);
const res = await req.text();
expect(req.status).toStrictEqual(500);
expect(res).toBe('Internal Server Error');
spy.mockRestore();
});
});
});
});
32 changes: 17 additions & 15 deletions tests/plugins/request-logger.ts
Expand Up @@ -19,21 +19,23 @@ afterEach(async () => {
server.close();
});

describe('request logger', () => {
describe('with logger enabled', () => {
it('logs', async () => {
expect.assertions(6);
const spy = jest.spyOn(process.stdout, 'write').mockImplementation();
const req = await fetch(server.url);
const res = await req.json();
expect(req.status).toStrictEqual(200);
expect(res).toMatchObject({ hello: 'world' });
expect(spy).toHaveBeenCalledTimes(1);
const log = JSON.parse(spy.mock.calls[0][0]);
expect(log.req).toHaveProperty('method', 'GET');
expect(log.req).toHaveProperty('url', '/');
expect(log.res).toHaveProperty('statusCode', 200);
spy.mockRestore();
describe('plugins', () => {
describe('request logger', () => {
describe('with logger enabled', () => {
it('logs', async () => {
expect.assertions(6);
const spy = jest.spyOn(process.stdout, 'write').mockImplementation();
const req = await fetch(server.url);
const res = await req.json();
expect(req.status).toStrictEqual(200);
expect(res).toMatchObject({ hello: 'world' });
expect(spy).toHaveBeenCalledTimes(1);
const log = JSON.parse(spy.mock.calls[0][0]);
expect(log.req).toHaveProperty('method', 'GET');
expect(log.req).toHaveProperty('url', '/');
expect(log.res).toHaveProperty('statusCode', 200);
spy.mockRestore();
});
});
});
});
13 changes: 13 additions & 0 deletions tests/seeds/server/routes/index.ts
@@ -0,0 +1,13 @@
import { light, Route } from '../../../../src/index';

class Index extends Route {
public disableRequestLogger = true;

public async handler() {
return {
hello: 'server',
};
}
}

module.exports = light(Index);
7 changes: 7 additions & 0 deletions tests/seeds/utils/add-routes/routes/default.ts
@@ -0,0 +1,7 @@
import { light, Route } from '../../../../../src/index';

class Index extends Route {}

module.exports = {
default: light(Index),
};
5 changes: 5 additions & 0 deletions tests/seeds/utils/add-routes/routes/index.ts
@@ -0,0 +1,5 @@
import { light, Route } from '../../../../../src/index';

class Index extends Route {}

export default light(Index);
1 change: 1 addition & 0 deletions tests/seeds/utils/add-routes/routes/throw.ts
@@ -0,0 +1 @@
throw new Error('error');
68 changes: 68 additions & 0 deletions tests/server.ts
@@ -0,0 +1,68 @@
import fetch from 'node-fetch';
import listen from 'test-listen';
import { join } from 'path';

import {
server,
light,
Route,
} from '../src/index';

let app: any;
let url: any;

beforeEach(async () => {
url = await listen(app.server);
});

afterEach(async () => {
app.server.close();
});

describe('server', () => {
describe('with functions as routes', () => {
beforeAll(() => {
app = server({
routes: [
{
handler: light(class Index extends Route {
public disableRequestLogger = true;

public async handler() {
return {
hello: 'server',
};
}
}),
method: 'GET',
path: '/',
},
],
});
});

it('returns an object', async () => {
expect.assertions(2);
const req = await fetch(url);
const res = await req.json();
expect(req.status).toStrictEqual(200);
expect(res).toMatchObject({ hello: 'server' });
});
});

describe('with string as routes', () => {
beforeAll(() => {
app = server({
routes: join(__dirname, './seeds/server/routes'),
});
});

it('returns an object', async () => {
expect.assertions(2);
const req = await fetch(url);
const res = await req.json();
expect(req.status).toStrictEqual(200);
expect(res).toMatchObject({ hello: 'server' });
});
});
});
30 changes: 16 additions & 14 deletions tests/serverless/aws.ts
@@ -1,20 +1,22 @@
import { light, Route } from '../../src/index';

describe('aws', () => {
process.env.LIGHT_ENVIRONMENT = 'aws';
describe('serverless', () => {
describe('aws', () => {
process.env.LIGHT_ENVIRONMENT = 'aws';

const server: any = light(class Index extends Route {
public async handler() {
return {
hello: 'world',
};
}
});
const server: any = light(class Index extends Route {
public async handler() {
return {
hello: 'world',
};
}
});

it('exports a handler', async () => {
expect.assertions(3);
expect(server).toBeTruthy();
expect(server.handler).toBeTruthy();
expect(typeof server.handler).toBe('function');
it('exports a handler', async () => {
expect.assertions(3);
expect(server).toBeTruthy();
expect(server.handler).toBeTruthy();
expect(typeof server.handler).toBe('function');
});
});
});
30 changes: 16 additions & 14 deletions tests/serverless/netlify.ts
@@ -1,20 +1,22 @@
import { light, Route } from '../../src/index';

describe('netlify', () => {
process.env.LIGHT_ENVIRONMENT = 'netlify';
describe('serverless', () => {
describe('netlify', () => {
process.env.LIGHT_ENVIRONMENT = 'netlify';

const server: any = light(class Index extends Route {
public async handler() {
return {
hello: 'world',
};
}
});
const server: any = light(class Index extends Route {
public async handler() {
return {
hello: 'world',
};
}
});

it('exports a handler', async () => {
expect.assertions(3);
expect(server).toBeTruthy();
expect(server.handler).toBeTruthy();
expect(typeof server.handler).toBe('function');
it('exports a handler', async () => {
expect.assertions(3);
expect(server).toBeTruthy();
expect(server.handler).toBeTruthy();
expect(typeof server.handler).toBe('function');
});
});
});
28 changes: 15 additions & 13 deletions tests/serverless/now.ts
@@ -1,19 +1,21 @@
import { light, Route } from '../../src/index';

describe('now', () => {
process.env.LIGHT_ENVIRONMENT = 'now';
describe('serverless', () => {
describe('now', () => {
process.env.LIGHT_ENVIRONMENT = 'now';

const server: any = light(class Index extends Route {
public async handler() {
return {
hello: 'world',
};
}
});
const server: any = light(class Index extends Route {
public async handler() {
return {
hello: 'world',
};
}
});

it('exports a function', async () => {
expect.assertions(2);
expect(server).toBeTruthy();
expect(typeof server).toBe('function');
it('exports a function', async () => {
expect.assertions(2);
expect(server).toBeTruthy();
expect(typeof server).toBe('function');
});
});
});
30 changes: 16 additions & 14 deletions tests/serverless/runkit.ts
@@ -1,20 +1,22 @@
import { light, Route } from '../../src/index';

describe('runkit', () => {
process.env.LIGHT_ENVIRONMENT = 'runkit';
describe('serverless', () => {
describe('runkit', () => {
process.env.LIGHT_ENVIRONMENT = 'runkit';

const server: any = light(class Index extends Route {
public async handler() {
return {
hello: 'world',
};
}
});
const server: any = light(class Index extends Route {
public async handler() {
return {
hello: 'world',
};
}
});

it('exports a function', async () => {
expect.assertions(3);
expect(server).toBeTruthy();
expect(server.endpoint).toBeTruthy();
expect(typeof server.endpoint).toBe('function');
it('exports a function', async () => {
expect.assertions(3);
expect(server).toBeTruthy();
expect(server.endpoint).toBeTruthy();
expect(typeof server.endpoint).toBe('function');
});
});
});

0 comments on commit 0e049b7

Please sign in to comment.