Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest Projects in a Monorepo unable to find config files in projects

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,
        },
    },
};
like image 893
mycroft16 Avatar asked Feb 21 '20 23:02

mycroft16


1 Answers

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.

like image 168
mycroft16 Avatar answered Oct 09 '22 18:10

mycroft16