React / Jest / Typescript:默认导出未定义

时间:2019-04-08 15:03:23

标签: reactjs typescript jestjs

src / services / backend.ts:

export const login = ({ metadataKey }) =>
  axiosInstance
    .get(`${API.DEFAULT_BROKER}${API.V1_LOGIN_PATH}/${metadataKey}`)
    .then(({ data }: any) => data);

export default {
  ...
  login
};

我有一个导入backend.ts并使用login函数的文件。当我使用import { login } from "../../services/backend";导入它时,它会起作用:

import { login } from "../../services/backend";

...
login({
  metadataKey
})

通过测试:

 PASS  src/redux/epics/authentication-epic.test.ts
  ✓ authenticationEpic loginEpic (9ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        1.607s, estimated 2s
Ran all test suites matching /src\/redux\/epics\/authentication-epic.test.ts/i.

但是,当我使用默认导出导入它时,它始终是未定义的:

import backend from "../../services/backend";

...
backend.login({
  metadataKey
})

失败测试:

 FAIL  src/redux/epics/authentication-epic.test.ts
  ✕ authenticationEpic loginEpic (37ms)

  ● authenticationEpic loginEpic

    TypeError: Cannot read property 'login' of undefined

      22 |
      23 |       return from(
    > 24 |         backend.login({
         |                 ^
      25 |           metadataKey
      26 |         })
      27 |       ).pipe(

即使使用第三方库,我也注意到了同样的问题。例如,我使用的是node-forge,但是除非我进行更改,否则它会崩溃

import forge from "node-forge";

收件人:

import { md } from "node-forge";

我在某处缺少设置吗?这是我package.json的笑话部分:

  "jest": {
    "collectCoverageFrom": [
      "src/**/*.{js,jsx,mjs}"
    ],
    "setupFiles": [
      "<rootDir>/config/polyfills.js"
    ],
    "testMatch": [
      "<rootDir>/src/**/*.{js,jsx,mjs,ts}",
      "<rootDir>/src/**/?(*.)(spec|test).{js,jsx,mjs,ts}"
    ],
    "globals": {
      "window": true
    },
    "testEnvironment": "jsdom",
    "testURL": "http://localhost",
    "transform": {
      "^.+\\.jsx?$": "<rootDir>/node_modules/babel-jest",
      "^.+\\.tsx?$": "ts-jest",
      "^.+\\.css$": "<rootDir>/config/jest/cssTransform.js",
      "^(?!.*\\.(js|jsx|mjs|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
    },
    "transformIgnorePatterns": [
      "[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs)$"
    ],
    "moduleNameMapper": {
      "^react-native$": "react-native-web"
    },
    "moduleFileExtensions": [
      "web.js",
      "js",
      "json",
      "web.jsx",
      "jsx",
      "node",
      "mjs",
      "ts",
      "tsx"
    ]
  }

0 个答案:

没有答案