"Document is not defined" in jest when testing server component that uses cookies
Unanswered
American Sable posted this in #help-forum
American SableOP
Trying to test a simple server component that uses the cookies function. (the jest-environment node comment is from https://github.com/vercel/next.js/discussions/44270#discussioncomment-6576533 to fix the
So that fixed that error. but now I get this error. Anyone having this issue?
x NEXT_RSC_ERR_CLIENT_IMPORT: next/headers error).So that fixed that error. but now I get this error. Anyone having this issue?
The error below may be caused by using the wrong test environment, see https://jestjs.io/docs/configuration#testenvironment-string. Consider using the "jsdom" test environment.
ReferenceError: document is not defined/** @jest-environment node */
import { cookies } from 'next/headers';
export default function TestComponent() {
const isAuthenticated = cookies().get('user_id');
return <div>foo</div>;
}/** @jest-environment node */
import { render, screen } from '@testing-library/react';
import TestComponent from './test-component';
it('should render foo', () => {
render(<TestComponent />);
expect(screen.queryByText('foo')).toBeInTheDocument();
});