【问题标题】:Jest Projects in a Monorepo unable to find config files in projectsMonorepo 中的 Jest 项目无法在项目中找到配置文件
【发布时间】:2020-06-06 09:32:29
【问题描述】:

在设置 Jest 项目时遇到问题。 项目 A 有一些它运行的额外配置(setupJest、创建一些自定义匹配器等),而项目 B 没有。当我运行 jest 时,它尝试运行的每个测试都会出现此错误:

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

repo 根目录中没有 src 文件夹,只有在各个项目文件夹中。我也没有在我的设置中的任何地方引用 src\tsconfig.spec.json 。所以我在这一点上有点困惑要尝试什么。我已经尝试将 rootDir 放在所有内容上并拼出路径,但似乎没有任何效果。非常欢迎任何建议、想法或帮助。

作为参考,repo 是 Windows 驱动器上保存 repo 的文件夹。例如c:\公司名称\产品名称\

这是我的结构

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

根 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,
        },
    },
};

【问题讨论】:

    标签: angular typescript jestjs monorepo


    【解决方案1】:

    好的,经过 4 天的密集挖掘 github 上报告的问题和我能找到的一些 youtube 教程,我搞砸了让这个工作的方式。

    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'],
    },
    };
    

    事实证明,真正重要的是“rootDir: './../../'”部分。其余的部分专门用于修复在我处理它们时出现的其他错误,但是一旦我从子项目中正确定义了 rootDir,它实际上开始正确地看到所有这些错误。所以这是关键。 jest.config.js(不管它在项目中的什么位置)定义相对于自身的 rootDir。因此,当它到达项目文件夹并访问配置时,它重新映射了 rootDir 并迷路了。将子项目中的 root 定义为与父 jest 配置相同可以解决问题。 Jest 的文档在这一点上并不完全清楚。

    【讨论】:

    • 真正的英雄。回来回答你自己的问题。最荣幸给你。
    【解决方案2】:

    每个项目中的jest.config.json。

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

    require.resolve 会找到正确的路径

    【讨论】:

    • 请在您的答案中添加一些解释,以便其他人可以从中学习
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-09
    • 2014-05-18
    • 2016-05-20
    • 2011-08-05
    • 1970-01-01
    • 2012-09-02
    • 2013-09-05
    相关资源
    最近更新 更多