【问题标题】:Transpilation when using Jest with Typescript将 Jest 与 Typescript 一起使用时的转译
【发布时间】:2018-11-06 00:05:31
【问题描述】:

当我在 Typescript 项目中使用 Jest 启动测试套件时,出现以下编译错误:

类型错误: /PATH/instant.ts: 无法读取未定义的“某些”属性

在我的 package.json 中,我有这个 Jest 配置:

"jest": {
    "preset": "react-native",
    "transform": {
      "^.+\\.tsx?$": "ts-jest"
    },
    "testRegex": "(/src/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?)$",
    "moduleFileExtensions": [
      "ts",
      "tsx",
     "js"
    ],
    "collectCoverage": true,
    "coverageReporters": [
     "json",
     "html"
    ]
  }

Instant.ts:

export class Instant {

    constructor(private id: string,
                private name: string) {
    }

}

还有 Instant.js:

export class Instant {
    constructor(id, name) {
        this.id = id;
        this.name = name;
    }
}
//# sourceMappingURL=instant.js.map

【问题讨论】:

  • 我想我们需要看看instant.ts的一些代码
  • @Fenton 我更新了我的问题
  • 代码中没有some
  • 我的代码中没有“一些”

标签: typescript jestjs


【解决方案1】:

我分享了我自己的 Jest 配置,它对我有用:

{
    preset: 'react-native',
    rootDir: '..',
    setupFiles: [
        '<rootDir>/configuration/jest.setup.js'
    ],
    moduleFileExtensions: [
        'ts',
        'tsx',
        'js'
    ],
    transform: {
        '^.+\\.js$': '<rootDir>/node_modules/react-native/jest/preprocessor.js',
        '\\.(ts|tsx)$': 'ts-jest'
    },
    testMatch: [
        '<rootDir>/__tests__/**/?(*.)+(spec|test).ts?(x)'
    ],
    testPathIgnorePatterns: [
        '\\.snap$',
        '<rootDir>/node_modules/'
    ],
    collectCoverageFrom: [
        '<rootDir>/src/**/*.{ts,tsx}'
    ],
    coveragePathIgnorePatterns: [
        '<rootDir>/__tests__/'
    ],
    coverageDirectory: '<rootDir>/jest/coverage',
    coverageReporters: [
        'html',
        'text',
        'text-summary'
    ]
}

【讨论】:

    猜你喜欢
    • 2018-08-09
    • 1970-01-01
    • 2019-06-08
    • 2019-05-25
    • 1970-01-01
    • 1970-01-01
    • 2017-10-31
    • 2018-04-04
    • 2019-09-02
    相关资源
    最近更新 更多