Jest.js TypeError:无法解构“React__namespace.useContext(...)”的属性“basename”,因为它为null

igetnqfo  于 2023-04-27  发布在  Jest
关注(0)|答案(2)|浏览(514)

我用Jest执行React测试,在测试运行后,它在render(<Carrinho ItensCarrinho={ itensCarrinho } incrementeCarrinho={ incrementeCarrinhoMock } decrementeCarrinho={ decrementeCarrinhoMock }/>);行返回错误TypeError: Cannot destructure property 'basename' of 'React__namespace.useContext(...)' as it is null.
我已经看到了帖子Uncaught TypeError: Cannot destructure property 'basename' of 'React2.useContext(...)' as it is null,它并没有解决我的问题。
Carrinho.test.js:

import React from "react";
import { render, screen } from "@testing-library/react";
import "@testing-library/jest-dom";
import userEvent from "@testing-library/user-event";
import Carrinho from '../components/Carrinho.js';

describe("Carrinho component", () => {
    const itensCarrinho = [{
        id: "1",
        nome: "Fone de ouvido",
        preco: 100,
        promo: 0,
        src: "nome.jpg",
        detalhes: "detalhes",
        quantidade: 1,
    }];
    const incrementeCarrinhoMock = jest.fn();
    const decrementeCarrinhoMock = jest.fn();
    it("renders correct heading", () => {
      render(<Carrinho ItensCarrinho={ itensCarrinho } incrementeCarrinho={ incrementeCarrinhoMock } decrementeCarrinho={ decrementeCarrinhoMock }/>);
      expect(screen.getByRole("heading").textContent).toMatch(/Total:/i);
    });
});

Carrinho.js:

import '../styles/carrinho.css';
import { Link } from "react-router-dom";

function Carrinho( { ItensCarrinho, incrementeCarrinho, decrementeCarrinho }){
    var total=0;
    ItensCarrinho.map((item) => {
        if(item.promo !== 0){
            total = total + item.quantidade*item.promo;
        }else{
            total = total + item.quantidade*item.preco;
        }
    })
    console.log(ItensCarrinho);
    return (
        <div id="produtoCarrinho">
            <div id="fundoCarrinho">
                <h1>Carrinho</h1>
                {ItensCarrinho.map((item) => (
                    <div id="itenCarrinho">
                        <img src={ item.src } alt={ item.nome }></img>
                        <div id="detalhes">
                            <h3>{ item.nome }</h3>
                            <p>{ item.detalhes }</p>
                            <p>{ (item.promo !=="0") ? "De R$ " + item.preco + ",00 por R$ " + item.promo+",00" : "R$ "+item.preco+",00" }</p>
                            <div id="btnIncrementar">
                                <button onClick={ () => { decrementeCarrinho(item.id) } }> - </button>
                                <p> { item.quantidade } </p>
                                <button onClick={ () => { incrementeCarrinho(item.id) } }> + </button>  
                            </div>
                        </div>
                    </div>
                ))}
                <h2>Total: R${ total },00</h2>
                <div id="btnCompre">
                    <Link to="/products/"><button>Continue comprando</button></Link>
                </div>
            </div>
        </div>
        );
}

export default Carrinho;
0vvn1miw

0vvn1miw1#

问题出在Link的使用上。该组件需要出现在路由器内部,但在您的测试中却没有。
您可以尝试使用react-router-dom中的MemoryRouter(* 这是最轻的一个 *) Package 组件来克服这个问题。

render(<MemoryRouter><Carrinho ... /></MemoryRouter>);
hyrbngr7

hyrbngr72#

我认为它是Carriho组件中使用的prop名称。在测试中,您将ItensCarriho作为prop传递,但在组件中,您使用ItensCarriho而不是ItensCarriho(小写“i”)进行解构。使ItensCarriho为null?,导致错误。
将传递到测试文件中的prop名称更改为itensCarrinho(小写“i”),如下所示:

render(<Carrinho itensCarrinho={ itensCarrinho } incrementeCarrinho={ incrementeCarrinhoMock } decrementeCarrinho={ decrementeCarrinhoMock }/>);

此外,在Carriho组件中,确保使用itensCarriho(小写字母“i”)进行解构,如下所示:

function Carrinho( { itensCarrinho, incrementeCarrinho, decrementeCarrinho }){

这可以解决它吗?在你的测试文件中,你用大写的“I”将prop ItensCarrinho传递给你的Carrinho组件,但是在你的Carrinho组件中,你用小写的“i”解构prop。这导致ItensCarrinho prop在Carrinho组件中未定义,导致你看到的错误。
要解决此问题,您可以更改Carriho组件中的 prop 名称,以匹配测试文件中 prop 的大写。

相关问题