TypeScript Jest: Cannot use import statement outside module

avatar

Borislav Hadzhiev

Tue Mar 15 20222 min read

TypeScript Jest: Cannot use import statement outside module #

The TypeScript jest error "Cannot use import statement outside module" occurs when we have misconfigured 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 are getting 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.

Create a jest.config.js file in the root directory of your project. The file should look something like:

jest.config.js
module.exports = { preset: 'ts-jest', testEnvironment: 'node', transform: { '^.+\\.ts?$': 'ts-jest', }, transformIgnorePatterns: ['<rootDir>/node_modules/'], };
Note that this is a JavaScript file and goes 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.

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

Jest by default 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 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" } }

Make sure to install ts-jest as it takes care of transforming your TypeScript test files to JavaScript before running them.

Now try using an import statement in your test files. Here is a file called example.test.ts that's located in the src/ directory.

src/example.test.ts
import { sum } from './another-file'; describe('works', () => { it('returns expected value', () => { expect(sum(10, 10)).toBe(20); }); });

The file imports a function from another file in the same directory.

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

import jest typescript success

The TypeScript jest error "Cannot use import statement outside module" occurs when jest is misconfigured in a TypeScript project. To solve the error, you have to make sure that your TypeScript files are compiled to JavaScript before they are ran, because jest on its own cannot understand TypeScript.

If you are getting 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.

Use the search field on my Home Page to filter through my more than 1,000 articles.