Jest 会在项目里以原始的 JavaScript 执行,所以如果你用了一些 Node 环境不支持的语法 (比如 JSX, TypeScript, Vue 模板语法),那就要把你的代码转译成原始的 JavaScript,这就跟你在构建浏览器前端代码时要做的转译工作一样。
Jest 提供 transform 配置来支持 Js 转译。
转译器(Transformer) 是一个能提供转译源代码能力的模块。 举个例子,假如你想在你的业务和测试代码中使用一些还没被 Node 支持的新语言特性,你可以引入一个代码预处理器来将新版本的 JavaScript 转译成当前支持的版本。
Jest将会缓存转换后的结果,并且试图让多方因素(比如正在转换的文件源和配置信息被修改等)造成的结果无效
默认值Jest ships with one transformer out of the box – babel-jest. 它会加载你项目的 Babel 配置,然后转译所有能正确匹配 /.[jt]sx?$/ 正则表达式的文件(也即所有 .js,.jsx,.ts 或 .tsx 文件)。 此外,babel-jest还将会注入 ES Module mocking中所提到的Babel插件。
提示记住,如果你想将它和其他代码预处理器一起使用,那必须要显式地引入默认的 babel-jest 转译器,
"transform": { "\.[jt]sx?$": "babel-jest", "\.css$": "some-css-transformer",}编写自定义Transformers你可以编写专属的Transformer, Transformer的API如下所示:
interface TransformOptions { supportsDynamicImport: boolean; supportsExportNamespaceFrom: boolean; /** * The value is: * - `false` if Jest runs without Node ESM flag `--experimental-vm-modules` * - `true` if the file extension is defined in [extensionsToTreatAsEsm](Configuration.md#extensionstotreatasesm-arraystring) * and Jest runs with Node ESM flag `--experimental-vm-modules` * * See more at https://jestjs.io/docs/next/ecmascript-modules */ supportsStaticESM: boolean; supportsTopLevelAwait: boolean; instrument: boolean; /** Cached file system which is used by `jest-runtime` to improve performance. */ cacheFS: Map; /** Jest configuration of currently running project. */ config: ProjectConfig; /** Stringified version of the `config` - useful in cache busting. */ configString: string; /** Transformer configuration passed through `transform` option by the user. */ transformerConfig: TransformerConfig;}type TransformedSource = { code: string; map?: RawSourceMap | string | null;};interface SyncTransformer { canInstrument?: boolean; getCacheKey?: ( sourceText: string, sourcePath: string, options: TransformOptions, ) => string; getCacheKeyAsync?: ( sourceText: string, sourcePath: string, options: TransformOptions, ) => Promise; process: ( sourceText: string, sourcePath: string, options: TransformOptions, ) => TransformedSource; processAsync?: ( sourceText: string, sourcePath: string, options: TransformOptions, ) => Promise;}interface AsyncTransformer { canInstrument?: boolean; getCacheKey?: ( sourceText: string, sourcePath: string, options: TransformOptions, ) => string; getCacheKeyAsync?: ( sourceText: string, sourcePath: string, options: TransformOptions, ) => Promise; process?: ( sourceText: string, sourcePath: string, options: TransformOptions, ) => TransformedSource; processAsync: ( sourceText: string, sourcePath: string, options: TransformOptions, ) => Promise;}type Transformer = | SyncTransformer | AsyncTransformer;type TransformerCreator = (transformerConfig?: TransformerConfig) => X;type TransformerFactory = { createTransformer: TransformerCreator;};备注为了简洁起见,上面某些类型没有完全列出。 Full code can be found in Jest repo on GitHub (remember to choose the right tag/commit for your version of Jest).
使用 Jest 时,有几种引入模块的方式 - 使用 Common JS (require) 或者 ECMAScript Modules (import -静态和动态引入) Jest 会按需把文件传给转译器 (比如,当检测到 require 或 import 的时候) 这个过程也称为 "转译",可