nextjs-auth0 icon indicating copy to clipboard operation
nextjs-auth0 copied to clipboard

Failure to execute Vitest tests due to CommonJS issue

Open zaru opened this issue 11 months ago • 0 comments

Checklist

  • [X] The issue can be reproduced in the nextjs-auth0 sample app (or N/A).
  • [X] I have looked into the Readme, Examples, and FAQ and have not found a suitable solution or answer.
  • [X] I have looked into the API documentation and have not found a suitable solution or answer.
  • [X] I have searched the issues and have not found a suitable solution or answer.
  • [X] I have searched the Auth0 Community forums and have not found a suitable solution or answer.
  • [X] I agree to the terms within the Auth0 Code of Conduct.

Description

I'm implementing authentication checks in Next.js middleware and using Vitest for unit testing these middlewares. However, when importing oauth4webapi, an error occurs.

Reproduction

  1. Run npm install @auth0/nextjs-auth0 vitest to install the necessary packages.
  2. Write the minimal code to reproduce the issue:
// index.ts
import { withMiddlewareAuthRequired } from "@auth0/nextjs-auth0/edge";
export function main() {
  withMiddlewareAuthRequired();
  return "foo";
}
  1. Write the minimal test code:
// index.test.ts
import { expect, test } from 'vitest'
import { main } from "./index";

test('return foo', () => {
  expect(main()).toBe("foo")
})
  1. Execute the tests with npx vitest.
Error: require() of ES Module /Users/zaru/Sites/node-sandbox/node-esm/node_modules/oauth4webapi/build/index.js from /Users/zaru/Sites/node-sandbox/node-esm/node_modules/@auth0/nextjs-auth0/dist/auth0-session/client/edge-client.js not supported.
Instead, change the require of index.js in /Users/zaru/Sites/node-sandbox/node-esm/node_modules/@auth0/nextjs-auth0/dist/auth0-session/client/edge-client.js to a dynamic import() which is available in all CommonJS modules.
 ❯ Object.<anonymous> node_modules/@auth0/nextjs-auth0/dist/auth0-session/client/edge-client.js:5:36
 ❯ Object.<anonymous> node_modules/@auth0/nextjs-auth0/dist/edge.js:7:23
 ❯ async /Users/zaru/Sites/node-sandbox/node-esm/index.ts:1:31
 ❯ async /Users/zaru/Sites/node-sandbox/node-esm/index.test.ts:2:31

Additional context

No response

nextjs-auth0 version

3.5.0

Next.js version

14.1.4

Node.js version

20.5.1

zaru avatar Mar 26 '24 08:03 zaru