【问题标题】:Exported function is undefined导出的函数未定义
【发布时间】:2019-08-11 01:27:48
【问题描述】:

我在 Typescript 中的 Mocha 测试遇到问题,我担心它与 Babel 有关,但我真的不确定发生了什么。

基本上,我有一个正在导出到文件中的函数

// src/my-ts-lib/tests/components/factoryMocks/componentConfigMocks.ts

...

export function getRandomConfig(type?: string): ComponentConfig {
  const randomComponentType = type || randomType();
  return {
    type: randomComponentType,
    config: configLibrary[randomComponentType]
  }
}

并被导入另一个,正在被测试调用:

// src/my-ts-lib/tests/components/RouteComponent/mocks/routeMocks.ts

...

import { getRandomConfig } from '../../factoryMocks/componentConfigMocks';

..

export const getSingleRouteConfigMock = (componentType?: string): RouteProps => {
  const defaultComponentType = 'PageLayout';
  return {
    childComponent: {
      type: componentType || defaultComponentType,
      config: getRandomConfig(componentType || defaultComponentType)
    },
    pageName: randomString(),
    path: randomString(),
  };
};

...

运行测试时,我收到以下错误:

RouteCompnent/mocks/routeMocks.ts:10
      config: getRandomConfig(componentType || defaultComponentType),
                                           ^
TypeError: componentConfigMocks_1.getRandomConfig is not a function
    at Object.exports.getSingleRouteConfigMock (/Users/.../routeMocks.ts:10:44)

如果我评论电话和console.log(getRandomConfig),我可以看到它是undefined。我不知道为什么会这样。更奇怪的是,在后续调用getSingleRouteConfigMock的测试中,同样的console.log正确地输出了函数,意味着它已经被导出了。

我摆弄过 Babel、Mocha 和 Typescript 配置,但没有成功。

这是 Babel 配置:

.babelrc

{
  "presets": ["@babel/preset-env", "@babel/preset-react"]
}

摩卡配置:

mocha.opts

--require ts-node/register
--watch-extensions ts tsx
--require source-map-support/register
--recursive
--require @babel/register
--require @babel/polyfill
src/**/*.spec.**

Typescript 配置:

tsconfig.json

{
   "compilerOptions": {
     "outDir": "./dist/",
     "sourceMap": true,
     "noImplicitAny": false,
     "module": "commonjs",
     "target": "es6",
     "jsx": "react"
   },
   "include": [
     "./src/**/*"
   ],
   "exclude": [
     "./src/**/*.spec.ts",
     "./src/my-ts-lib/components/**/*.spec.tsx",
     "./src/my-ts-lib/test-helpers/*"
   ],
 }

以及相关栏目package.json

...
"dependencies": {
  ...
  "@babel/polyfill": "7.2.x",
  ...
},
"devDependencies": {
  "@babel/core": "7.2.x",
  "@babel/preset-env": "7.2.x",
  "@babel/preset-react": "7.0.x",
  "@babel/register": "7.0.x",
  "babel-loader": "8.x",
  "mocha": "3.2.x",
  ...
}

【问题讨论】:

  • import { getRandomConfig } 你不是导出单个函数而不是对象吗?
  • 在同一个文件中导出了其他函数 (componentConfigMocks.ts)。如果我没记错的话,这 - import { foo } from ... - 是导入不是 default 的导出的方式,无论它是否是一个对象。

标签: javascript typescript mocha.js babeljs commonjs


【解决方案1】:

我发现我有一个循环依赖。这就是为什么这不起作用。

【讨论】:

  • 你能告诉我们更多关于循环依赖的信息吗?
【解决方案2】:

导致此症状的另一个可能原因是,当您在生产模式下使用 Parcel 之类的捆绑程序并删除未使用的项目时,模块中实际上缺少该功能(该特定问题在 Empty Javascript File When Building With Parcel JS 2 中讨论)。检查编译的模块文件,确保名称存在。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 2021-01-31
    • 1970-01-01
    • 2021-10-09
    • 1970-01-01
    • 1970-01-01
    • 2020-05-12
    相关资源
    最近更新 更多