Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jest builder fails if the config file is written in TypeScript #5394

Closed
tmercswims opened this issue Apr 19, 2021 · 4 comments · Fixed by #6920
Closed

Jest builder fails if the config file is written in TypeScript #5394

tmercswims opened this issue Apr 19, 2021 · 4 comments · Fixed by #6920
Assignees
Labels
outdated scope: testing tools Issues related to Cypress / Jest / Playwright / Vitest support in Nx type: feature

Comments

@tmercswims
Copy link

tmercswims commented Apr 19, 2021

Current Behavior

As of this pull request, released in Jest 26.6.0, Jest supports writing config files in TypeScript (e.g. jest.config.ts). However, when I attempt to do this in an Nx project, it does not work.

Pointing the @nrwl/jest:jest builder at a TypeScript Jest config results in an error which I think originates from TypeScript itself, complaining about how the config file is not inside a module:

$ nx run exchange-api:test
Cannot use import statement outside a module

Interestingly, if I bypass Nx altogether and invoke Jest directly, pointing to that same config file with zero other changes, it works as expected:

$ jest --config ./apps/exchange/api/jest.config.ts
# works...

So that, of course, leads me to believe that Nx is doing something incorrectly when it comes to passing the config on to Jest.

Expected Behavior

I expect that pointing Nx to my jest.config.ts should work.

Steps to Reproduce

The general idea is pretty straightforward: Point the @nrwl/jest:jest builder at a Jest config which is written in TypeScript. My particular setup looks something like this:

jest.config.ts:

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

const config: Config.InitialOptions = {
  displayName: 'exchange-api',
  preset: '@nrwl/jest/preset',
  globals: {
    'ts-jest': {
      tsConfig: '<rootDir>/tsconfig.spec.json',
    },
  },
  coverageDirectory: '<rootDir>/coverage/',
  coverageThreshold: {
    global: {
      functions: 99,
      branches: 97,
      lines: 99,
      statements: 0,
    },
  },
};

export = config;

tsconfig.spec.json:

{
  "extends": "./tsconfig.json",
  "include": [
    "./test/**/*.ts"
  ]
}

builder definition:

"test": {
  "builder": "@nrwl/jest:jest",
  "options": {
    "jestConfig": "apps/exchange/api/jest.config.ts",
    "passWithNoTests": true
  }
}

Failure Logs

$ nx run exchange-api:test
Cannot use import statement outside a module

>  NX   CLOUD  See run details at ...

Environment

>  NX  Report complete - copy this into the issue template

  Node : 14.16.0
  OS   : win32 x64
  yarn : 1.22.5

  nx : Not Found
  @nrwl/angular : Not Found
  @nrwl/cli : 11.4.0
  @nrwl/cypress : 11.4.0
  @nrwl/devkit : 11.4.0
  @nrwl/eslint-plugin-nx : 11.4.0
  @nrwl/express : Not Found
  @nrwl/jest : 11.4.0
  @nrwl/linter : 11.4.0
  @nrwl/nest : 11.4.0
  @nrwl/next : Not Found
  @nrwl/node : 11.4.0
  @nrwl/react : Not Found
  @nrwl/schematics : Not Found
  @nrwl/tao : 11.4.0
  @nrwl/web : Not Found
  @nrwl/workspace : 11.4.0
  typescript : 4.1.5
@vsavkin vsavkin added scope: testing tools Issues related to Cypress / Jest / Playwright / Vitest support in Nx type: feature and removed type: bug labels Apr 24, 2021
@mheob
Copy link

mheob commented Sep 2, 2021

I ran into the same problem today. Is there an implementation planned to use a typed configuration of Jest?

@FrozenPandaz
Copy link
Collaborator

Typescript config files will soon be supported by @nrwl/jest:jest 🎉

However, I would recommend holding off on changing them just yet because our automated migrations will not work.

ManojBahuguna pushed a commit to ManojBahuguna/nx that referenced this issue Sep 16, 2021
@demisx
Copy link

demisx commented Dec 17, 2021

Is there any update when the workspace root jest.config.ts and jest.config.ts will be generated and supported by Nx? v13.x?

@github-actions
Copy link

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated scope: testing tools Issues related to Cypress / Jest / Playwright / Vitest support in Nx type: feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants