首页 > 解决方案 > Jest 配置没有正确识别要测试的文件

问题描述

我有当前的文件夹结构(其中 test 在我的目录的根目录中):

test
    unit
        helper
            helper.js
        helper2
            helper2.js

我的jest.config.js外观如下(此配置文件位于我文件夹的根目录中):

module.exports = {
    testEnvironment: 'node',
    collectCoverage: true,
    collectCoverageFrom: ['**/*.{js,jsx}', '!**/node_modules/**'],
    coverageDirectory: './coverage',
    roots: ['<rootDir>/test'],
    coverageReporters: ['text'],
    rootDir: '.',
    reporters: ['default'],
};

当我运行命令时:jest --verbose我收到No tests found, existing with code 1. 我的笑话配置中有什么问题需要修改吗?

标签: javascriptunit-testingjestjs

解决方案


即使您修改了这个roots笑话,仍然有testMatch。默认情况下,它的值为

"testMatch": [
  "**/__tests__/**/*.[jt]s?(x)",
  "**/?(*.)+(spec|test).[tj]s?(x)"
],

因此您可以将其更改为:

"testMatch": [
  "**/*.[jt]s?(x)",
],

注意:通过运行jest --showConfig,您将对所有配置有一个很好的概述,并了解可能需要更改的其他内容


推荐阅读