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

Using vitest in a monorepo produces an unexpected module dependency error #27481

Open
ardabeyazoglu opened this issue Dec 27, 2024 · 0 comments
Labels
bug Something isn't working correctly node compat

Comments

@ardabeyazoglu
Copy link

ardabeyazoglu commented Dec 27, 2024

Rewriting my issue from #23882 as requested, since it was bloated.

Trying to use Deno 2.1.4 with Vitest 2.1.8 in a monorepo produces a strange error.

The monorepo (nx) is roughly as follows:

  • apps/
    • node-app1/
    • node-app2/
    • deno-app/
      • functions/
      • deno.json, deno.lock...
      • node_modules
  • libs/
    • shared/src/...
  • node_modules/
  • package.json, package-lock.json

When i run deno task --unstable-detect-cjs --unstable-sloppy-imports --unstable-node-globals test-vitest run inside the deno-app folder, i get the following error:

 FAIL  functions/_tests/vitest.example.test.ts [ functions/_tests/vitest.example.test.ts ]
ReferenceError: exports is not defined
 ❯ ../../node_modules/redis/dist/index.js:16:23
     14|     for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
     15| };
     16| Object.defineProperty(exports, "__esModule", { value: true });
       |                       ^
     17| exports.createCluster = exports.createClient = void 0;
     18| const client_1 = require("@redis/client");

The error is related to node-redis package. I am not actually using node-redis as it is not compatible with Deno, though I have to keep it because my node apps use it. Also, the error indicates that Deno detects it from the node_modules outside the deno app, not the one inside the deno-app. The root node_modules is only for the node apps, and i don't reference anything there from deno-app.

deno.json

{
  "importMap": "./functions/import_map.json",
  "nodeModulesDir": "auto",
  "compilerOptions": {
    "lib": ["deno.window", "deno.unstable"]
  },
  "tasks": {
    "test-vitest": "vitest"
  }
}

import_map.json:

{
  "imports": {
    "assert": "node:assert",
    "async_hooks": "node:async_hooks",
    "buffer": "node:buffer",
    "child_process": "node:child_process",
    "crypto": "node:crypto",
    "deno-redis": "jsr:@db/redis@0.37.0",
    "dns": "node:dns",
    "dns/promises": "node:dns/promises",
    "@shared/": "../../../libs/shared/src/",
    "dotenv": "npm:dotenv",
    "events": "node:events",
    "fs": "node:fs",
    "fs/promises": "node:fs/promises",
    "happy-dom": "npm:happy-dom",
    "http": "node:http",
    "https": "node:https",
    "http2": "node:http2",
    "module": "node:module",
    "net": "node:net",
    "os": "node:os",
    "path": "node:path",
    "perf_hooks": "node:perf_hooks",
    "redis": "npm:redis",
    "querystring": "node:querystring",
    "readline": "node:readline",
    "readline/promises": "node:readline/promises",
    "stream": "node:stream",
    "stream/promises": "node:stream/promises",
    "stream/consumers": "node:stream/consumers",
    "stream/web": "node:stream/web",
    "tls": "node:tls",
    "tty": "node:tty",
    "url": "node:url",
    "util": "node:util",
    "vitest": "npm:vitest@2.1.8",
    "zlib": "node:zlib"
  }
}

vite.config.mts

import { defineConfig } from "vitest/config";
import path from "path";
export default defineConfig({
  root: Deno.cwd(),
  resolve: {
    alias: {
      "@shared": path.resolve("../../libs/shared/src"),
    }
  },
  test: {
    watch: false,
    globals: true,
    environment: "node", // also tried happy-dom
    include: ["functions/_tests/vitest.example.test.ts"],
    passWithNoTests: true,
    pool: "threads",
    isolate: false,
    setupFiles: [
      "./vitest.setup.mts"
    ]
  },
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working correctly node compat
Projects
None yet
Development

No branches or pull requests

2 participants