typescript 抛出 configure not a function error with dotenv and jest

typescript throws configure not a function error with dotenv and jest

我正在尝试同时使用 dotenv 和 jest,运行 立即出错。

一个测试文件,tests/authenticationt.test.ts只有

require('dotenv').configure();

足以导致此错误(无论是否存在测试都会抛出此错误):

PS C:\Users\rj\code\BGA.ts> npm run test
Debugger attached.

> @ test C:\Users\rj\code\BGA.ts
> jest

Debugger attached.
 FAIL  tests/authentication.test.ts
  ● Test suite failed to run

    TypeError: require(...).configure is not a function

    > 1 | require('dotenv').configure();
        |                               ^
      2 |

      at Object.<anonymous> (tests/authentication.test.ts:1:31)
      at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)

jest.config.js

module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  modulePaths: [
    "."
  ],
};

package.json

{
  "scripts": {
    "test": "jest"
  },
  "devDependencies": {
    "@types/jest": "^26.0.23",
    "dotenv": "^10.0.0",
    "jest": "^27.0.4",
    "ts-jest": "^27.0.3",
    "typescript": "^4.3.2"
  },
  "dependencies": {
    "@types/puppeteer": "^5.4.3"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "es2017",
    "module": "commonjs",
    "strictNullChecks": true,
    "outDir": "build",
    "lib": [      
      "es2017",
      "DOM"
    ],
  },
  "include": ["src/**/*"]
}

其他详细信息

我没能找到与此错误匹配的 Whosebug 问题;但是,这是一个基本的设置问题。这是在 Windows 10 机器上,我通过 powershell 触发测试。 dotenv 与我的其他打字稿文件配合得很好(即这只是玩笑的问题)。

尝试require('dotenv').config()