acvetkov / sinon-chrome

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

Spying on browser.notifications.create fails with "attempted to wrap undefined property..." #102

Closed GrayedFox closed 3 years ago

GrayedFox commented 3 years ago

Hello, the following code errors with "TypeError: attempted to wrap undefined property create as function" even though create is a valid and defined method of the notifications API.

const sinon = require('sinon')
const browser = require('sinon-chrome/extensions')
const chrome = require('sinon-chrome')
const { assert } = require('chai')

describe('hello', function () {
  before(async function () {
    global.browser = browser // need to patch global browser with mocked api
    browser.menus = chrome.contextMenus // sinon-chrome doesn't wrap this method as it should
    sinon.stub(browser.i18n, 'getAcceptLanguages').yields(['de-de', 'en-au'])
    sinon.stub(browser.i18n, 'getUILanguage').yields('en-en')
    sinon.spy(browser.menus, 'create')
    sinon.spy(browser.notifications, 'create')
  })
})

Both the menus and notifications spies fail with the same type error.

Am I doing something wrong here?

GrayedFox commented 3 years ago

I debugged the notification and menu objects and realised that the create method is technically a getter.

Changing sinon.spy(browser.menus, 'create') to sinon.spy(browser.menus, 'create', ['get']) solved it, as per the Sinon getter spy/mock syntax.