Search code examples
typescriptunit-testingjestjsnomachine-nx

NX and Jest don't find all my tests that end with spec.tsx


Hi I am new with nx and I was kinda confuse with it. I have multiple tests in my projects but for some reason jest doesn't find any of it. They do have the extension spect.tsx and I wanted to know what part of the the code is use to make it able to search the them. Thank you :)


Solution

  • I had a similar problem and it was caused by the Nx Jest preset not including the .tsx extension for modules and ts-jest transformations. I fixed it by editing jest.config.js.

    Before:

    const nxPreset = require('@nrwl/jest/preset').default;
    
    module.exports = {
      ...nxPreset
    };
    

    After:

    const nxPreset = require('@nrwl/jest/preset').default;
    
    module.exports = {
      ...nxPreset,
      moduleFileExtensions: ['ts', 'tsx', 'js', 'mjs', 'html'],
      transform: {
        '^.+\\.(tsx?|js|html)$': [
          'ts-jest',
          {
            tsconfig: '<rootDir>/tsconfig.spec.json',
          },
        ],
      },
    };