Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failure to execute Vitest tests due to CommonJS issue #1714

Open
6 tasks done
zaru opened this issue Mar 26, 2024 · 0 comments
Open
6 tasks done

Failure to execute Vitest tests due to CommonJS issue #1714

zaru opened this issue Mar 26, 2024 · 0 comments

Comments

@zaru
Copy link

zaru commented Mar 26, 2024

Checklist

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant