Use Vitest for testing with ESM

XMLWordPrintableJSON

    • Type: Task
    • Resolution: Done
    • Priority: Major - P3
    • 1.9.0
    • Affects Version/s: None
    • Component/s: None
    • None
    • Not Needed
    • Developer Tools

      Jest support for ESM is experimental. This is particularly bad for us when importing mongodb-mcp-server as it depends on openid-client which is an ESM only package.

      We end up with issues like
      ```
      ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/metricsServer.test.ts.

      (node:769) ExperimentalWarning: VM Modules is an experimental feature and might change at any time
      (Use `node --trace-warnings ...` to show where the warning was created)
      FAIL Remote MCP Server src/metricsServer.test.ts
      ● Test suite failed to run

      Must use import to load ES Module: /Users/gagik.amaryan/mdb/mms/systems/common/typescript/metrics-server/index.ts

      at Runtime.requireModule (../../node_modules/.pnpm/jest-runtime@29.7.0/node_modules/jest-runtime/build/index.js:850:21)

      FAIL Remote MCP Server src/config/env/base.test.ts
      ● Test suite failed to run

      Must use import to load ES Module: /Users/gagik.amaryan/mdb/mms/systems/node_modules/.pnpm/openid-client@6.8.1/node_modules/openid-client/build/index.js

      at Runtime.requireModule (../../node_modules/.pnpm/jest-runtime@29.7.0/node_modules/jest-runtime/build/index.js:850:21)
      at Object.<anonymous> (../../node_modules/.pnpm/@mongodb-js+oidc-plugin@2.0.6/node_modules/@mongodb-js/oidc-plugin/src/plugin.ts:38:1)
      at Object.<anonymous> (../../node_modules/.pnpm/@mongodb-js+oidc-plugin@2.0.6/node_modules/@mongodb-js/oidc-plugin/src/api.ts:5:1)
      at Object.<anonymous> (../../node_modules/.pnpm/@mongodb-js+oidc-plugin@2.0.6/node_modules/@mongodb-js/oidc-plugin/src/index.ts:1:1)
      at Object.<anonymous> (../../node_modules/.pnpm/@mongodb-js+devtools-connect@3.14.2_@mongodb-js+oidc-plugin@2.0.6_mongodb-log-writer@2._e30d7f395b69c99959dc6d9eb2949c70/node_modules/@mongodb-js/devtools-connect/src/connect.ts:19:1)
      at Object.<anonymous> (../../node_modules/.pnpm/@mongodb-js+devtools-connect@3.14.2_@mongodb-js+oidc-plugin@2.0.6_mongodb-log-writer@2._e30d7f395b69c99959dc6d9eb2949c70/node_modules/@mongodb-js/devtools-connect/src/index.ts:2:1)
      at Object.<anonymous> (../../node_modules/.pnpm/@mongosh+service-provider-node-driver@3.18.0_@aws-sdk+credential-providers@3.969.0_mongodb-log-writer@2.5.4_bson@7.0.0_/node_modules/@mongosh/service-provider-node-driver/src/node-driver-service-provider.ts:95:1)
      at Object.<anonymous> (../../node_modules/.pnpm/@mongosh+service-provider-node-driver@3.18.0_@aws-sdk+credential-providers@3.969.0_mongodb-log-writer@2.5.4_bson@7.0.0_/node_modules/@mongosh/service-provider-node-driver/src/index.ts:1:1)

      ReferenceError: You are trying to `import` a file after the Jest environment has been torn down. From src/config/env/base.test.ts.
      ```

      There's various hacky workarounds that lead to other issues (jest mocking not working, etc) and core features like mocking are not supported in ESM mode.

      Vitest provides a drop-in replacement for jest with first class ESM support, including mocking.

      Adopting it would make integration testing easier and ease the transition for future ESM services.

      API for coverage etc is essentially drop in same from CNCP services end it should hopefully be easy to adopt as well but this requires additional investigation
      Created with Jira Integration+ for gagik.amaryan (gagik.amaryan@mongodb.com)

            Assignee:
            Gagik Amaryan
            Reporter:
            Gagik Amaryan
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: