Skip to content

Commit

Permalink
Adding test/nodeEnvSetupFile.js for Jest configuration
Browse files Browse the repository at this point in the history
Adding ApiContainer tests
  • Loading branch information
Borales committed Sep 13, 2018
1 parent efa2517 commit d89cf9d
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 13 deletions.
4 changes: 2 additions & 2 deletions examples/shop-with-blog/server/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
global.__SERVER__ = true; // eslint-disable-line no-underscore-dangle

process.on('unhandledRejection', (reason, p) => {
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
process.on('unhandledRejection', (reason, promise) => {
console.log('Unhandled Rejection at: Promise', promise, 'reason:', reason);
});
process.on('uncaughtException', ex => {
console.log('Uncaught Exception: ', ex);
Expand Down
13 changes: 13 additions & 0 deletions packages/falcon-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@
"test": "jest --watch",
"test:coverage": "jest --ci --coverage"
},
"jest": {
"collectCoverageFrom": [
"src/**/*.js"
],
"coverageReporters": [
"html",
"text",
"text-summary"
],
"setupFiles": [
"../../test/nodeEnvSetupFile"
]
},
"dependencies": {
"@deity/falcon-server-env": "^1.0.0",
"@deity/falcon-logger": "1.0.0",
Expand Down
31 changes: 31 additions & 0 deletions packages/falcon-server/src/containers/ApiContainer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const ApiContainer = require('./ApiContainer');

const apis = [{ package: 'fake-backend-api', name: 'fake-api' }];

describe('ApiContainer', () => {
it('Should register provided API DataSources', () => {
/** @type {ApiContainer} */
const container = new ApiContainer(apis);
expect(container.dataSources.size).toBe(1);
expect(container.endpoints).toHaveLength(1);

const apiInstance = container.dataSources.get('fake-api');
expect(apiInstance).toBeTruthy();
expect(apiInstance.name).toBe('fake-api');

const endpoint = container.endpoints[0];
expect(endpoint.path).toBe('/api/info');
});

it('Should do nothing for an empty API list', () => {
const container = new ApiContainer();
expect(container.dataSources.size).toBe(0);
expect(container.endpoints).toHaveLength(0);
});

it('Should not fail for missing API DataSource package', () => {
const container = new ApiContainer([{ package: 'foo-bar' }]);

expect(container.dataSources.size).toBe(0);
});
});
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
global.__SERVER__ = true; // eslint-disable-line no-underscore-dangle

const { mockServer } = require('graphql-tools');
const Logger = require('@deity/falcon-logger');
const ExtensionContainer = require('./ExtensionContainer');

// disable logger for tests
Logger.setLogLevel('error');

const extensions = [
{
package: 'fake-shop-extension',
Expand All @@ -31,7 +25,7 @@ const mocks = {
})
};

describe('ExtensionsContainer', () => {
describe('ExtensionContainer', () => {
let container;

beforeEach(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { ApiDataSource } = require('@deity/falcon-server-env');

module.exports = class FakeBackendApi extends ApiDataSource {
getEndpoints() {
return [
{
path: '/api/info',
methods: ['get'],
handler: () => {}
}
];
}
};
9 changes: 5 additions & 4 deletions packages/falcon-server/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,11 @@ class FalconServer {

start() {
const handleStartupError = err => {
this.eventEmitter.emit(Events.ERROR, err);
Logger.error('FalconServer: Initialization error - cannot start the server');
Logger.error(err.stack);
process.exit(2);
this.eventEmitter.emitAsync(Events.ERROR, err).then(() => {
Logger.error('FalconServer: Initialization error - cannot start the server');
Logger.error(err.stack);
process.exit(2);
});
};

this.initialize()
Expand Down
7 changes: 7 additions & 0 deletions test/nodeEnvSetupFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// eslint-disable-next-line no-underscore-dangle
global.__SERVER__ = true;
// eslint-disable-next-line import/no-extraneous-dependencies
const Logger = require('@deity/falcon-logger');

// disable logger for tests
Logger.setLogLevel('error');

0 comments on commit d89cf9d

Please sign in to comment.