test-setup.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. const tsConfigPaths = require("tsconfig-paths")
  2. const fs = require("fs")
  3. const path = require("path")
  4. const Module = require("module")
  5. const baseUrl = path.resolve(__dirname)
  6. const tsConfig = JSON.parse(fs.readFileSync(path.join(baseUrl, "tsconfig.json"), "utf-8"))
  7. /**
  8. * The aliases point towards the `src` directory.
  9. * However, `tsc` doesn't compile paths by itself
  10. * (https://www.typescriptlang.org/docs/handbook/modules/reference.html#paths-does-not-affect-emit)
  11. * So we need to use tsconfig-paths to resolve the aliases when running tests,
  12. * but pointing to `out` instead.
  13. */
  14. const outPaths = {}
  15. Object.keys(tsConfig.compilerOptions.paths).forEach((key) => {
  16. const value = tsConfig.compilerOptions.paths[key]
  17. outPaths[key] = value.map((path) => path.replace("src", "out/src"))
  18. })
  19. tsConfigPaths.register({
  20. baseUrl: baseUrl,
  21. paths: outPaths,
  22. })
  23. // Mock the @google/genai module to avoid ESM compatibility issues in tests
  24. // The module is ES6 only, but the integration tests are compiled to commonJS.
  25. const originalRequire = Module.prototype.require
  26. Module.prototype.require = function (id) {
  27. // Intercept requires for @google/genai
  28. if (id === "@google/genai") {
  29. // Return the mock instead
  30. const mockPath = path.join(baseUrl, "out/src/core/api/providers/gemini-mock.test.js")
  31. return originalRequire.call(this, mockPath)
  32. }
  33. return originalRequire.call(this, id)
  34. }