Why is pg_promise_1.default undefined when my code runs under jest?

79 Views Asked by At

I am working on a NestJS service, written in Typescript. It depends on a first-party NPM package for database access, which depends on the pg-promise NPM page. The service runs very nicely when invoked with "npm run start" (which runs "nest start").

But during integration testing (“npm run test:integration” which runs jest), it crashes with:

TypeError: (0 , pg_promise_1.default) is not a function

That is thrown from a line of code in a constructor in the pgPromise package:

this.dbClient = (0, pg_promise_1.default)({ /* code omitted */ })(connectionString)

There’s some initialization code in the same file as the constructor, which creates pg_promise_1.default:

const pg_promise_1 = __importStar(require("pg-promise"));

When run via “npm run start” this returns an object with a ton of properties on it including the ‘default’ property. When run via “npm run test:integration” this returns only { __esModule: true } which obviously doesn’t have the ‘default’ property.

(This service also has a set of unit tests, which work well, and are out-of-scope for this question - there's no mocking in this scenario, nor should there be.)

I've tried various settings of the 'module' and 'target' options in tsconfig.json in both the service and the first-party NPM package, with no luck at all. (I don't think the package's configuration should change anyway, since the service runs just fine both locally and in production.)

tsconfig.json for the service:

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2020",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": false,
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "forceConsistentCasingInFileNames": false,
    "noFallthroughCasesInSwitch": false,
    "allowJs": true,
    "moduleResolution": "Node",
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "rootDir": "."
  },
  "include": ["./src/**/*", "rq"],
  "exclude": ["./scripts"]
}

jest.config.js for the integration tests:

module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  modulePaths: ['<rootDir>'],
  modulePathIgnorePatterns: ['./dist/'],
  moduleDirectories: ['node_modules', 'src'],
  testMatch: ['**/src/test/integration/*'],
  testTimeout: 30000,
}

tsconfig.json for the first-party database module, which wraps pg-promise:

{
  "compilerOptions": {
    "target": "es2020",                                  /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
    "lib": ["es2020"],                                   /* Specify a set of bundled library declaration files that describe the target runtime environment. */
    "module": "commonjs",                                /* Specify what module code is generated. */
    "rootDir": ".",                                      /* Specify the root folder within your source files. */
    "moduleResolution": "node",                          /* Specify how TypeScript looks up a file from a given module specifier. */
    "resolveJsonModule": true,                           /* Enable importing .json files. */
    "allowJs": true,                                     /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
    "declaration": true,                                 /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
    "sourceMap": true,                                   /* Create source map files for emitted JavaScript files. */
    "outDir": "dist",                                    /* Specify an output folder for all emitted files. */
    "esModuleInterop": true,                             /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
    "forceConsistentCasingInFileNames": true,            /* Ensure that casing is correct in imports. */
    "strict": true,                                      /* Enable all strict type-checking options. */
    "noImplicitAny": true,                               /* Enable error reporting for expressions and declarations with an implied 'any' type. */
    "skipLibCheck": true                                 /* Skip type checking all .d.ts files. */
  },
  "include": ["./**/*.ts", "./**/*.json"],
  "exclude": ["./test", "./coverage", "./coverage-int", "./dist"]
}

I also tried changing module=commonjs to module=ES2020 in that file, but the integration tests still fail with the same TypeError.

0

There are 0 best solutions below