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

big*_*ato 5 typescript reactjs 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
};
Run Code Online (Sandbox Code Playgroud)

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

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

...
login({
  metadataKey
})
Run Code Online (Sandbox Code Playgroud)

通过测试:

 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.
Run Code Online (Sandbox Code Playgroud)

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

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

...
backend.login({
  metadataKey
})
Run Code Online (Sandbox Code Playgroud)

失败测试:

 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(
Run Code Online (Sandbox Code Playgroud)

即使使用 3rd 方库,我也注意到了同样的问题。例如,我正在使用node-forge,但除非我更改,否则它会中断

import forge from "node-forge";
Run Code Online (Sandbox Code Playgroud)

到:

import { md } from "node-forge";
Run Code Online (Sandbox Code Playgroud)

我错过了某个地方的设置吗?这是我的 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"
    ]
  }
Run Code Online (Sandbox Code Playgroud)