返回

Jest配置剖析,逐个击破!

前端

1. Jest 配置文件定义位置

Jest 配置文件可以定义在项目根目录下的 jest.config.js 或 package.json 文件中。

2. 项目根目录 jest.config.js

// jest.config.js
module.exports = {
  // ... 其他配置项
};

3. package.json

{
  "name": "my-project",
  "version": "1.0.0",
  "scripts": {
    "test": "jest"
  },
  "jest": {
    // ... 其他配置项
  }
}

4. 命令行 + 脚本的运行模式

Jest 提供了两种运行模式:命令行模式和脚本运行模式。

5. 命令行模式

jest [options] [patterns]

例如:

jest --coverage

6. 脚本运行模式

在 package.json 文件中定义脚本:

{
  "scripts": {
    "test": "jest"
  }
}

然后在命令行中运行:

npm test

7. Jest 配置选项

Jest 提供了丰富的配置选项,可以满足各种测试需求。

8. 常用配置选项

  • rootDir: 项目根目录。
  • testRegex: 测试文件匹配正则表达式。
  • testPathIgnorePatterns: 忽略的测试文件匹配正则表达式。
  • moduleFileExtensions: 模块文件扩展名。
  • transform: 文件转换器。
  • coverageDirectory: 代码覆盖率报告输出目录。
  • collectCoverage: 是否收集代码覆盖率。
  • coverageThreshold: 代码覆盖率阈值。

9. 高级配置选项

  • globalSetup: 在所有测试用例执行前运行的函数。
  • globalTeardown: 在所有测试用例执行后运行的函数。
  • setupFiles: 在每个测试用例执行前运行的文件。
  • setupFilesAfterEnv: 在每个测试用例执行后运行的文件。
  • moduleNameMapper: 模块名称映射。
  • unmockedModulePathPatterns: 不被 mock 的模块路径匹配正则表达式。

10. 示例

// jest.config.js
module.exports = {
  rootDir: './',
  testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.[jt]sx?
// jest.config.js
module.exports = {
  rootDir: './',
  testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.[jt]sx?$',
  testPathIgnorePatterns: ['/node_modules/'],
  moduleFileExtensions: ['js', 'jsx', 'ts', 'tsx'],
  transform: {
    '^.+\\.jsx?$': 'babel-jest',
    '^.+\\.tsx?$': 'ts-jest'
  },
  coverageDirectory: './coverage/',
  collectCoverage: true,
  coverageThreshold: {
    global: {
      branches: 80,
      functions: 80,
      lines: 80,
      statements: 80
    }
  }
};
#x27;
, testPathIgnorePatterns: ['/node_modules/'], moduleFileExtensions: ['js', 'jsx', 'ts', 'tsx'], transform: { '^.+\\.jsx?
// jest.config.js
module.exports = {
  rootDir: './',
  testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.[jt]sx?$',
  testPathIgnorePatterns: ['/node_modules/'],
  moduleFileExtensions: ['js', 'jsx', 'ts', 'tsx'],
  transform: {
    '^.+\\.jsx?$': 'babel-jest',
    '^.+\\.tsx?$': 'ts-jest'
  },
  coverageDirectory: './coverage/',
  collectCoverage: true,
  coverageThreshold: {
    global: {
      branches: 80,
      functions: 80,
      lines: 80,
      statements: 80
    }
  }
};
#x27;
: 'babel-jest', '^.+\\.tsx?
// jest.config.js
module.exports = {
  rootDir: './',
  testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.[jt]sx?$',
  testPathIgnorePatterns: ['/node_modules/'],
  moduleFileExtensions: ['js', 'jsx', 'ts', 'tsx'],
  transform: {
    '^.+\\.jsx?$': 'babel-jest',
    '^.+\\.tsx?$': 'ts-jest'
  },
  coverageDirectory: './coverage/',
  collectCoverage: true,
  coverageThreshold: {
    global: {
      branches: 80,
      functions: 80,
      lines: 80,
      statements: 80
    }
  }
};
#x27;
: 'ts-jest' }, coverageDirectory: './coverage/', collectCoverage: true, coverageThreshold: { global: { branches: 80, functions: 80, lines: 80, statements: 80 } } };

11. 结语

Jest 是一个功能强大且易于使用的 JavaScript 测试框架,其丰富的配置选项可以满足各种测试需求。通过本文的学习,您已经对 Jest 的配置机制有了深入的了解。在实际项目中,您可以根据自己的需求灵活应用这些配置选项,编写出高质量、易于维护的测试代码。