acvetkov / sinon-chrome

Testing chrome extensions with Node.js
ISC License
437 stars 46 forks source link

Add support for chrome.runtime.lastError #9

Open sk- opened 8 years ago

sk- commented 8 years ago

Currently there's no support for chrome.runtime.lastError.

After importing sinon-chrome what I'm doing is the following:

chrome.runtime.lastErrorStub = sinon.stub();
Object.defineProperty(
  chrome.runtime, 'lastError',
  {
    get: chrome.runtime.lastErrorStub,
    set: function(value) {
      // TODO: maybe make the stub return this value
    }
  }
);

var originalChromeReset = chrome.reset;
chrome.reset = function() {
  originalChromeReset();
  chrome.runtime.lastErrorStub.reset();
  chrome.runtime.lastErrorStub.resetBehavior();
}

Feel free to grab that code.

acvetkov commented 8 years ago

Hi!

Thank for issue. We have supported chrome properties since version 1.0. Please, update your dependencies.

You can do

var chrome = require('sinon-chrome');

chrome.runtime.lastError = new Error('error');

// reset all props
chrome.flush(); 

// or

chrome.runtime.lastError = undefined;

Note: in v.1.0 we remove all predefined props and behaviors

sk- commented 8 years ago

Note that the current support of chrome.runtime.lastError does not help in complex cases, as you can only set one value.

If you want for example that the call to chrome.tabs.create succeeds and then the call to chrome.storage.local.get to fail then you would need to do something like what I first posted.

acvetkov commented 8 years ago

@sk- yes, you are right. Do you want to create pull request?