Jest: TS jest.config.ts error: SyntaxError: Unexpected token 'export'

Created on 19 Oct 2020  路  4Comments  路  Source: facebook/jest

馃悰 Bug Report

Using jest.config.ts and running jest I get the following error:

Error: Jest: Failed to parse the TypeScript config file /Users/rafael/dev/maintained/detect-it/jest.config.ts
  SyntaxError: Unexpected token 'export'

I have TypeScript setup for es modules, but maybe there is something else I'm missing, tsconfig.json here. I installed ts-node and copied the jest.config.ts from the docs.

To Reproduce

Create jest.config.ts:

import type {Config} from '@jest/types';

const config: Config.InitialOptions = {
  verbose: true,
};
export default config;

Run npx jest

Expected behavior

For jest.config.ts to parse and tests to run.

Link to repl or repo (highly encouraged)

Trying to convert the jest config from js to ts in this repo branch:
https://github.com/rafgraph/detect-it/tree/jest-ts-config

envinfo

  System:
    OS: macOS 10.15.7
    CPU: (8) x64 Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
  Binaries:
    Node: 12.18.3 - ~/.n/bin/node
    Yarn: 1.22.5 - ~/.n/bin/yarn
    npm: 6.14.8 - ~/.n/bin/npm
  npmPackages:
    jest: ^26.6.0 => 26.6.0 
Bug Report Needs Repro Needs Triage

All 4 comments

@Gamote any thoughts on this? (PR for supporting jest.config.ts)

@rafgraph Hmm, looks like the tsconfig.json is interfering with the internal compiler's settings. I will have a look in detail later or tomorrow.

@Gamote I am also having the same issue on a brand new project. Thanks for looking into this!

image

import type { Config } from '@jest/types';

const config: Config.InitialOptions = {
  moduleFileExtensions: ['ts', 'tsx', 'js'],
  testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|js?|tsx?|ts?)$',
  globals: {
    NODE_ENV: 'test'
  },
  snapshotSerializers: ['enzyme-to-json/serializer'],
  transform: {
    '^.+\\.(j|t)sx?$': 'babel-jest'
  },
  coveragePathIgnorePatterns: [
    '/node_modules/',
    'jest.setup.ts',
    '<rootDir>/configs/',
    'jest.config.ts',
    '.json',
    '.snap'
  ],
  setupFiles: ['<rootDir>/jest.setup.ts'],
  coverageReporters: ['json', 'lcov', 'text', 'text-summary'],
  moduleNameMapper: {
    '\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
      '<rootDir>/__mocks__/mocks.ts',
    '\\.(css|less|scss)$': '<rootDir>/__mocks__/mocks.ts'
  }
};

export default config;

Out in 26.6.1

Was this page helpful?
0 / 5 - 0 ratings

Related issues

benmonro picture benmonro  路  119Comments

seibelj picture seibelj  路  116Comments

vitalibozhko picture vitalibozhko  路  138Comments

iffy picture iffy  路  137Comments

udbhav picture udbhav  路  236Comments