【问题标题】:Using Jest with Typescript + preact使用 Jest 和 Typescript + preact
【发布时间】:2018-01-03 18:57:09
【问题描述】:

在测试中将 JSX 转换为 h() 的问题。所有配置文件都类似于 create-react-app,不包括 TypeScriptpreact 的更改

我通过 create-react-app my-app --script=react-scripts-ts 创建应用程序 - 用于 TypeScript 项目。然后弹出并将react更改为preact(不要使用preact-compat)。

为了迁移到preact,我将package.json 添加到babel.plugins section 新插件["babel-plugin-transform-react-jsx", { pragma: "h"}] - 用于将<JSX /> 转换为h(JSX) 函数调用,而不是默认的React.createElement(JSX)(迁移指南@987654321 @)。

这很好用。

但 test 有不同的转译<JSX /> 配置:默认转译为React.createElement(JSX)。在测试中,我出现错误ReferenceError: React is not defined at Object.<anonymous> (src/Linkify/Linkify.test.tsx:39:21)。如果我在测试和测试文件中手动将<JSX /> 更改为h(SomeComponent) - 它可以工作。

如何将<JSX /> 转译成h(JSX) 进行测试?

// typescriptTransform.js
// Copyright 2004-present Facebook. All Rights Reserved.

'use strict';

const fs = require('fs');
const crypto = require('crypto');
const tsc = require('typescript');
const tsconfigPath = require('app-root-path').resolve('/tsconfig.json');
const THIS_FILE = fs.readFileSync(__filename);

let compilerConfig = {
  module: tsc.ModuleKind.CommonJS,
  jsx: tsc.JsxEmit.React,
};

if (fs.existsSync(tsconfigPath)) {
  try {
    const tsconfig = tsc.readConfigFile(tsconfigPath).config;

    if (tsconfig && tsconfig.compilerOptions) {
      compilerConfig = tsconfig.compilerOptions;
    }
  } catch (e) {
    /* Do nothing - default is set */
  }
}

module.exports = {
  process(src, path, config, options) {
    if (path.endsWith('.ts') || path.endsWith('.tsx')) {
      let compilerOptions = compilerConfig;
      if (options.instrument) {
        // inline source with source map for remapping coverage
        compilerOptions = Object.assign({}, compilerConfig);
        delete compilerOptions.sourceMap;
        compilerOptions.inlineSourceMap = true;
        compilerOptions.inlineSources = true;
        // fix broken paths in coverage report if `.outDir` is set
        delete compilerOptions.outDir;
      }

      const tsTranspiled = tsc.transpileModule(src, {
        compilerOptions: compilerOptions,
        fileName: path,
      });
      return tsTranspiled.outputText;
    }
    return src;
  },
  getCacheKey(fileData, filePath, configStr, options) {
    return crypto
      .createHash('md5')
      .update(THIS_FILE)
      .update('\0', 'utf8')
      .update(fileData)
      .update('\0', 'utf8')
      .update(filePath)
      .update('\0', 'utf8')
      .update(configStr)
      .update('\0', 'utf8')
      .update(JSON.stringify(compilerConfig))
      .update('\0', 'utf8')
      .update(options.instrument ? 'instrument' : '')
      .digest('hex');
  },
};

测试样本:

import { h, render } from 'preact';
import Linkify from './Linkify';

it('renders without crashing', () => {
  const div = document.createElement('div');
  render(<Linkify children={'text'} />, div);
});

【问题讨论】:

  • 嗨@亚历山大!我正在努力在我的 Typescript + Preact 应用程序中设置 Jest。您能否分享任何工作示例或您的代码。

标签: typescript babeljs create-react-app jestjs preact


【解决方案1】:

我找到了解决办法。

我在babel.plugins 部分new plugin ["babel-plugin-transform-react-jsx", { pragma: "h"}] 中错了 - 它没有使用。真的这个编译指示使用从tsconfig.json - "jsxFactory": "h"

但是,该指令在 typescriptTransform.js 中不使用。

我正在扩展编译器选项

let compilerConfig = {
  module: tsc.ModuleKind.CommonJS,
  jsx: tsc.JsxEmit.React,
  jsxFactory: "h" // <-- duplicate option in jest transform config file
};

希望对你有用。

【讨论】:

    猜你喜欢
    • 2020-07-15
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    • 2020-02-04
    • 2022-09-29
    • 2023-04-01
    • 2017-02-25
    • 2019-05-20
    相关资源
    最近更新 更多