Jest config (TypeScript): Type '{ __PATH_PREFIX__: string; }' is not assignable to type 'string | string[]'

Viewed 91

I decided to write the Jest configuration for my Gatsby site using TypeScript with the ts-jest/prests package.

I'm getting a TypeScript error on the globals assignment in the code below:

import type { InitialOptionsTsJest } from 'ts-jest/dist/types'
import { jsWithBabel as tsjPreset } from 'ts-jest/presets'

const config: InitialOptionsTsJest = {
  transform: {
    ...tsjPreset.transform,
  },
  testEnvironment: 'node',
  moduleNameMapper: {
    '.+\\.(css|styl|less|sass|scss)$': `identity-obj-proxy`,
    '.+\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': `<rootDir>/__mocks__/file-mock.js`,
    testPathIgnorePatterns: [`node_modules`, `\\.cache`, `<rootDir>.*/public`],
    transformIgnorePatterns: [`node_modules/(?!(gatsby)/)`],
    globals: {
      __PATH_PREFIX__: '',
    },
    testURL: `http://localhost`,
    setupFiles: [`<rootDir>/loadershim.js`],
  },
}

export default config

The error:

Type '{ PATH_PREFIX: any; }' is not assignable to type 'string | string[]'. Object literal may only specify known properties, and 'PATH_PREFIX' does not exist in type 'string[]'.ts(2322) Config.d.ts(135, 9): The expected type comes from this index signature.

I'm new to TypeScript, so I'm not exactly sure how to rectify this. The globals property with __PATH_PREFIX__ is needed by Gatsby for testing certain components.

0 Answers
Related