【问题标题】:Jest throws ReferenceError when using Flow generic typesJest 在使用 Flow 泛型类型时抛出 ReferenceError
【发布时间】:2019-06-03 07:18:42
【问题描述】:

我有一个使用 Redux、Flow-typed 和 Jest 的 React 应用程序。为了创建我的减速器,我使用了自定义通用工厂createReducer<S> (initialState: S, actions: any): Reducer<S>

这样我就可以创建一些减速器:

const FooReducer = createReducer<FooState>(initialState, {
  'ACTION_ADD_USER': () => 'Foo',
})

我有以下测试用例:

// @flow

import { createReducer } from 'create-reducer'

type AppState = { name: string }

const initialState: AppState = { name: '' }

const NameReducer = createReducer<AppState>(initialState, {
  'NAME_CHANGE': (state, name) => ({ ...state, name })
})

it('should change the name', () => {
  const action = { type: 'NAME_CHANGE', name: 'bar' }

  expect(NameReducer({ name: 'foo' }, action)).toEqual({ name: 'bar' })
})

Jest 抛出以下错误:

FAIL temp.spec.jsx

  ● Test suite failed to run

    ReferenceError: AppState is not defined

       7 | const initialState: AppState = { name: '' }
       8 | 
    >  9 | const NameReducer = createReducer<AppState>(initialState, {
         |                                   ^
      10 |   'NAME_CHANGE': (state, name) => ({ ...state, name })
      11 | })
      12 | 

      at Object.AppState (temp.spec.jsx:9:35)

出于某种原因,Jest 无法找到我的类型定义,即使它是在那里声明的。我一直在彻底寻找解决方法,但到目前为止还没有任何运气。希望有人知道这里出了什么问题?

附录:

环境

babel-core        6.26.0
babel-jest        23.6.0
babel-loader      6.4.1
babel-preset-flow 6.23.0
flow-bin          0.89.0
flow-typed        2.2.0
jest              23.6.0

.babelrc(更新):

{
  "plugins": [
    "syntax-dynamic-import",
    "transform-runtime",
    "transform-object-rest-spread",
    "transform-class-properties",
    "transform-es2015-destructuring"
  ],
  "presets": [
    "babel-preset-flow",
    "es2015",
    "react",
    "stage-0"
  ],
  "env": {
    "start": {
      "presets": [
        "react-hmre"
      ]
    },
    "test": {
      "presets": [
        "es2015",
        "react",
        "stage-0"
      ]
    }
  }
}

jest.config.js

module.exports = {
  roots: ['<rootDir>/spec/javascripts'],
  moduleNameMapper: {
    'c3': '<rootDir>/__mocks__/c3.js',
    '\\.(css|less|sass|scss)$': '<rootDir>/spec/javascripts/__mocks__/styleMock.js',
    '\\.(gif|ttf|eot|svg)$': '<rootDir>/spec/javascripts/__mocks__/fileMock.js'
  },
  moduleFileExtensions: [
    'jsx',
    'js'
  ],
  setupFiles: [
    '<rootDir>/spec/javascripts/__mocks__/global-mocks.js'
  ],
  moduleDirectories: [
    'node_modules',
    'app/javascript/src'
  ],
  transform: {
    '^.+\\.jsx?$': './node_modules/babel-jest'
  },
  testURL: 'http://localhost',
  testRegex: '.*.spec.jsx',
  verbose: true
}

createReducer.js

function createReducer<S> (initialState: S, handlers: any): Reducer<S> {
  return function reducer (state = initialState, action) {
    if (handlers.hasOwnProperty(action.type)) {
      return handlers[action.type](state, action)
    } else {
      return state
    }
  }
}

【问题讨论】:

  • 我真的没有答案,但我觉得奇怪的是 Jest 根本不应该关心类型,对吧?这个想法是babel-jest 在执行 jest 之前删除流类型(babel-preset-flow)。另一方面,这不是语法错误。也许你可以分享你的 babel 配置或 repo 来重现?
  • 感谢@Herku!我添加了配置和我现在认为相关的其他一些模块。我当然没有使用此预设,所以问题一定存在。我仍在努力使其工作,但请随时将此作为答案!
  • 您是否尝试将babel-preset-flow 添加到预设中?它还必须是第一个运行的,不确定它们需要按什么顺序运行,但您应该能够弄清楚。你如何运行你的应用程序?
  • 我在我的预设之上添加了babel-preset-flow,但没有任何改变......它与 webpack 一起运行,但问题是,只有在运行 Jest 时它才会失败。 Webpack 编译得很好,应用程序运行良好。
  • 你有没有发现哪里出了问题?我也遇到了同样的问题。

标签: jestjs babeljs flowtype babel-jest flow-typed


【解决方案1】:

babel-preset-flow 被调用为flow 预设在.babelrc 文件中。这是更新版本:

{
  "plugins": [
    "syntax-dynamic-import",
    "transform-runtime",
    "transform-object-rest-spread",
    "transform-class-properties"
  ],
  "presets": [
    "flow",
    "es2015",
    "react",
    "stage-0"
  ],
  "env": {
    "start": {
      "presets": [
        "react-hmre"
      ]
    }
  }
}

默认和测试环境需要相同的配置(这里不需要 env/test 配置)。

transform-es2015-destructuring 插件已经包含在babel-preset-es2015 中。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-31
  • 1970-01-01
相关资源
最近更新 更多