Where to put test utils in Next.js projects?

Viewed 22

I have a pretty standard Next.js (12.x) React (18.x) project with Jest (28.x) for testing. My tests e.g. __tests__/Product.test.tsx will run just fine, but now I have some utils I'd like to reuse across tests and tried this:

__tests__/util/my-test-helper.ts

export function setupSomeThing() {
  return { foo: jest.fn() };
}

and then import that function in my tests. But the test runner complains with:

FAIL  __tests__/util/my-test-helper.ts

* Test suite failed to run
  Your test suite must contain at least one test.

How can I prevent this issue? The Next.js testing documentation seems to have no instruction for this case.

What is the convention or solution (or workaround) for Next.js Jest tests?

1 Answers

Sharing my current workaround, which is to put utils in:

__tests-utils__/my-test-helper.ts

As per the comments, perhaps a Jest test match pattern change would help, or perhaps a default exception already exists which could be used by convention. Regardless, above variant works too.

Related