【问题标题】:Typescript `declare global` preventing Jest test suites from runningTypescript `declare global` 阻止 Jest 测试套件运行
【发布时间】:2018-01-03 19:40:10
【问题描述】:

我使用 react、redux 和 jest 的应用程序有一个 typescript 文件,其中包含以下代码:

declare global {
  interface Window { eventSource: any;}

  class EventSource {
    errorers: any;
    onerror: any;
    onmessage: () => void;

    addEventListener(event: string, cb: () => void): void;

    constructor(name: string);
  }
}

我正在使用 jest 测试框架在应用程序中使用 es6 javascript 的其他区域运行测试,它们运行良好。不幸的是,有两个测试套件由于以下错误而被阻止运行,另一个就像它一样:

/Users/blyncsy/Documents/blyncsyu/client/app/bundles/analytic/data-hub.ts:3
declare global {
        ^^^^^^
SyntaxError: Unexpected identifier

  at transformAndBuildScript (node_modules/jest-runtime/build/transform.js:321:12)
  at Object.<anonymous> (app/bundles/Pulse/actions/odAnalyzerActions.js:2:16)
  at Object.<anonymous> (app/bundles/Pulse/containers/od-analyzer/ODLineRenderer.js:6:26)

如果上面列出的一个被注释掉,那么同一个打字稿文件的其他部分会搞砸测试运行器。此文件在第一个测试套件中正在测试的操作文件中被引用,并且来自该文件的操作正在被第二个套件测试的文件中引用。我假设在测试遇到这个 .ts 文件之前需要做一些事情来预编译它,但我无法弄清楚如何去做。这是 webpack 配置:

/* eslint comma-dangle: ["error",
 {"functions": "never", "arrays": "only-multiline", "objects":
 "only-multiline"} ] */

const webpack = require('webpack');
const pathLib = require('path');

const devBuild = process.env.NODE_ENV !== 'production';

const config = {
  entry: [
    'es5-shim/es5-shim',
    'es5-shim/es5-sham',
    'babel-polyfill',
    './app/bundles/analytic',
    './app/bundles/Pulse/startup/registration',
  ],
  output: {
    filename: 'webpack-bundle.js',
    path: pathLib.resolve(__dirname, '../app/assets/webpack'),
  },
  devtool: "source-map",
  resolve: {
    extensions: [".ts", ".tsx", '.js', '.jsx'],
  },
  plugins: [
    new webpack.EnvironmentPlugin({ NODE_ENV: 'development' }),
  ],
  module: {
    rules: [
      {
        test: /travel-info-type.ts/,
        use: [{
          loader: 'expose-loader',
          options: 'TravelInfoType'
        }]
      },
      {
        test: /heatmap-getter.ts/,
        use: [{
          loader: 'expose-loader',
          options: 'HeatmapGetter'
        }]
      },
      {
        test: /data-hub.ts/,
        use: [{
          loader: 'expose-loader',
          options: 'DataHub'
        }]
      },
      {
        test: require.resolve('react'),
        use: {
          loader: 'imports-loader',
          options: {
            shim: 'es5-shim/es5-shim',
            sham: 'es5-shim/es5-sham',
          }
        },
      },
      {
        test: /\.jsx?$/,
        use: 'babel-loader',
        exclude: /node_modules/,
      },
      // All files with a '.ts' or '.tsx' extension will be handled by 'awesome-typescript-loader'.
      { test: /\.tsx?$/, loader: "awesome-typescript-loader" },

      // All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
      { enforce: "pre", test: /\.js$/, loader: "source-map-loader" },
      // Extract css files
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader'],
      },
    ],
  },
};

module.exports = config;

if (devBuild) {
  console.log('Webpack dev build for Rails'); // eslint-disable-line no-console
  module.exports.devtool = 'eval-source-map';
} else {
  console.log('Webpack production build for Rails'); // eslint-disable-line no-console
}

感谢任何帮助!

【问题讨论】:

    标签: typescript jestjs


    【解决方案1】:

    你的错误

    /Users/blyncsy/Documents/blyncsyu/client/app/bundles/analytic/data-hub.ts:3

    表示 Jest 正在尝试运行 .ts 文件。你需要使用ts-jest来转换这个TS文件。

    更多

    这在 TypeScript 文档中有记录:https://facebook.github.io/jest/docs/getting-started.html#using-typescript

    【讨论】:

    • 是的,试过了,还是一样的错误。
    • 事实上,按照链接文档中的说明安装,我得到另一个与 tsconfig.json 文件相关的错误:``` 尝试从 /Users/blyncsy/Documents 读取时发生了一些错误/blyncsyu/client/tsconfig.json: { "messageText": "在配置文件 'tsconfig.json' 中找不到输入。指定的 'include' 路径是 '[\"./src/**/*\"]'和 'exclude' 路径是 '[]'。", "category": 1, "code": 18003 }```
    猜你喜欢
    • 2018-10-14
    • 2022-01-05
    • 2019-10-21
    • 1970-01-01
    • 2020-11-25
    • 1970-01-01
    • 2020-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多