Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Top level await expressions are only .... #4223

Open
vekexasia opened this issue Oct 27, 2023 · 6 comments
Open

[Bug]: Top level await expressions are only .... #4223

vekexasia opened this issue Oct 27, 2023 · 6 comments

Comments

@vekexasia
Copy link

vekexasia commented Oct 27, 2023

Version

29.1.1

Steps to reproduce

src/testasync.ts

await new Promise(() => 'a')
export const a = true

jest.config.cjs

/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {

  moduleNameMapper: {
    '(.+)\\.js$': '$1'
  },
  transform: {
    '^.+\\.ts$': ['ts-jest', {
      diagnostics: true,
      tsconfig: {
        module: 'nodenext',
        moduleResolution: 'bundler',
        target: 'es2017',
      },
      useESM: true,
      compiler: 'typescript',
    }]
  }
}

and a test file

import { a } from '../src/testasync'
describe('test', () => {
  it('should work', () => {
    expect(true).toBe(a)
  })
})
export {}

Expected behavior

test pass

Actual behavior

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.

on
await new Promise(() => 'a')

Debug log

ts-jest.log

Additional context

CodeSandbox

Environment

System:
    OS: Linux 5.15 Ubuntu 22.04.2 LTS 22.04.2 LTS (Jammy Jellyfish)
    CPU: (16) x64 AMD Ryzen 7 5800X3D 8-Core Processor
  Binaries:
    Node: 20.6.1 - ~/.nvm/versions/node/v20.6.1/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v20.6.1/bin/yarn
    npm: 9.8.1 - ~/.nvm/versions/node/v20.6.1/bin/npm
  npmPackages:
    jest: ^29.7.0 => 29.7.0
@uriva
Copy link

uriva commented Nov 5, 2023

I think I have the same problem, here's my setup: https://stackoverflow.com/questions/77407758/top-level-await-with-jest-and-typescript

@vekexasia
Copy link
Author

update on this?

@douglascayers
Copy link

Came across this problem myself tonight while trying to migrate a commonjs project to ESM.

No matter how I change my tsconfig.json target, module, and moduleResolution values to, the error always comes up. 😢

@alex-page
Copy link

I managed to fix this issue by setting up my jest config with useESM https://kulshekhar.github.io/ts-jest/docs/getting-started/options/useESM

@midnqp
Copy link

midnqp commented Apr 3, 2024

@alex-page Can you please share your entire jest config file?

I tried useESM but couldn't get it to work. My tsconfig contains { "module": "esnext", "target": "nodenext"}

My jest config is as follows.

{
  moduleNameMapper: {
    '^(\\.{1,2}/.*)\\.js$': '$1',
    '^@/(.*)\\.js$': '<rootDir>/src/$1.ts',
  },

  transform: {
    '^.+\\.(ts|tsx)?$': [
      'ts-jest', 
      { 
        useESM: true
      }
    ], 
  },
  
  testMatch: [ '<rootDir>/tests/**/*.test.ts'],
}

@midnqp
Copy link

midnqp commented Apr 3, 2024

Update 🎉 Adding the following line fixes this bug for me!

 extensionsToTreatAsEsm: [".ts"],

Ah, feels great. It's 3am now for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants