为什么我的笑话设置不能与顶级等待一起使用

Mic*_*ael 5 javascript typescript jestjs babel-jest next.js

我有一个顶级等待来加载应用程序机密,如下所示:

// lib/config/secrets.ts
const secrets = await loadSecrets();
export default secrets;
Run Code Online (Sandbox Code Playgroud)

我将其导入其他模块中以进行数据库连接设置等:

// lib/db/index.ts
import { Pool } from "pg";
import secrets from "lib/config/secrets";

const pool = new Pool({
   connectionString: secrets.dbUrl,
   ...
});


Run Code Online (Sandbox Code Playgroud)

当通过 Webpack 运行我的应用程序时,这工作正常,但是当使用 jest 运行单元测试时,我收到以下错误: SyntaxError: await is only valid in async functions and the top level bodies of modules。我已经在这个问题上停留了一段时间,甚至考虑过其他途径来完全避免异步秘密加载,因为我还没有找到解决方案。我见过类似的问题Jest won't receive top-level-await with NodeJS16 & TypeScript,但我没有ts-jest在我的项目中使用。我的配置是否有任何明显的情况会导致测试期间出现此问题,或者这只是不受支持?

next.config.js通过 Webpack 支持顶级等待:

/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  webpack: (config) => {
    config.experiments.topLevelAwait = true;
    return config;
  },
};

module.exports = nextConfig;
Run Code Online (Sandbox Code Playgroud)

我的jest.config.js

const nextJest = require("next/jest");

const createJestConfig = nextJest({
  // Provide the path to your Next.js app to load next.config.js and .env files in your test environment
  dir: "./",
});

// Add any custom config to be passed to Jest
const customJestConfig = {
  moduleDirectories: ["node_modules", "<rootDir>"],
  modulePaths: ["<rootDir>/lib"],
  //   setupFilesAfterEnv: ["<rootDir>/jest.setup.js"],
  moduleNameMapper: {
    // Handle module aliases (this will be automatically configured for you soon)
    "^components/(.*)$": "<rootDir>/components/$1",
    "^lib/(.*)$": "<rootDir>/lib/$1",
    "^pages/(.*)$": "<rootDir>/pages/$1",
  },
  testEnvironment: "jest-environment-jsdom",
};

// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
module.exports = createJestConfig(customJestConfig);
Run Code Online (Sandbox Code Playgroud)

我的package.json

{
  "name": "app",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
     ...
  },
  "dependencies": {
    "@aws-sdk/client-secrets-manager": "^3.199.0",
    "@emotion/react": "^11.7.1",
    "@emotion/styled": "^11.6.0",
    "@mui/icons-material": "^5.3.1",
    "@mui/material": "^5.4.0",
    "install": "^0.13.0",
    "next": "^12.1.0",
    "next-auth": "^4.2.1",
    "npm": "^8.4.1",
    "pg": "^8.7.3",
    "react": "17.0.2",
    "react-dom": "17.0.2"
  },
  "devDependencies": {
    "@babel/core": "^7.17.5",
    "@babel/preset-env": "^7.16.11",
    "@babel/preset-typescript": "^7.16.7",
    "@types/aws-lambda": "^8.10.108",
    "@types/jest": "^27.4.1",
    "@types/node": "17.0.8",
    "@types/pg": "^8.6.5",
    "@types/react": "17.0.38",
    "@typescript-eslint/eslint-plugin": "^5.11.0",
    "@typescript-eslint/parser": "^5.11.0",
    "babel-jest": "^27.5.1",
    "esbuild": "^0.15.12",
    "eslint": "8.6.0",
    "eslint-config-next": "12.0.8",
    "eslint-config-prettier": "^8.5.0",
    "eslint-plugin-prettier": "^4.0.0",
    "eslint-plugin-simple-import-sort": "^7.0.0",
    "eslint-plugin-unused-imports": "^2.0.0",
    "jest": "^27.5.1",
    "prettier": "^2.6.2",
    "typescript": "4.5.4"
  }
}
Run Code Online (Sandbox Code Playgroud)

我的.babelrc

{
  "presets": ["next/babel"],
  "plugins": []
}
Run Code Online (Sandbox Code Playgroud)

我的tsconfig.json

{
  "compilerOptions": {
    "target": "esnext",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true,
    "baseUrl": ".",
    "paths": {
      "components": ["components/"],
      "lib": ["lib/"],
      "sql": ["sql/"],
      "styles": ["styles/"]
    }
  },
  "include": ["types/*.ts", "**/*.ts", "**/*.tsx"],
  "exclude": ["node_modules"]
}
Run Code Online (Sandbox Code Playgroud)
更新:

我尝试添加"type":"module"并将package.jsonjest.config 和 next.config 扩展更新为.cjs. 它仍然产生相同的错误:SyntaxError: await is only valid in async functions and the top level bodies of modules