🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

@darcytech/jest-preset-darcytech

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@darcytech/jest-preset-darcytech

jest 基础可共享配置

latest
npmnpm
Version
2.3.0
Version published
Maintainers
1
Created
Source

@darcytech/jest-preset-darcytech

简介

jest 的基础、可共享配置。

安装

npm i --save-dev @darcytech/jest-preset-darcytech jest

用法

//jest-config.js
module.exports = {
  preset: '@darcytech/jest-preset-darcytech',
};

个别配置介绍

  • collectCoverageFrom:指定了统计覆盖率信息时,应该从哪些文件收集信息。该配置将在项目目录下的 src 文件夹中收集测试覆盖率。
  • moduleFileExtensions:指定了一组模块文件的扩展名。如果你 require 模块的时候,没有指定文件扩展名,jest 会从左往右匹配文件。注意:如果你项目中使用的是 js,jsx,那么最好覆盖这个配置,将 js,jsx 放到数组的开头。
  • moduleNameMapper:这里指定了一些静态资源的 mock。
  • testMatch:这里指定tests为所有测试文件的根目录。
  • timers:这里指定 @sinonjs/fake-timers 作为 fake timers 的 实现
  • transform:指定 node_modules 文件中的所有代码不会被转换

常见问题

1.配置 jest 时,你觉得配置没问题,但是测试就是失败。

可能是缓存的问题。运行测试可以加上 --no-cache 。配置好 jest 后,记得将 --no-cache 去掉,因为这会影响测试性能。如果想要删除缓存,可以参考--clearCache

2. Mocking not working

内部使用了 @swc/jest ,mock api 时,需要将 mock 的代码写到另一个文件,并在测试文件顶部引入。

// mock.ts
import { jest } from '@jest/globals';

jest.mock('apis/taskAutoAssignApi');
jest.mock('apis/taskTemplateApi');

// a.test.ts
import 'mock';
// 其他代码

Keywords

jest

FAQs

Package last updated on 10 Dec 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts