Taztingo
Taztingo

Reputation: 1945

Ember how to test my console.log wrapper?

I have a log service that wraps console.log. I'm having difficulty testing it because console.log is async. I override console.log, but I'm still having async issues. The console.log function is being called after my assert.

wrapper(msg) {
  console.log.bind(console); //I am binding it because in my real code the console is looked up in a map.
  console.log(msg);
}

test('log() can handle valid mask', function (assert) {
  let passed = false;
  subject = this.subject();
  console.log = function() {
    passed = true;
  };  

  subject.wrapper('testing');                        
  assert.equal(passed, true);
});

How do I get it to wait for console.log to run? I attempted using promises, but I had no luck with them.

Upvotes: 0

Views: 625

Answers (1)

Vasiliy vvscode Vanchuk
Vasiliy vvscode Vanchuk

Reputation: 7169

You can use async from qunit

test('log() can handle valid mask', function (assert) {
  let passed = false;
  debugger;
  console.log = function() {
    setTimeout(function() {
          passed = true;
    }, 0);
  };

  var done1 = assert.async();
  wrapper('testing');

  setTimeout(function() {
    assert.equal(passed, true);
    done1();
  }, 100);
});

http://jsbin.com/nozoruxori/edit?js,output works fine

with Ember for async checking you can use andThen wrapper ( for async assertion )

also looks like console.log.bind(console); //I am binding it because in my real code the console is looked up in a map. miss something cause in this state it has no sense

Upvotes: 1

Related Questions