1

I have the following code in background.js


var allCookies = [];
function getAllCookies() {
   chrome.cookies.getAll({}, function(cookies) {
    for (var i in cookies) {
      allCookies.push(cookies[i])
     }
    }
   });
}

Now in specs.js, I have written the following code to test the getAllCookies method -


describe('getAllCookies', function() {
    beforeEach(function() {
      chrome = {
        cookies: {
            getAll : function() { 
                return [
                    'cookie1',
                    'cookie2'
                ]
            }
        }
      };
      spyOn(chrome.cookies,'getAll');
    });
    it('should updated global variable allCookies', function() {
      getAllCookies();
      expect(allCookies).toEqual(['cookie1','cookie2'])
    })
  })

But the test is failing as allCookies = [] but it should be equal to ['cookie1','cookie2']

Can someone please help me to mock such chrome APIs (say chrome.cookies.getAll) which takes callback function as argument?

  • Have you had a look at sinon-chrome: https://stackoverflow.com/questions/2869827/how-to-test-chrome-extensions and https://github.com/acvetkov/sinon-chrome? – serv-inc Jun 26 '19 at 10:21

0 Answers0