I also faced these errors with the VTEX test library and struggled to perform the tests.
In my case, I uninstalled the vtex/test-tools
library and manually configured Jest to version 27, along with @apollo/react-testing
version 3.1.4.
When testing my component, I use the MockedProvider
to create mocks for both useQuery
and useMutation
.
Here’s an example of how a useMutation
was mocked:
import { UtmsPersistence, mapUrlUtmsToVars } from "./UtmsPersistence";
import { render, act, waitFor, screen } from "@testing-library/react";
import { OrderFormProviderMock } from "../../__mocks__/vtex.order-manager";
import { useRuntime } from "../../__mocks__/vtex.render-runtime";
import { MockedProvider } from "@apollo/react-testing";
import React from "react";
import UPDATE_MARKETING_DATA from "./graphql/UPDATE_MARKETING_DATA.graphql";
describe("components: UtmsPersistence", () => {
const setOrderForm = jest.fn();
beforeEach(jest.clearAllMocks);
it("should not render anything", async () => {
useRuntime.mockReturnValue({
route: {
queryString: {}
}
});
await act(async () => {
render(
<MockedProvider addTypename={false} mocks={[]} >
<OrderFormProviderMock >
<UtmsPersistence />
</OrderFormProviderMock>
</MockedProvider>
);
});
const textInDocument = screen.queryByText(/./);
expect(textInDocument).not.toBeInTheDocument();
});
it("should not apply parameters that are not used", async () => {
useRuntime.mockReturnValue({
route: {
queryString: {
fruta: "banana",
animal: "vaca",
}
}
});
const orderForm = {
marketingData: {
coupon: "",
utmCampaign: "",
utmMedium: "",
utmSource: "",
utmiCampaign: "",
utmiPart: "",
utmiPage: "",
__typename: "MarketingData"
}
};
const gqlMock = [{
request: {
query: UPDATE_MARKETING_DATA,
variables:
{ input: { utmSource: "source" } }
},
newData: jest.fn(() => ({
data: {
updateOrderFormMarketingData: {
marketingData: {
coupon: "",
utmCampaign: "",
utmMedium: "",
utmSource: "",
utmiCampaign: "",
utmiPart: "",
utmiPage: "",
__typename: "MarketingData"
}
}
}
})),
}];
await act(async () => {
render(
<MockedProvider addTypename={false} mocks={gqlMock} >
<OrderFormProviderMock orderForm={orderForm} setOrderForm={setOrderForm}>
<UtmsPersistence />
</OrderFormProviderMock>
</MockedProvider>
);
});
expect(gqlMock[0].newData).not.toHaveBeenCalled();
expect(setOrderForm).not.toHaveBeenCalled();
});
If you have any questions about setting up a specific frontend testing environment to run VTEX IO projects and avoid this error, I created a library/configuration on npm that you can install. It comes with all the necessary packages and the most up-to-date versions compatible with VTEX. Since both the Node and React/TypeScript versions from VTEX IO are outdated, finding compatible solutions can be challenging.
jest-vtex-io