npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tongtian/jest-config-preset

v2.0.0

Published

jest 基础的可共享配置

Downloads

16

Readme

@tongtian/jest-config-preset

简介

jest 的基础、可共享配置。

安装

npm i --save-dev @tongtian/jest-config-preset

用法

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

约定

  • 业务代码在根目录的 src 文件下
  • 测试代码在根目录的 test 文件夹下。

常见问题

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

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

2.SyntaxError: Cannot use import statement outside a module

Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

Here's what you can do:
 • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/en/ecmascript-modules for how to enable it.
 • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
 • If you need a custom transformation specify a "transform" option in your config.
 • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

碰到此类错误,可以按照以下两步解决:

  • 将 @babel/preset-env 的 modules 选项删除或设置为 'auto'。
  • 测试时,将 babel 的 targets 设置为 {node:'current'}。注意,babel 7.13.0 开始,targets 作为了 babel 的顶级配置选项,低于 7.13.0 版本,targets 是 @babel/preset-env 的配置选项。
const isTestEnv = process.env.NODE_ENV === 'test';
module.exports = {
  targets: isTestEnv
    ? { node: 'current' }
    : {
        browsers: ['last 2 versions', '> 1%', 'ie >= 9'],
      },
  ...其他配置,
};

3.如何让 jest 使用单独的 babel 配置

// jest.transform.js
module.exports = require('babel-jest').createTransformer({
  presets: [...],
  plugins: [...]
});
const jestPreset = require('@tongtian/jest-config-preset');
// 修改jest 的 transform 选项
module.exports = {
  preset: '@tongtian/jest-config-preset',
  transform: {
    ...jestPreset.transform,
    '^.+\\.(js|jsx|ts|tsx)$': '<rootDir>/jest.transform.js',
  },
};