简体   繁体   English

chrome未定义-摩卡和打字稿

[英]chrome is not defined - Mocha and Typescript

I have a hard time compiling tests with typescript . 我很难用typescript编译测试。 When try to run npm test got 当尝试运行npm test

onClicked: chrome.browserAction !== undefined ? onClicked:chrome.browserAction!==未定义? chrome.browserAction.onClicked : undefined chrome.browserAction.onClicked:未定义

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 . app目录通过webpack进行编译,但是通过npm test抛出错误。 The following gulp task successfully runs with webpack. 以下gulp任务可通过webpack成功运行。

Gulp Task Gulp任务

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. chrome.browserAction是网络扩展代码。 Node.js does not know about the chrome global or web-extensions. Node.js不了解chrome全局或Web扩展。

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. 如果要测试Web扩展代码,则需要为所使用的每个API实现一个模拟实例,可惜的是,到目前为止,还没有类似的实例。 I have started creating some mocking functions for my extension here: https://github.com/Lusito/forget-me-not/blob/master/test/browserMock.ts 我已经开始在这里为我的扩展创建一些模拟功能: 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 我通过将chrome定义为

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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