Skip to content

fix(test): Suppress logs when NODE_ENV=test #147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from

Conversation

spencerwilson-optimizely
Copy link
Contributor

@spencerwilson-optimizely spencerwilson-optimizely commented Jul 12, 2018

Summary

  • Ensure console isn't logged to during unit tests
  • Refactor createInstance tests.
  • Delete superfluous indent in lib/optimizely > constructor tests. When viewing the diff in GitHub, try the 'Hide whitespace changes' option to make this diff clearer.
  • Simplify npm test definition. The bug here before was a lack of 'single quotes' around the glob pattern, which prevent glob expansion by bash (the interpreter of npm run-scripts).
  • Add *.swp (vim temporary files) to gitignore

The status quo is noise like this. Now,

$ npm test

> @optimizely/optimizely-sdk@2.1.1 test /Users/swilson/code/javascript-sdk/packages/optimizely-sdk
> NODE_ENV=test mocha --reporter dot './lib/**/*.tests.js'



  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․sandbox.stub(obj, 'meth', val) is deprecated and will be removed from the public API in a future version of sinon.
 Use sandbox.stub(obj, 'meth').callsFake(fn) instead in order to stub a function.
 Use sandbox.stub(obj, 'meth').value(fn) instead in order to stub a non-function value.
sinon.stub(obj, 'meth', fn) is deprecated and will be removed from the public API in a future version of sinon.
 Use stub(obj, 'meth').callsFake(fn).
 Codemod available at https://github.com/hurrymaplelad/sinon-codemod
․sandbox.stub(obj, 'meth', val) is deprecated and will be removed from the public API in a future version of sinon.
 Use sandbox.stub(obj, 'meth').callsFake(fn) instead in order to stub a function.
 Use sandbox.stub(obj, 'meth').value(fn) instead in order to stub a non-function value.
sinon.stub(obj, 'meth', fn) is deprecated and will be removed from the public API in a future version of sinon.
 Use stub(obj, 'meth').callsFake(fn).
 Codemod available at https://github.com/hurrymaplelad/sinon-codemod
․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
  ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․

  372 passing (616ms)

With the noise removed, deprecation warnings from Sinon are now clearly visible. Hooray!

Initially I wanted to avoid putting the NODE_ENV stuff inside the logger module, instead trying to stub logger.createInstance in the appropriate places in the tests. Down this path, I discovered that one issue was that, though the logger in most (all?) tests is properly stubbed, the optlyInstance is often constructed before the createdLogger.log method is stubbed (example), which yields the symptom of lots of log messages like

[OPTIMIZELY] - INFO Tue Jul 03 2018 22:52:25 GMT+0000 (Coordinated Universal Time) OPTIMIZELY: Datafile is valid.

making it through before the stub. (See how many of these are in the example build link above.)

I tried moving the optlyInstance construction after createdLogger.log was stubbed, but that caused all of the many assertions like this one to start to fail. This was kind of a bummer and I'm not sure how much value asserting exact text of log messages adds, so at this point I pivoted to the NODE_ENV approach.

Test plan

In the master branch, there are 371 tests. In this branch, 372. That's from the createInstance refactor, which net created 1 new test:

  optimizelyFactory
    APIs
      createInstance
        ✓ should create an instance of optimizely
        config fails validation
          ✓ catches internal errors
          a logger was supplied
            ✓ an error message is submitted to the supplied logger
          a logger was not supplied
            ✓ an error message is submitted to a simple logger

@coveralls
Copy link

coveralls commented Jul 12, 2018

Coverage Status

Coverage decreased (-0.03%) to 97.078% when pulling 8d0957c on sw/supress-test-logs into 6a3c9a5 on master.

@spencerwilson-optimizely
Copy link
Contributor Author

This is ready for review, please.

optimizelyFactory.createInstance({
datafile: {},
context('a logger was supplied', function() {
it('an error message is submitted to the supplied logger', function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I would make the message something like "submits an error message to the supplied logger" so it reads better in combination with the context.

@@ -78,6 +78,15 @@ Logger.prototype.setLogLevel = function(logLevel) {
* @private
*/
Logger.prototype.__shouldLog = function(targetLogLevel) {
try {
if (process.env.NODE_ENV === 'test') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Could we not just configure the test to set the logLevel to enums.LOG_LEVEL.ERROR + 1 (or some other really high value)? Then you wouldn't have to mess with the env in the logger test.

@mikeproeng37
Copy link
Contributor

Obsolete

@mikeproeng37 mikeproeng37 deleted the sw/supress-test-logs branch February 21, 2019 23:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants