简体   繁体   中英

chrome is not defined - Mocha and Typescript

I have a hard time compiling tests with typescript . When try to run npm test got

onClicked: chrome.browserAction !== undefined ? chrome.browserAction.onClicked : undefined

I have basic test that looks likes something:

import Nightmare from "nightmare";
import * as chai from "chai";
const Client = require('node-rest-client').Client;
import chaiString from "chai-string";
chai.use(chaiString);

import DomainDriver from "../../app/scripts/domains/driver";
const expect: any = chai.expect;

The test configuration is as follows:

{
  ...
  "test": "mocha --recursive --require ts-node/register --require babel-core/register tests/domains/*"
},

The module code is as follows:

export default {
  browserAction: {
    onClicked: chrome.browserAction !== undefined ? chrome.browserAction.onClicked : undefined,

The app directory get's compiled with webpack but it throws error with npm test . The following gulp task successfully runs with webpack.

Gulp Task

gulp.task(`${PLATFORM}-webpack`, (callback) => {

    webpack(Object.create(webpackConfig), function(err, stats) {
        if(err) throw new Exception(`${PLATFORM}-webpack`, err);
        console.log(`[${PLATFORM}-webpack]`, stats.toString({
            colors: true
        }));
        callback();
    });
});

Am I still missing something? Thank you

查看目录除非键入目录,否则您要查找的文件位于“ ../../app/domains/driver.ts”中。

chrome.browserAction is web-extensions code. Node.js does not know about the chrome global or web-extensions.

If you want to test web-extension code, you will need to implement a mock instance for every API you use, as sadly, nothing like it exists so far. I have started creating some mocking functions for my extension here: https://github.com/Lusito/forget-me-not/blob/master/test/browserMock.ts

Keep in mind, I made those to fit my needs. They are in no way complete nor do they cover all cases.

I solved it by defining chrome as

const chrome = require('sinon-chrome/apps');
(global as any).chrome = chrome;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM