在开玩笑中嘲笑(打字稿 - 模拟铬命名空间)

发布于 2025-02-01 12:49:54 字数 2379 浏览 6 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霓裳挽歌倾城醉 2025-02-08 12:49:55

对于仍在2023年仍在挣扎的任何人,我通过以下方面解决:


import { pathsToModuleNameMapper, type JestConfigWithTsJest } from 'ts-jest';
const chrome = require('sinon-chrome/extensions');

const config: JestConfigWithTsJest = {
  extensionsToTreatAsEsm: ['.ts'],
  verbose: true,
  preset: 'ts-jest/presets/default-esm',
  testEnvironment: 'node',
  transform: {
    '^.+\\.(ts)?

https://stackoverflow.com/a/4446660338对于指针!

: ['ts-jest', { useESM: true }], }, testPathIgnorePatterns: ['./dist'], moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths /*, { prefix: '<rootDir>/' } */), // setupFilesAfterEnv: ['./jest.setup.js'], globals: { chrome, }, }; export default config;

https://stackoverflow.com/a/4446660338对于指针!

For anyone still struggling with this in 2023, I solved by the following:


import { pathsToModuleNameMapper, type JestConfigWithTsJest } from 'ts-jest';
const chrome = require('sinon-chrome/extensions');

const config: JestConfigWithTsJest = {
  extensionsToTreatAsEsm: ['.ts'],
  verbose: true,
  preset: 'ts-jest/presets/default-esm',
  testEnvironment: 'node',
  transform: {
    '^.+\\.(ts)?

credit to https://stackoverflow.com/a/44660338 for the pointer!

: ['ts-jest', { useESM: true }], }, testPathIgnorePatterns: ['./dist'], moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths /*, { prefix: '<rootDir>/' } */), // setupFilesAfterEnv: ['./jest.setup.js'], globals: { chrome, }, }; export default config;

credit to https://stackoverflow.com/a/44660338 for the pointer!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文