vercel / analytics

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

Must use import to load ES Module - Jest #67

Closed wadehammes closed 1 year ago

wadehammes commented 1 year ago

I'm getting this error in my Jest tests when trying to us va.track:

image

Here is my jest.config.ts:

// jest.config.ts
import type { Config } from "@jest/types";
import nextJest from "next/jest";

// Sync object
const customJestConfig: Config.InitialOptions = {
  verbose: true,
  testPathIgnorePatterns: ["<rootDir>/.next/", "<rootDir>/node_modules/"],
  setupFiles: ["<rootDir>/.jest/setEnvVars.ts"],
  setupFilesAfterEnv: ["<rootDir>/.jest/setupTests.ts"],
  moduleDirectories: ["node_modules", "<rootDir>"],
  testEnvironment: "jest-environment-jsdom",
  transformIgnorePatterns: ["<rootDir>/node_modules/(?!isbot|swiper)"],
};

// Providing the path to your Next.js app which will enable loading next.config.js and .env files
const createJestConfig = nextJest({ dir: "./" })(customJestConfig);

module.exports = async () => {
  // Create Next.js jest configuration presets
  const jestConfig = await createJestConfig();

  // Custom `moduleNameMapper` configuration
  const moduleNameMapper = {
    ...jestConfig.moduleNameMapper,
    "swiper/react": "<rootDir>/node_modules/swiper",
    "\\.(css|less|scss|sass)$": "identity-obj-proxy",
  };

  return { ...jestConfig, moduleNameMapper, testTimeout: 20000 };
};

Any idea if this is something on my end?

wadehammes commented 1 year ago

For others that stumble on this, was as simple as adding "@vercel/analytics": "identity-obj-proxy", to the moduleNameMapper.

const moduleNameMapper = {
    ...jestConfig.moduleNameMapper,
    "swiper/react": "<rootDir>/node_modules/swiper",
    "@vercel/analytics": "identity-obj-proxy",
    "\\.(css|less|scss|sass)$": "identity-obj-proxy",
  };
mrchief commented 10 months ago

Sneaky stuff that should be documented but is not. Thanks for the solution!