首页 > 解决方案 > Graphql:Typescript + Jest + Relay + Jest 似乎无法集成在一起

问题描述

我正在测试使用 Typescript 和 Relay 构建的 React 应用程序。当我尝试测试继电器供电组件时,出现以下错误:

 Unexpected invocation at runtime. Either the Babel transform was not set up, or it failed to identify this call site. Make sure it is being used verbatim as `graphql`.

我关注 .babelrc 文件:


{
  "presets": ["@babel/env", "@babel/react"],
  "plugins": ["relay",
    [
      "transform-relay-hot",
      {
        "schema": "./schema.graphql",
        "watchInterval": 2000,
        "verbose": true
      }
    ]
  ]
}


并遵循 Jest 配置:

module.exports = {
  roots: ["<rootDir>/src"],
  transform: {
    "^.+\\.tsx?$": "ts-jest",
  },
  testRegex               : "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
  moduleFileExtensions    : ["ts", "tsx", "js", "jsx", "json", "node"],
  transformIgnorePatterns : ["node_modules/(?!(date-fns))"],
  moduleNameMapper: {
      "^Apps(.*)$"      : "<rootDir>/src/apps$1",
      "^Pages(.*)$"     : "<rootDir>/src/pages$1",
      "^Components(.*)$": "<rootDir>/src/components$1",
      "^Logic(.*)$"     : "<rootDir>/src/logic$1",
      "^Theme(.*)$"     : "<rootDir>/src/theme$1",
      "^Config(.*)$"    : "<rootDir>/src/config$1",
      "^Public(.*)$"    : "<rootDir>/src/public$1",
  },
  globals: {
    'ts-jest': {
      tsConfig: "tsconfig.json",
      // diagnostics: false
    }
  },

  setupTestFrameworkScriptFile: "./src/test/test.setup.ts"
};



我在这里错过了什么吗?

标签: reactjstypescriptgraphqlrelayjsrelaymodern

解决方案


不确定这个问题是否仍然相关,但这是我所拥有的。

我使用ts-jest. 我花了几天的时间。基本上切换到babel-jest解决了这个问题。jest.config.js

module.exports = {
  ...
  transform: {
    "\\.[jt]sx?$": "babel-jest",
    ...
  },
  ...
  globals: { // if necessary
    'jest': { // don't forget to change `ts-jest` to `jest` here
      babelConfig: true
    }
  }
}

推荐阅读