vercel / analytics

Privacy-friendly, real-time traffic insights
https://vercel.com/analytics
Mozilla Public License 2.0
416 stars 24 forks source link

fix: export issue when testing with jest and ts-node #134

Closed feugy closed 5 months ago

feugy commented 5 months ago

🌤️ What's in there?

An attempt to reproduce and fix the invalid export issue with Jest.

🧪 How to test?

  1. cd apps/next
  2. pnpm i
  3. pnpm test

    Test should pass

To reproduce the issue: pnpm --ignore-workspace install -D ts-node @vercel/analytics@1.2.1

Then tests will fail:

  FAIL  app/api/serverless/route.test.ts
  ● Test suite failed to run

    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

    Details:

    /Users/damien/dev/analytics/apps/nextjs/node_modules/.pnpm/@vercel+analytics@1.2.1/node_modules/@vercel/analytics/dist/server/index.mjs:151
    export {
    ^^^^^^

    SyntaxError: Unexpected token 'export'

❗ Note to reviewer

The sole real change in this PR is in package.json, were we switch the Node.js export from ESM to CJS file. It's a bit unfair, since the docs says that we could either use CJS or ESM. I guess it's a nasty combination of jest, ts-node and node.

vercel[bot] commented 5 months ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
analytics-nextjs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 20, 2024 1:57pm
analytics-remix ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 20, 2024 1:57pm