在开玩笑中嘲笑(打字稿 - 模拟铬命名空间)
我有一个 chrome ground 。 我开始添加一些测试,但是我在一些测试方面很难。
我想测试我的 < app/>
component ,但是我以任何方式都会遇到错误。
通过这样做:
import React from "react";
import { render, fireEvent } from "@testing-library/react";
import App from "../../App";
import { BMEUtils } from "../../app/helpers/utils/BMEUtils";
import { FormContainerID } from "../../app/dom-elements/_components/forms/AbsoluteContainer";
import renderer from 'react-test-renderer';
describe("tests ", () => {
it("test App", () => {
const component = renderer.create(
<App />,
);
const b = document.getElementById(FormContainerID)
const a = document.getElementById("login-btn");
let tree = component.toJSON();
expect(tree).toMatchSnapshot();
expect(a).not.toBeNull();
expect(b).not.toBeNull();
});
});
我在测试过程中会遇到错误
ReferenceError: chrome is not defined
,因为其中一个组件调用此名称空间添加侦听器(chrome.runtime.onmessage.Addlistener
),
我尝试过:
global.chrome = jest.fn() as any;
jest.mock('chrome');
import chrome from "sinon-chrome";
beforeAll(function () {
global.chrome = chrome as any;
});
const chrome = require('sinon-chrome/extensions');
beforeAll(function () {
global.chrome = chrome ;
});
var globalRef:any =global;
globalRef.chrome = jest.fn().mockImplementation();
const component = renderer.create(
<App />,
);
import { mount } from 'enzyme';
const wrapper = mount(
<App/>
);
const p = wrapper.find('#login-btn');
,但没有一个为我工作。
这是我的主要问题。
我遇到的其他问题,也可能与以前有关的问题是,我不知道如何模拟我正在调用的方法中执行的某些函数的实现,而不是模块。
例如,
// Test as Base64 Image
// Utils is a namespace
test('base64Image', () => {
...
const file = ...;
let base64Image = Utils.asBase64Image(file);
expect(base64Image).not.toEqual("");
...
});
function asBase64Image(file:File) {
...
canvas.getContext("2d");
img.onload
...
}
我想出的解决方法是使用模块替换这些功能并模拟它们,例如,而不是img.onload
- &gt; Module.LoadImage(IMG)。然后(...)
但是,如果可能的话,我想学习正确的方法。
无论如何,我现在真正的问题是我无法测试我的&lt; app/&gt;
组件。
I have a chrome extension in react.
I started to add some test, but i am having a hard with some tests.
I want to test my <App/>
component, but i get an error in any way i have tried it.
By doing this:
import React from "react";
import { render, fireEvent } from "@testing-library/react";
import App from "../../App";
import { BMEUtils } from "../../app/helpers/utils/BMEUtils";
import { FormContainerID } from "../../app/dom-elements/_components/forms/AbsoluteContainer";
import renderer from 'react-test-renderer';
describe("tests ", () => {
it("test App", () => {
const component = renderer.create(
<App />,
);
const b = document.getElementById(FormContainerID)
const a = document.getElementById("login-btn");
let tree = component.toJSON();
expect(tree).toMatchSnapshot();
expect(a).not.toBeNull();
expect(b).not.toBeNull();
});
});
I get a error during the test
ReferenceError: chrome is not defined
Because one of the components calls this namespace to add a listener (chrome.runtime.onMessage.addListener
)
I have tried:
global.chrome = jest.fn() as any;
jest.mock('chrome');
import chrome from "sinon-chrome";
beforeAll(function () {
global.chrome = chrome as any;
});
const chrome = require('sinon-chrome/extensions');
beforeAll(function () {
global.chrome = chrome ;
});
var globalRef:any =global;
globalRef.chrome = jest.fn().mockImplementation();
const component = renderer.create(
<App />,
);
import { mount } from 'enzyme';
const wrapper = mount(
<App/>
);
const p = wrapper.find('#login-btn');
But none of them worked for me.
This is my main problem.
Other problem that i have, and may be related to the previous, is that i dont know how to mock the implementation of some functions that are executed inside the method i am calling, and are not modules.
For example
// Test as Base64 Image
// Utils is a namespace
test('base64Image', () => {
...
const file = ...;
let base64Image = Utils.asBase64Image(file);
expect(base64Image).not.toEqual("");
...
});
function asBase64Image(file:File) {
...
canvas.getContext("2d");
img.onload
...
}
The workaround that i came up with is to use Modules to replace those functions and mock them, for example instead of img.onload
-> module.loadImage(img).then(...)
but i want to learn the correct way to do it if it is possible.
Anyway my real problem now is that i can not test my <App/>
component.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于仍在2023年仍在挣扎的任何人,我通过以下方面解决:
https://stackoverflow.com/a/4446660338对于指针!
For anyone still struggling with this in 2023, I solved by the following:
credit to https://stackoverflow.com/a/44660338 for the pointer!