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

Can't use the package with Jest: SyntaxError: Cannot use import statement outside a module #187

Open
zaxovaiko opened this issue Jul 27, 2024 · 0 comments

Comments

@zaxovaiko
Copy link

Hey. I love your package since it helps me mock complicated data in just a few lines.

Besides using the package for generating mocked data, I decided to use it in my unit/integrational tests that I wrote using Jest. However, when I'm trying to import the createSeedClient, I get an error. Before running tests, I execute npx @snaplet/seed sync.

➜  ✗ npm run test

> api@1.0.0 test
> jest --coverage --verbose --detectOpenHandles --forceExit

TypeError: Jest: Got error running globalSetup - /Users/.../apps/api/src/lib/jest/setup.ts, reason: Unknown file extension ".ts" for /Users/.../apps/api/src/lib/jest/setup.ts
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:160:9)
    at defaultGetFormat (node:internal/modules/esm/get_format:203:36)
    at defaultLoad (node:internal/modules/esm/load:143:22)
    at async ModuleLoader.load (node:internal/modules/esm/loader:409:7)
    at async ModuleLoader.moduleProvider (node:internal/modules/esm/loader:291:45)
    at async link (node:internal/modules/esm/module_job:76:21)
npm ERR! Lifecycle script `test` failed with error:
npm ERR! Error: command failed
npm ERR!   in workspace: api@1.0.0
npm ERR!   at location: /Users/.../apps/api

I tried literally everything, every combination of Jest configuration (esm), and read every documentation I ever could, but I still can't figure out how to fix that.

I'm not sure if the problem is directly related to your package rather than some limitations of the ts-jest library, but I hope you can direct me to the solution or at least a good idea of how to make a workaround for the problem.

package.json:

...
  "devDependencies": {
    "@snaplet/copycat": "5.0.0",
    "@snaplet/seed": "0.97.20",
    "@types/jest": "29.5.12",
    "jest": "29.7.0",
    "ts-jest": "29.2.3",
    "ts-node": "10.9.2",
    ...
  },

jest.config.ts:

...

const jestConfig: JestConfigWithTsJest = {
  testEnvironment: "node",
  preset: "ts-jest",
  rootDir: ".",
  globalSetup: "<rootDir>/src/lib/jest/setup.ts",
  globalTeardown: "<rootDir>/src/lib/jest/teardown.ts",
};

setup.js:

import Seed from "@snaplet/seed"; // here is the problem with an import

import { connectToDatabase, db } from "../database";

export default async function () {
  const seed = await Seed.createSeedClient({ dryRun: false });
  await connectToDatabase();
}

tsconfig.json:

{
  "$schema": "https://json.schemastore.org/tsconfig",
  "compilerOptions": {
    "rootDirs": ["src"],
    "outDir": "dist",
    "lib": ["es2020"],
    "target": "es2020",
    "module": "commonjs",
    "allowJs": true,
    "moduleResolution": "node",
    "types": ["node", "jest"],
    "strict": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "skipLibCheck": true,
    "baseUrl": ".",
    "rootDir": ".",
    "paths": {
      "@/*": ["src/*"],
    }
  },
  "exclude": ["node_modules", "generated.ts"]
}
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