javascript - testing chrome.storage.local.set with jest - Stack Overflow

I'm a very beginner with testing and started using jest. This function I have saves data in chrome

I'm a very beginner with testing and started using jest. This function I have saves data in chrome.storage.local and I want to test if chrome.storage.local called 3 times. Here is the function:

saveData: function(){
  chrome.storage.local.set({'blackList': bgModule.blackList}, function() {});
  chrome.storage.local.set({'pastDays': bgModule.pastDays}, function() {});
  chrome.storage.local.set({'websiteList': bgModule.websiteList}, function() {});
}

and here is my test:

const bgModule = require("../../app/Background/background.js");

const chrome = {
  storage: {
    local: {
      set: function() {},
      get: function() {}
    }
  }
};

let blackListStub = bgModule.blackList;
let pastDaysStub = [
  {day: "day1"},
  {day2: "day2"},
  {day3: "day3"}];
let websiteListStub = [
  {websiteName: "facebook"},
  {websiteName: "stackoverflow"},
  {websiteName: "github"}];

it ("should save data in local storage", () => {
  spyOn(bgModule, 'saveData');
  bgModule.saveData();
  const spy = spyOn(chrome.storage.local, 'set');

  spy({'blackList' : blackListStub});
  spy({'pastDays' : pastDaysStub});
  spy({'websiteList' : websiteListStub});

  expect(spy).toHaveBeenCalledWith({'blackList' : blackListStub});
  expect(spy).toHaveBeenCalledWith({'pastDays' : pastDaysStub});
  expect(spy).toHaveBeenCalledWith({'websiteList' : websiteListStub});
  expect(bgModule.saveData).toHaveBeenCalledTimes(1);
  expect(spy).toHaveBeenCalledTimes(3);
});

Test passes but it's incorrect. I want to make it work so when I change saveData it will break the test. The test I have has everything hardcoded into it and doesn't depend on saveData at all. I looked for example and tutorial materials and couldn't find anything for this case. Any help or direction I should take would be very helpful.

After help and correct answer with use of global.chrome refactored test looks like this:

it ("should save data in local storage", () => {
  bgModule.saveData();
  expect(chrome.storage.local.set).toHaveBeenCalledTimes(3);
});  

I'm a very beginner with testing and started using jest. This function I have saves data in chrome.storage.local and I want to test if chrome.storage.local called 3 times. Here is the function:

saveData: function(){
  chrome.storage.local.set({'blackList': bgModule.blackList}, function() {});
  chrome.storage.local.set({'pastDays': bgModule.pastDays}, function() {});
  chrome.storage.local.set({'websiteList': bgModule.websiteList}, function() {});
}

and here is my test:

const bgModule = require("../../app/Background/background.js");

const chrome = {
  storage: {
    local: {
      set: function() {},
      get: function() {}
    }
  }
};

let blackListStub = bgModule.blackList;
let pastDaysStub = [
  {day: "day1"},
  {day2: "day2"},
  {day3: "day3"}];
let websiteListStub = [
  {websiteName: "facebook."},
  {websiteName: "stackoverflow."},
  {websiteName: "github."}];

it ("should save data in local storage", () => {
  spyOn(bgModule, 'saveData');
  bgModule.saveData();
  const spy = spyOn(chrome.storage.local, 'set');

  spy({'blackList' : blackListStub});
  spy({'pastDays' : pastDaysStub});
  spy({'websiteList' : websiteListStub});

  expect(spy).toHaveBeenCalledWith({'blackList' : blackListStub});
  expect(spy).toHaveBeenCalledWith({'pastDays' : pastDaysStub});
  expect(spy).toHaveBeenCalledWith({'websiteList' : websiteListStub});
  expect(bgModule.saveData).toHaveBeenCalledTimes(1);
  expect(spy).toHaveBeenCalledTimes(3);
});

Test passes but it's incorrect. I want to make it work so when I change saveData it will break the test. The test I have has everything hardcoded into it and doesn't depend on saveData at all. I looked for example and tutorial materials and couldn't find anything for this case. Any help or direction I should take would be very helpful.

After help and correct answer with use of global.chrome refactored test looks like this:

it ("should save data in local storage", () => {
  bgModule.saveData();
  expect(chrome.storage.local.set).toHaveBeenCalledTimes(3);
});  
Share Improve this question edited Feb 21, 2018 at 14:55 Nikita Chernykh asked Feb 20, 2018 at 21:49 Nikita ChernykhNikita Chernykh 2701 gold badge5 silver badges18 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

The easiest thing would be to use dependency injection, so instead of calling chrome.storage.local directly, passed it as an argument to your function that then can be easily replaced by a mock in your test.

The other way would be to add it as a global variable into your test environment like this:

const get = jest.fn()
const set = jest.fn()
global.chrome = {
  storage: {
    local: {
      set,
      get
    }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745259122a4619127.html

相关推荐

  • javascript - testing chrome.storage.local.set with jest - Stack Overflow

    I'm a very beginner with testing and started using jest. This function I have saves data in chrome

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信