Next.js Discord

Discord Forum

Error with jest and RTL tests

Unanswered
Pacific sand lance posted this in #help-forum
Open in Discord
Pacific sand lanceOP
Hi, I am trying to test my HomePage ui component to simply see if it renders text on the screen. But when I run my test, it always gives me some random error with node modules,

 Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation
'./jwe/compact/decrypt.js';
                                                                                      ^^^^^^

    SyntaxError: Unexpected token 'export'

5 Replies

Pacific sand lanceOP
import { render, screen } from '@testing-library/react';
import HomePage from '@/app/components/HomePage';
import '@testing-library/jest-dom';
import TimeLineTabs from '@/app/components/TimeLineTabs';

describe('Home', () => {
  it('renders a heading', async () => {
    const timelinePosts = [
      {
        id: 219,
        content: 'This is a detailed post by User 1, post number 1.',
        imageUrl: null,
        authorId: 89,
        createdAt: new Date(),
      },
    ];
    const otherTimeLinePosts = [
      {
        id: 31,
        content: 'This is a detailed post by User 2, post number 1.',
        imageUrl: null,
        authorId: 89,
        createdAt: new Date(),
      },
    ];
    render(<HomePage data={timelinePosts} otherData={otherTimeLinePosts} />);
    screen.debug();
    expect(screen.getByText('Main Content Here')).toBeVisible();
  });
});
//test
import { Post } from '../lib/definitions';
import { UserPost } from './UserPost';
import TimeLineTabs from './TimeLineTabs';
import { Text } from '@chakra-ui/react';
import { User } from '../lib/definitions';
import { getServerSession } from 'next-auth';
import { authOptions } from '../api/auth/[...nextauth]/authOptions';

interface HomePageProps {
  data: Post[];
  otherData: Post[];
}
export async function HomePage({ data, otherData }: HomePageProps) {
  const session = await getServerSession(authOptions);

  const { name, email, profilePicture } = (await prisma.user.findUnique({
    where: {
      id: session?.user.id,
    },
  }))!;

  return (
    <div>
      <TimeLineTabs
        data={data}
        otherData={otherData}
        name={name}
        email={email}
        profilePicture={profilePicture}
      />
    </div>
  );
}

export default HomePage;
//component
Pacific sand lanceOP
i tried so many different things from googling but nothing works my tests are just forever broken it seems
Pacific sand lanceOP
bump