I have code that loads a plugin dynamically based on environment.
Because I am using jest --coverage
, and 100% coverage is required, I need to test if the plugin was loaded or not given the environment conditions set in the NODE_ENV.
All I am looking for here is how to determine if a particular plugin has been registered or not, after I compose the server object with Glue()
and a manifest file that has logic to include the plugin only when NODE_ENV
is not "production"
.
In case someone else is trying to figure this out, here is what I ended up doing:
describe('GIVEN NODE_ENV is production', () => {
describe('WHEN service is initialized', () => {
let server;
const OLD_ENV = process.env;
beforeEach(async () => {
jest.resetModules();
process.env = { ...OLD_ENV };
process.env.NODE_ENV = 'production';
server = await initialize();
});
afterEach(async () => {
process.env = OLD_ENV;
await server.stop();
server = null;
});
test('THEN swagger plugin should not be added', () => {
expect(server.registrations['hapi-swagger']).not.toBeDefined();
});
});
});