TypeScript Jest: Cannot use import statement outside module

avatar
Borislav Hadzhiev

Last updated: Feb 28, 2024
3 min

banner

# TypeScript Jest: Cannot use import statement outside module

The TypeScript jest error "Cannot use import statement outside module" occurs when we misconfigure jest in a TypeScript project and run test files directly without compiling them to JavaScript first.

To solve the error, transform your test files with ts-jest before running them.

jest cannot use import statement outside module

If you got the error in TypeScript without using the Jest testing library, check out my TypeScript-specific article - Cannot use import statement outside a module in TypeScript.

# Creating a jest.config.js file

Create a jest.config.js file in the root directory of your project. The file should look similar to the following.

jest.config.js
module.exports = { preset: 'ts-jest', testEnvironment: 'node', transform: { '^.+\\.ts?$': 'ts-jest', }, transformIgnorePatterns: ['<rootDir>/node_modules/'], };
The code for this article is available on GitHub
This is a JavaScript file and should be added to your project's root directory. If you decide to place your jest config in package.json, you have to convert this to JSON by enclosing strings in double quotes.

The example configuration above should be located in the root directory of your project (where your package.json file is).

The transform option ensures that your TypeScript test files are transformed with ts-jest.

By default, jest looks for test files by matching files inside of a __tests__ directory or files with .test or .spec suffix, e.g. example.test.ts or example.spec.ts.

This is determined by the testRegex option.

jest-test-default-matches
├── __tests__ │ └── component.spec.js # test │ └── anything # test ├── package.json # not test ├── foo.test.js # test ├── bar.spec.jsx # test └── component.js # not test

Here is an example package.json file.

package.json
{ "name": "example", "version": "1.0.0", "main": "build/index.js", "scripts": { "test": "jest" }, "devDependencies": { "@types/jest": "^27.4.0", "@types/node": "^17.0.21", "jest": "^27.5.1", "nodemon": "^2.0.15", "ts-jest": "^27.1.3", "ts-node": "^10.7.0", "typescript": "^4.6.2" } }
The code for this article is available on GitHub
Make sure to install ts-jest as it takes care of transforming your TypeScript test files to JavaScript before running them.
shell
# with NPM npm i -D jest typescript npm i -D ts-jest @types/jest # with YARN yarn add --dev jest typescript yarn add --dev ts-jest @types/jest

install jest and ts in development mode

Now, try using an import statement in your test files.

Here is a file called example.test.ts located in the src/ directory.

src/example.test.ts
import { sum } from './another-file'; describe('works', () => { it('returns the expected value', () => { expect(sum(10, 10)).toBe(20); }); });
The code for this article is available on GitHub

The file imports a function from a different file located in the same directory.

Here is the example code for another-file.ts - a module located in the same directory as example.test.ts.

another-file.ts
export function sum(a: number, b: number) { return a + b; }

You should also ensure that your tsconfig.json file is configured correctly and includes your jest typings.

tsconfig.json
{ "compilerOptions": { // ... rest "types": ["node", "jest"], // ... rest } }

If I run my test, I can see that the error has been resolved and my test passes.

import jest typescript success

The code for this article is available on GitHub

The TypeScript jest error occurs when Jest is misconfigured in a TypeScript project.

To solve the error, make sure that your TypeScript files are compiled to JavaScript before they are run because jest on its own cannot understand TypeScript.

If you got the error in TypeScript without using the Jest testing library, check out my TypeScript-specific article - Cannot use import statement outside a module in TypeScript.

I've also written an article on how to solve the "Cannot find name 'it', 'describe' or 'expect' error in Jest".

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.