From e0539d253a09e5ef6ca7b20f749550fe38f472bc Mon Sep 17 00:00:00 2001 From: Jason Lengstorf Date: Mon, 16 Oct 2017 12:04:29 -0500 Subject: [PATCH] fix: correctly support live data fix ##18 --- src/dev/app.js | 5 ++++- src/dev/server.js | 6 +++++- test/dev/server-test.js | 17 +++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/dev/app.js b/src/dev/app.js index f671ec1..5a5cfb5 100644 --- a/src/dev/app.js +++ b/src/dev/app.js @@ -6,12 +6,15 @@ import { grampsExpress } from '../index'; const app = express(); +const enableMockData = + process.env.NODE_ENV !== 'production' && process.env.GRAMPS_MODE !== 'live'; + app.use(bodyParser.json()); app.use( grampsExpress({ dataSources: [], - enableMockData: process.env.NODE_ENV !== 'production', + enableMockData, extraContext: req => ({ req }), }), ); diff --git a/src/dev/server.js b/src/dev/server.js index c81ba72..4e72742 100644 --- a/src/dev/server.js +++ b/src/dev/server.js @@ -5,13 +5,17 @@ import app from './app'; // Get the port from the various places it can be set const port = parseInt(process.env.PORT, 10) || 8080; +const mode = + process.env.NODE_ENV === 'production' || process.env.GRAMPS_MODE === 'live' + ? 'live' + : 'mock'; // Start the server, then display app details and URL info. app.set('port', port); app.listen(app.get('port'), () => { const message = [ `${EOL}============================================================`, - ` GrAMPS is running in ${process.env.NODE_ENV} mode on port ${port}`, + ` GrAMPS is running in ${mode} mode on port ${port}`, '', ` GraphiQL: http://localhost:${port}/graphiql`, `============================================================${EOL}`, diff --git a/test/dev/server-test.js b/test/dev/server-test.js index 8fbe4e6..0dab3cc 100644 --- a/test/dev/server-test.js +++ b/test/dev/server-test.js @@ -20,4 +20,21 @@ describe('dev/server', () => { it('calls the listen method with the correct arguments', () => { expect(app.listen.mock.calls[0][0]).toBe(9999); }); + + it('correctly sets the mode based on the env', () => { + jest.resetModules(); + + process.env.NODE_ENV = 'production'; + + // eslint-disable-next-line global-require + const logger = require('../../src/lib/defaultLogger'); + const spy = jest.spyOn(logger, 'info'); + + // eslint-disable-next-line global-require + require('../../src/dev/server'); + + expect(spy).toHaveBeenCalledWith( + expect.stringMatching(/running in live mode/), + ); + }); });