1

I'm getting this error when trying to run a new test:

error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', or 'nodenext', and the 'target' option is set to 'es2017' or higher.

But server/tsconfig.json already has:

{
  "compilerOptions": {
    "target": "ES2022",
    "esModuleInterop": true,
    "module": "ES2022",
    "strictNullChecks": true,
    "allowSyntheticDefaultImports": true,
    "lib": ["ES2021.String"],
    "moduleResolution": "node",
    "composite": true /* Enable project compilation https://www.typescriptlang.org/docs/handbook/project-references.html */
  },
  "references": [
    {
      "path": "../src"
    },
    {
      "path": "../shared"
    }
  ]
}

My Jest test looks like:

test('something', async () => {
  await updateTransactions();
  // ...
}

and updateTransactions.ts looks like:

const connectionString = process.env.POSTGRESQL_CONNECTION_STRING;
const pgClient = new pg.Client({ connectionString });
await pgClient.connect();

export async function updateTransactions() {
   // ...
}

I've already looked at questions like Top-level ‘await’ expressions are only allowed when the ‘module’ option is set to ‘esnext' and How can I use async/await at the top level?

I know that I could refactor await pgClient.connect(); to be within a function, but for various reasons, I'm curious how to just enable top-level await instead.

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
Ryan
  • 22,332
  • 31
  • 176
  • 357
  • 2
    Because you showed a snippet from a test and said that it's a Jest test, I'm guessing this error occurs when running the test in Jest. Have you read [the documentation](https://jestjs.io/docs/ecmascript-modules) for running Jest in ESM mode? By default it runs in CJS mode where TLA is **not** supported, and ESM support is only experimental for Jest. If you need to run tests on ESM code, Jest is probably going to present some challenges. – jsejcksn Sep 28 '22 at 16:33
  • You might be able to get it to work by putting `preset: 'ts-jest/presets/default-esm'` and `transform: {'^.+\\.tsx?$': ['ts-jest', {tsconfig: '/server/tsconfig.json',useESM: true}]}` in your `jest.config.js` and call jest with `node --experimental-vm-modules node_modules/jest/bin/jest.js`. – Oblosys Sep 28 '22 at 16:58
  • I went ahead and refactored not to use a top-level await: https://github.com/NEARFoundation/tx-tracking-app/commit/2ddbd60564eb6b795fcd5e9c31fb842789f67158#diff-e32a1e570a3cff680a4e3b1335dc66cd816ab14bc9bff451d5f5d467e0278abeR74 I'd be curious though if someone wants to offer a commit that would make it work while still using a top-level await. – Ryan Sep 28 '22 at 19:26
  • Does this answer your question? [Jest won't accept top-level-awaits with NodeJS16 & TypeScript](https://stackoverflow.com/questions/70725063/jest-wont-accept-top-level-awaits-with-nodejs16-typescript) – mikemaccana Feb 21 '23 at 13:44
  • Did you try changing moduleResolution to nodenext? – morganney Feb 21 '23 at 13:44

0 Answers0