1

I am trying to test my project using a mock API instead of a real one to save money. However, when I add the following code to my setupTests.js file:

// setupTests.js
import { server } from "./mocks/server";

beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());

I get this error:

Test suite failed to run

    ReferenceError: TextEncoder is not defined

      1 | // mocks/server.js
    > 2 | import { setupServer } from 'msw/node';
        | ^
      3 | import { handlers } from './handlers'; // your request handlers
      4 |
      5 | export const server = setupServer(...handlers);


What We Tried:

  1. Polyfill for TextEncoder and TextDecoder:

    • We tried adding a polyfill for TextEncoder and TextDecoder in the setupTests.js file.
    • Code added:
      const { TextEncoder, TextDecoder } = require('util');
      global.TextEncoder = TextEncoder;
      global.TextDecoder = TextDecoder;
      
  2. Ensured Jest Environment is jsdom:

    • We checked and ensured that the Jest environment is set to jsdom to simulate a browser-like environment.
    • Jest configuration:
      // jest.config.js
      module.exports = {
        testEnvironment: 'jsdom',
        setupFilesAfterEnv: ['./setupTests.js'],
      };
      
  3. Updated Dependencies:

    • We updated all dependencies to their latest versions to ensure compatibility.
    • Command used:
      npm update
      
  4. Verified Mock Server Setup:

    • We ensured the mock server setup in mocks/server.js was correct.
    • Example:
      // mocks/server.js
      import { setupServer } from 'msw/node';
      import { handlers } from './handlers';
      
      export const server = setupServer(...handlers);
      
  5. Created a Minimal Test Case:

    • We created a minimal test case to isolate the problem.
    • Test code:
      // src/__tests__/example.test.js
      import { render, screen } from '@testing-library/react';
      import '@testing-library/jest-dom/extend-expect';
      import React from 'react';
      
      const TestComponent = () => <div>Hello, World!</div>;
      
      test('renders hello world', () => {
        render(<TestComponent />);
        expect(screen.getByText('Hello, World!')).toBeInTheDocument();
      });
      

What We Expected:

  • Polyfill for TextEncoder and TextDecoder:

    • We expected adding the polyfill to make TextEncoder and TextDecoder available in the Node.js environment, resolving the error.
  • Jest Environment as jsdom:

    • We expected using the jsdom environment to simulate a browser-like environment where TextEncoder and TextDecoder would be available.
  • Updated Dependencies:

    • We expected updating dependencies to resolve any compatibility issues that might be causing the error.
  • Verified Mock Server Setup:

    • We expected ensuring the correct setup of the mock server to eliminate any configuration issues.
  • Minimal Test Case:

    • We expected the minimal test case to help identify if there were any fundamental issues with the setup.

1
  • Are using this anywhere in your test files ? /** * @jest-environment jsdom */
    – Rahul
    Commented Jun 10 at 5:41

0

Browse other questions tagged or ask your own question.