gpt4 book ai didi

typescript - 无法使用 Jest 中的 openlayers 模块测试任何代码

转载 作者:行者123 更新时间:2023-12-03 23:53:30 29 4
gpt4 key购买 nike

我正在尝试为碰巧导入一个或两个 openlayers 模块的某些模块编写一些测试。但是正如其他一些人发现的那样( hereherehere ),这不是开箱即用的。这是我尝试过的:

  • 重命名 .babelrcbabel.config.js并导出配置
  • 已添加 transformIgnorePatterns给我的jest.config.js

  • 我现在不知道该怎么解决这个问题。

    我正在使用:
  • 非 CRA webpack 配置
  • Jest v23.6.0
  • 通天塔核心 6.26.3
  • typescript 3.1.3
  • ts-jest 22.4.6

  • 这是我的配置:

    Jest :
    module.exports = {
    setupFiles: [
    "./testConfig/test-shim.js",
    "./testConfig/test-setup.js"
    ],
    transform: {
    "^.+\\.tsx?$": "ts-jest"
    },
    transformIgnorePatterns: [
    "/node_modules/(?!(ol)/).*/",
    "node_modules/(?!(ol)/)",
    ],
    testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx|tsx?)$",
    moduleNameMapper: {
    "^(Controllers|Api|Utilities)/(.*)$": "<rootDir>Scripts/$1/$2"
    },
    moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
    coverageReporters: ["text", "text-summary", "html"],
    coverageDirectory: "testConfig/coverageReport",
    collectCoverageFrom: ["**/Scripts/{App,Controllers,Utilities,Localization,EntryPoints}/**/*.{ts,tsx}"],
    coverageThreshold: {
    global: {
    branches: 0,
    functions: 0,
    lines: 0,
    statements: 0
    }
    }
    };

    最佳答案

    终于搞定了问题在于该项目使用的是 TypeScript,并且像往常一样,这使事情变得比需要的复杂得多。

    由于需要编译 OL 源代码,并且这些文件是用 JavaScript 编写的,因此我需要在我的配置中添加另一个转换来处理这些文件。之后,它提示 Canvas ,所以我还必须安装一个 Canvas 模拟。

    所以我的配置的更改部分如下:

      setupFiles: [
    "./testConfig/test-shim.js",
    "jest-canvas-mock", // <- the new mock
    "./testConfig/test-setup.js"
    ],
    transform: {
    "^.+\\.tsx?$": "ts-jest",
    "^.+\\.jsx?$": "babel-jest", // <- also compile js/x files
    },
    testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
    transformIgnorePatterns: [
    "node_modules/(?!(ol)/)", // <- exclude the OL lib
    ],

    希望这对其他人有帮助!

    关于typescript - 无法使用 Jest 中的 openlayers 模块测试任何代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53707492/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com