当我的代码在 jest 下运行时,为什么 pg_promise_1.default 未定义?

问题描述 投票:0回答:0

我正在开发一个用 Typescript 编写的 NestJS 服务。 它依赖于用于数据库访问的第一方 NPM 包,它又依赖于 pg-promise NPM 页面。 当使用“npm run start”(运行“nest start”)调用时,该服务运行得非常好。

但是在集成测试期间(“npm run test:integration”运行 jest),它崩溃了:

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

这是从 pgPromise 包中的构造函数中的一行代码抛出的:

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

在与构造函数相同的文件中有一些初始化代码,它创建了 pg_promise_1.default:

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

当通过“npm run start”运行时,这将返回一个包含大量属性的对象,包括“默认”属性。当通过“npm run test:integration”运行时,它只返回 { __esModule: true },这显然没有“默认”属性。

(此服务也有一组单元测试,效果很好,并且超出了这个问题的范围 - 在这种情况下没有模拟,也不应该有。)

我已经在服务和第一方 NPM 包中尝试了 tsconfig.json 中“模块”和“目标”选项的各种设置,但一点运气都没有。 (我不认为包的配置应该改变,因为该服务在本地和生产环境中都运行良好。)

服务的 tsconfig.json:

{
  "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 用于集成测试:

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

第一方数据库模块的 tsconfig.json,它包装了 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"]
}

我还尝试在该文件中将 module=commonjs 更改为 module=ES2020,但集成测试仍然因相同的类型错误而失败。

typescript jestjs ts-jest pg-promise
© www.soinside.com 2019 - 2024. All rights reserved.