Skip to content

Commit

Permalink
Merge pull request #23 from appannie/chore/dont-console-error-if-test…
Browse files Browse the repository at this point in the history
…-env

Don't console.error if NODE_ENV is test
  • Loading branch information
SBoudrias committed Nov 9, 2020
2 parents e8e449d + 640ed0d commit 8a91066
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
30 changes: 30 additions & 0 deletions packages/ab-testing/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,34 @@ describe('ab-testing module', () => {
expect(experiment.getCohort('experiment_2')).toEqual('test_force_include');
expect(experiment.getCohort('experiment_2')).toEqual('test_force_include');
});

it('does not show an error message if NODE_ENV is test', () => {
const spy = jest.spyOn(console, 'error').mockImplementation();

expect(
new Experiments(
config,
1,
hashObject({ user_id: 1, email_domain: 'example.com' }, config.salt)
).getCohort('experiment_3')
).toEqual('control');
expect(spy).not.toHaveBeenCalled();
});

it('shows an error message if NODE_ENV is not test', () => {
const initialEnv = global.process.env.NODE_ENV;
global.process.env.NODE_ENV = 'development';
const spy = jest.spyOn(console, 'error').mockImplementation();

expect(
new Experiments(
config,
1,
hashObject({ user_id: 1, email_domain: 'example.com' }, config.salt)
).getCohort('experiment_3')
).toEqual('control');
expect(spy).toHaveBeenCalled();

global.process.env.NODE_ENV = initialEnv;
});
});
3 changes: 2 additions & 1 deletion packages/ab-testing/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ export class Experiments {
if (!(experimentName in this.matchedCohorts)) {
const experimentConfig: Experiment = this.config[experimentName];
if (experimentConfig == null) {
console.error(`unrecognized ab testing experiment name: ${experimentName}`);
process.env.NODE_ENV !== 'test' &&
console.error(`unrecognized ab testing experiment name: ${experimentName}`);
this.matchedCohorts[experimentName] = 'control';
} else {
this.matchedCohorts[experimentName] = matchUserCohort(
Expand Down

0 comments on commit 8a91066

Please sign in to comment.