kulshekhar / ts-jest

A Jest transformer with source map support that lets you use Jest to test projects written in TypeScript.
https://kulshekhar.github.io/ts-jest
MIT License
6.95k stars 452 forks source link

Unable to process file. Please make sure that `outDir` in your tsconfig is neither `''` or `'.'`. #2407

Closed volkanunsal closed 3 years ago

volkanunsal commented 3 years ago

🐛 Bug Report

I am getting this error when I configure ts-jest to process a ESM module with the .js extension.

    Unable to process '/Users/newuser/forks/aws-mobile-appsync-sdk-js/packages/aws-appsync/node_modules/@apollo/client/utilities/utilities.cjs.js'. Please make sure that `outDir` in your tsconfig is neither `''` or `'.'`.

      at compileFn (../../node_modules/ts-jest/dist/compiler/language-service.js:508:15)
      at Object.compile (../../node_modules/ts-jest/dist/compiler/instance.js:39:21)
      at TsJestTransformer.process (../../node_modules/ts-jest/dist/ts-jest-transformer.js:85:41)
      at ScriptTransformer.transformSource (../../node_modules/@jest/transform/build/ScriptTransformer.js:464:35)
      at ScriptTransformer._transformAndBuildScript (../../node_modules/@jest/transform/build/ScriptTransformer.js:569:40)
      at ScriptTransformer.transform (../../node_modules/@jest/transform/build/ScriptTransformer.js:607:25)

To Reproduce

Steps to reproduce the behavior:

This is my jest.config.js file

const esModules = ['@apollo/.*'].join('|');

module.exports = {
  transform: {
    '^.+\\.tsx?$': 'ts-jest',
    '^.+\\.jsx?$': 'ts-jest',
  },
  transformIgnorePatterns: [`/node_modules/(?!${esModules})`],
  testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$',
  collectCoverageFrom: ['src/**/*', '!src/vendor/**'],
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
  testEnvironment: 'node',
  preset: 'ts-jest/presets/js-with-ts',
  globals: {
    'ts-jest': {
      tsconfig: 'tsconfig.test.json',
    },
  },
};

And my tsconfig.test.json file:

{
  "compilerOptions": {
    "outDir": "lib",
    "rootDir": "src",
    "module": "commonjs",
    "target": "es5",
    "moduleResolution": "node",
    "declaration": true,
    "lib": ["dom", "es6",  "esnext.asynciterable", "es2017.object"],
    "skipLibCheck": true,
   "allowJs": true
  },
  "exclude": ["lib", "__tests__"],
  "compileOnSave": true
}

Expected behavior

I expect the file to compile.

Here is the repo. Use yarn to install the dependencies and run the tests under packages/aws-appsync

https://github.com/volkanunsal/aws-mobile-appsync-sdk-js

envinfo

System:
    OS: Mac
    Node version: 15.4

Npm packages:
    jest: 26.6.3
    ts-jest: 26.5
    typescript: 4.2.2
    babel(optional): 7.13.1

TS_JEST_LOG:

It gets cut off. So I'm copying the last line, which seems to be the most relevant.

{"context":{"fileName":"/Users/newuser/forks/aws-mobile-appsync-sdk-js/packages/aws-appsync/node_modules/@apollo/client/utilities/utilities.cjs.js","logLevel":20,"namespace":"ts-compiler","package":"ts-jest","version":"26.5.2"},"message":"compileFn(): computing diagnostics using language service","sequence":2142,"time":"2021-02-27T20:34:09.856Z"}
ahnpnl commented 3 years ago

The workaround for your issue is adding to transformIgnorePatterns something like this

transformIgnorePatterns: [`/node_modules/(?!${esModules})`, '/node_modules/@apollo/client/utilities/utilities.cjs.js'],

Since utilities.cjs.js is already in CommonJS format so it doesn't have to be processed by ts-jest anymore. The same to other files .cjs.js