Skip to content

Commit

Permalink
test(whook): Improve test coveraging
Browse files Browse the repository at this point in the history
  • Loading branch information
nfroidure committed Feb 7, 2019
1 parent 37ea509 commit 624d756
Show file tree
Hide file tree
Showing 17 changed files with 808 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/whook-cors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,4 @@
"preversion": " && npm run compile",
"test": "npm run jest"
}
}
}
5 changes: 3 additions & 2 deletions packages/whook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@
},
"jest": {
"coverageReporters": [
"lcov"
"lcov",
"html"
],
"testPathIgnorePatterns": [
"/node_modules/"
Expand Down Expand Up @@ -152,4 +153,4 @@
"README.md",
"CHANGELOG.md"
]
}
}
35 changes: 35 additions & 0 deletions packages/whook/src/libs/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { noop, identity, pipe, compose } from './utils';

describe('noop', () => {
it('should work', () => {
noop();
});
});

describe('identity', () => {
it('should work', () => {
expect(identity('a')).toEqual('a');
});
});

describe('pipe', () => {
it('should work', () => {
expect(
pipe(
identity,
identity,
)('a'),
).toEqual('a');
});
});

describe('noop', () => {
it('should work', () => {
expect(
compose(
identity,
identity,
)('a'),
).toEqual('a');
});
});
34 changes: 23 additions & 11 deletions packages/whook/src/services/ENV.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ export default initializer(
* @return {Promise<Object>}
* A promise of an object containing the actual env vars.
*/
async function initENV({ NODE_ENV, PWD, BASE_ENV = {}, log = noop }) {
async function initENV({
NODE_ENV,
PWD,
BASE_ENV = {},
PROCESS_ENV = process.env,
log = noop,
readFile = _readFile,
}) {
let ENV = { ...BASE_ENV };

log('info', `Loading the environment service.`);
Expand All @@ -48,7 +55,7 @@ async function initENV({ NODE_ENV, PWD, BASE_ENV = {}, log = noop }) {
the process env when building.
*/
if (!process.env.ISOLATED_ENV) {
ENV = { ...ENV, ...process.env };
ENV = { ...ENV, ...PROCESS_ENV };
log('info', `Using local env.`);
}

Expand All @@ -59,16 +66,9 @@ async function initENV({ NODE_ENV, PWD, BASE_ENV = {}, log = noop }) {
*/
try {
const envPath = path.join(PWD, `.env.${NODE_ENV}`);
const buf = await new Promise((resolve, reject) => {
fs.readFile(envPath, (err, data) => {
if (err) {
reject(err);
return;
}
resolve(data);
});
});
const buf = await readFile(envPath);
const FILE_ENV = dotenv.parse(buf);

log('info', `Using .env file at ${envPath}.`);

ENV = { ...ENV, ...FILE_ENV };
Expand All @@ -83,3 +83,15 @@ async function initENV({ NODE_ENV, PWD, BASE_ENV = {}, log = noop }) {
NODE_ENV,
};
}

async function _readFile(path) {
return new Promise((resolve, reject) => {
fs.readFile(path, (err, data) => {
if (err) {
reject(err);
return;
}
resolve(data);
});
});
}
57 changes: 57 additions & 0 deletions packages/whook/src/services/ENV.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import initENV from './ENV';

describe('initENV', () => {
const log = jest.fn();
const readFile = jest.fn();

beforeEach(() => {
log.mockReset();
readFile.mockReset();
});

it('should work with existing file', async () => {
readFile.mockResolvedValueOnce(
Buffer.from(
`DB_PASSWORD=oudelali
DB_HOST = 'localhost'
`,
),
);

const ENV = await initENV({
ENV: { ENV: 1337 },
NODE_ENV: 'development',
BASE_ENV: { KEY_BASE_ENV: 'test' },
PROCESS_ENV: { KEY_PROCESS_ENV: 'test' },
PWD: '/home/whoami/my-whook-project',
log,
readFile,
});

expect({
ENV,
logCalls: log.mock.calls,
readFileCalls: readFile.mock.calls,
}).toMatchSnapshot();
});

it('should fail with non-existing file', async () => {
readFile.mockRejectedValueOnce(new Error('EEXISTS'));

const ENV = await initENV({
ENV: { ENV: 1337 },
NODE_ENV: 'development',
BASE_ENV: { KEY_BASE_ENV: 'test' },
PROCESS_ENV: { KEY_PROCESS_ENV: 'test' },
PWD: '/home/whoami/my-whook-project',
log,
readFile,
});

expect({
ENV,
logCalls: log.mock.calls.filter(args => 'debug' !== args[0]),
readFileCalls: readFile.mock.calls,
}).toMatchSnapshot();
});
});
4 changes: 2 additions & 2 deletions packages/whook/src/services/HOST.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import internalIp from 'internal-ip';
import _internalIp from 'internal-ip';
import { initializer } from 'knifecycle';
import { noop } from '../libs/utils';

Expand Down Expand Up @@ -29,7 +29,7 @@ export default initializer(
* @return {Promise<String>}
* A promise of a containing the actual host.
*/
async function initHOST({ ENV = {}, log = noop }) {
async function initHOST({ ENV = {}, log = noop, internalIp = _internalIp }) {
if ('undefined' !== typeof ENV.HOST) {
log('info', `Using ENV host ${ENV.HOST}`);
return ENV.HOST;
Expand Down
57 changes: 57 additions & 0 deletions packages/whook/src/services/HOST.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import initHOST from './HOST';

describe('initHOST', () => {
const log = jest.fn();
const internalIp = { v4: jest.fn() };

beforeEach(() => {
log.mockReset();
internalIp.v4.mockReset();
});

it('should use the env HOST first', async () => {
const HOST = await initHOST({
ENV: { HOST: '192.168.1.11' },
log,
internalIp,
});

expect({
HOST,
logCalls: log.mock.calls,
internalIpV4Calls: internalIp.v4.mock.calls,
}).toMatchSnapshot();
});

it('should find a HOST by itself if no env HOST', async () => {
internalIp.v4.mockResolvedValueOnce('192.168.1.10');

const HOST = await initHOST({
ENV: {},
log,
internalIp,
});

expect(HOST);
expect({
logCalls: log.mock.calls,
internalIpV4Calls: internalIp.v4.mock.calls,
}).toMatchSnapshot();
});

it('should fallback to localhost', async () => {
internalIp.v4.mockResolvedValueOnce('');

const HOST = await initHOST({
ENV: {},
log,
internalIp,
});

expect(HOST);
expect({
logCalls: log.mock.calls,
internalIpV4Calls: internalIp.v4.mock.calls,
}).toMatchSnapshot();
});
});
4 changes: 2 additions & 2 deletions packages/whook/src/services/PORT.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default initializer(
{
name: 'PORT',
type: 'service',
inject: ['ENV', '?log'],
inject: ['ENV?', '?log'],
options: { singleton: true },
},
initPORT,
Expand All @@ -29,7 +29,7 @@ export default initializer(
* @return {Promise<Number>}
* A promise of a number representing the actual port.
*/
async function initPORT({ ENV, log = noop }) {
async function initPORT({ ENV = {}, log = noop }) {
if ('undefined' !== typeof ENV.PORT) {
log('info', `Using ENV port ${ENV.PORT}`);
return ENV.PORT;
Expand Down
34 changes: 34 additions & 0 deletions packages/whook/src/services/PORT.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import initPORT from './PORT';

describe('initPORT', () => {
const log = jest.fn();

beforeEach(() => {
log.mockReset();
});

it('should use the env port first', async () => {
const port = await initPORT({
ENV: { PORT: 1337 },
log,
});

expect({
port,
logCalls: log.mock.calls,
}).toMatchSnapshot();
});

it('should find a port by itself if no env port', async () => {
const port = await initPORT({
log,
});

expect(port);
expect({
logCalls: log.mock.calls.map(([arg1, arg2, ...args]) => {
return [arg1, arg2.replace(/port (\d+)/, 'port ${PORT}'), ...args];
}),
}).toMatchSnapshot();
});
});
63 changes: 63 additions & 0 deletions packages/whook/src/services/__snapshots__/ENV.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`initENV should fail with non-existing file 1`] = `
Object {
"ENV": Object {
"KEY_BASE_ENV": "test",
"KEY_PROCESS_ENV": "test",
"NODE_ENV": "development",
"PWD": "/home/whoami/my-whook-project",
},
"logCalls": Array [
Array [
"info",
"Loading the environment service.",
],
Array [
"info",
"Using local env.",
],
Array [
"info",
"Could not load \\".env.development\\" file.",
],
],
"readFileCalls": Array [
Array [
"/home/whoami/my-whook-project/.env.development",
],
],
}
`;

exports[`initENV should work with existing file 1`] = `
Object {
"ENV": Object {
"DB_HOST": "localhost",
"DB_PASSWORD": "oudelali",
"KEY_BASE_ENV": "test",
"KEY_PROCESS_ENV": "test",
"NODE_ENV": "development",
"PWD": "/home/whoami/my-whook-project",
},
"logCalls": Array [
Array [
"info",
"Loading the environment service.",
],
Array [
"info",
"Using local env.",
],
Array [
"info",
"Using .env file at /home/whoami/my-whook-project/.env.development.",
],
],
"readFileCalls": Array [
Array [
"/home/whoami/my-whook-project/.env.development",
],
],
}
`;
Loading

0 comments on commit 624d756

Please sign in to comment.