17

Having trouble setting up jest projects. Project A has some additional configuration that it runs (setupJest, creates some custom matchers, etc) that project B does not have. When I run jest I get this error for every single test it tries to run:

Test suite failed to run
File not found: <rootDir>/src/tsconfig.spec.json (resolved as: repo\src\tsconfig.spec.json)

There is no src folder in the repo root, only inside the individual project folders. I'm also not referencing src\tsconfig.spec.json anywhere in my setups. So I'm a bit confused at this point as to what to try. I've tried putting rootDir on everything and spelling out the paths, but nothing seems to work. Any suggestions or ideas or help would be REALLY welcome.

For reference, repo is a folder on a windows drive that holds the repo. e.g. c:\companyName\productName\

This is my structure

repo
  |- jest.config.js
  |- projects
       |- projectA
            |- jest.config.js
            |- tsconfig.spec.json
            |- src
                 |- setupJest.js
       |- projectB
            |- jest.config.js
            |- tsconfig.spec.json

root jest.config.js:

module.exports = {
    preset: 'jest-preset-angular',
    projects: [
        '<rootDir>/projects/projectA/jest.config.js',
        '<rootDir>/projects/projectB/jest.config.js'
    ]
    reporters: ['jest-silent-reporter'],
};

projectA jest.config.js:

module.exports = {
    globals: {
        'ts-jest': {
            tsConfig: 'tsconfig.spec.json',
            stringifyContentPathRegex: '\\.html$',
            astTransformers: ['jest-preset-angular/InlineHtmlStripStylesTransformer'],
            diagnostics: false,
        },
    },
    setupFilesAfterEnv: ['src/setupJest.ts'],
};

projectB jest.config.js:

module.exports = {
    globals: {
    'ts-jest': {
        tsConfig: 'tsconfig.spec.json',
            stringifyContentPathRegex: '\\.html$',
            astTransformers: ['jest-preset-angular/InlineHtmlStripStylesTransformer'],
            diagnostics: false,
        },
    },
};

3 Answers 3

27

Okay, so after 4 days of intense digging through reported issues on github and a handful of youtube tutorials I could find, I muddled my way to getting this to work.

root jest.config.js

globals: {
    'ts-jest': {
        astTransformers: ['jest-preset-angular/InlineHtmlStripStylesTransformer'],
        diagnostics: false,
        stringifyContentPathRegex: '\\.html$',
    },
},
moduleDirectories: ['node_modules', './'],
modulePaths: ['node_modules', './'],
preset: 'ts-jest',
projects: ['projects/projectA', 'projects/projectB'],
reporters: ['jest-silent-reporter'],
testEnvironment: 'node',
transform: {
    '\\.ts$': ['ts-jest'],
    '\\.html$': ['ts-jest'],
},
verbose: true,

projectA/B jest.config.js

module.exports = {
globals: {
    'ts-jest': {
        astTransformers: ['jest-preset-angular/InlineHtmlStripStylesTransformer'],
        diagnostics: false,
        stringifyContentPathRegex: '\\.html$',
        tsConfig: '<rootDir>/projects/projectA/tsconfig.spec.json',
    },
},
displayName: 'projectA',
moduleDirectories: ['node_modules', './'],
modulePaths: ['node_modules', './'],
name: 'projectA',
rootDir: './../../',
testMatch: ['<rootDir>/projects/projectA/**/*.spec.ts'],
transform: {
    '\\.ts$': ['ts-jest'],
    '\\.html$': ['ts-jest'],
},
};

Turns out the really important bit is the "rootDir: './../../'" piece. The rest was somewhat specific to fixing the other errors that came up as I worked through them, but once I got the rootDir defined properly from the sub projects, it actually started seeing all of them properly. So that is the key. jest.config.js (regardless of where it is in the project) defines rootDir relative to itself. So when it got to a project folder and accessed the config it remapped the rootDir and got lost. Defining root from the sub projects as the same as the parent jest config fixes the problem. Jest's documentation isn't exactly clear on this point.

3
  • 14
    True hero. Came back to answer your own question. Most honor to you.
    – ontek
    Commented May 3, 2021 at 21:30
  • Can you share your git repo so I can follow your project structure and see the config files ? Commented Feb 3, 2023 at 19:27
  • @user3739018 Unfortunately I am unable to. It's behind a fairly intense NDA imposed by an investment partner. Sorry I'm just getting to this. Life got a bit wild for a bit.
    – mycroft16
    Commented Feb 20 at 7:55
2

While setting rootDir worked for me at the beginning, I encountered some errors and discovered in the documentation that we need set roots too:

While rootDir is mostly used as a token to be re-used in other configuration options, roots is used by the internals of Jest to locate test files and source files.

This applies also when searching for manual mocks for modules from node_modules (__mocks__ will need to live in one of the roots).

By default, roots has a single entry but there are cases where you may want to have multiple roots within one project, for example roots: ["/src/", "/tests/"].

source: https://jestjs.io/docs/configuration/#roots-arraystring

So I ended up with:

module.exports = {
  rootDir: '../..',
  roots: ['<rootDir>/packages/myPackage/src'],
};

in /packages/myPackage/jest.config.js

1

jest.config.json in every project.

module.exports = {
    preset: 'ts-jest',
    globals: {
        'ts-jest': {
            tsconfig: require.resolve('./tsconfig.jest.json'),
        },
    },
};

require.resolve will find the right path

1
  • Please add some explanation to your answer such that others can learn from it
    – Nico Haase
    Commented Jan 23, 2022 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.