You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

chrome-stub

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

chrome-stub

Easily stub out chrome API calls for great test coverage

1.6.0
latest
Source
npmnpm
Version published
Weekly downloads
693
325.15%
Maintainers
1
Weekly downloads
 
Created
Source

chrome-stub

Chrome API mocking library.

npm install chrome-stub

Build Status Coverage Status

usage

In most cases you will want to set this as a global at the top of your test file, because chrome is a global in your extension code.

chrome = require('chrome-stub')

i18n

To use the I18n library, chrome-stub loads a default messages.json file that has only one string in it, 'test' If you pass a string that does not exist, you will receive a blank string back.

If you want to load a different messages.json file, for example the one you use in your chrome extension then use the method below in your test file.

_locales/ru/messages.json

{
	"test": {
		"message": "Тест"
	}
}

test.js

let path = require('path');

let config = path.resolve(process.cwd(), '_locales/ru/messages.json');

chrome.loadDefaults(config);
chrome.i18n.getMessage('test'); // Тест

All other APIs

All chrome APIs have been stubbed with sinon stub. You can define your own application-specific behavior of the stubs by following the sinon stub api. This allows you to define your behavior within your tests. For example:

// inside test
chrome.tabs.query.yields([{
  id:0,index:0,windowId:0,highlighted:true,
  active:true,pinned:false,url:'https://example.com',title:'example.com - Home',
  favIconUrl:'https://example.com/favicon.ico',status:'complete',width:800,height:600
}]);

// do something that causes your code to call chrome.tabs.query, e.g.:
getFirstTab(function(tab){
  // ...
  // your code's callback is called with the value provided to yields()
});

Originally forked from sethmcl/chrome-mock

Keywords

chrome

FAQs

Package last updated on 30 Aug 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts