【问题标题】:Not able to use .ts files with Jasmine and Karma. Getting error 'Unable to determine file type..'无法将 .ts 文件与 Jasmine 和 Karma 一起使用。出现错误“无法确定文件类型..”
【发布时间】:2020-10-05 05:32:50
【问题描述】:

我真的需要一些帮助来配置 karma.conf.js 以处理打字稿文件,或者不确定这里缺少什么或我做错了什么。对单元测试很陌生,学习了 Jasmine 框架(在示例中它使用 *.js 和 *.spec.js 文件),但在我的 ionic-angular 项目中,它的 *.ts 文件和出现错误(如下所示)。我一直试图弄清楚 2 天,但无法解决这个问题。非常感谢任何帮助。

[使用 ionic 3 (ionic-angular 3.9.+) 框架与 Angular 6 (6.1.10) 和 typescript (3.8.+.)]

我的 tsconfig.json >

 {
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [
      "dom",
      "es2015"
    ],
    "module": "es2015",
    "moduleResolution": "node",
    "sourceMap": true,
    "target": "es6"
  },
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules",
    "src/**/*.spec.ts",
    "src/**/__tests__/*.ts"
  ],
  "compileOnSave": false,
  "atom": {
    "rewriteTsconfig": false
  }
} 

我的 karma.conf.js >

module.exports = function (config) {
  config.set({
    frameworks: ['jasmine', 'jasmine-matchers'],
    files: [
      'src/**/*.ts',
      'src/**/*.spec.ts'
    ],
    plugins: [
      'karma-jasmine',
      'karma-jasmine-matchers',
      'karma-chrome-launcher'
    ],
    reporters: ['dots'],
    colors: true,
    browsers: ['ChromeHeadless'],
    singleRun: true
  })
};

我的 package.json devDependencies:

"devDependencies": {
    "@ionic/app-scripts": "3.2.4",
    "jasmine-core": "^3.5.0",
    "karma": "^5.1.0",
    "karma-chrome-launcher": "^3.1.0",
    "karma-jasmine": "^3.3.1",
    "karma-jasmine-matchers": "^4.0.2",
    "puppeteer": "^1.20.0",
    "ts-node": "^8.10.2",
    "typescript": "^3.8.3"
  },

我得到的错误:

15 06 2020 09:40:23.844:WARN [middleware:karma]: Unable to determine file type from the file extension, defaulting to js.
  To silence the warning specify a valid type for C:/Users/USR01/Documents/testing/src/providers/member/member.ts in the configuration file.
  See http://karma-runner.github.io/latest/config/files.html
........
........
15 06 2020 09:40:23.844:WARN [middleware:karma]: Unable to determine file type from the file extension, defaulting to js.
  To silence the warning specify a valid type for C:/Users/USR01/Documents/testing/src/providers/scores/scores.ts in the configuration file.
Chrome 83.0.4103.97 (Windows 10): Executed 0 of 0 SUCCESS (0.001 secs / 0 secs)
npm ERR! Test failed.  See above for more details.

【问题讨论】:

  • 你有什么发现吗?我也面临同样的问题。
  • @HimanshuSingh 还没有。但如果你这样做,请发表评论。

标签: angular typescript unit-testing ionic3 karma-jasmine


【解决方案1】:

这看起来很有希望。 https://www.npmjs.com/package/karma-typescript

添加预处理器以将 TypeScript 编译为常规 Javascript 后,将其放入 files: [] 数组中,告诉 Karma 这是一个 js 文件:

  files: [
    {
      pattern: 'src/**/*.ts',
      type: 'js'  // to silence the warning. Means load with <script> tag
    },
  ]

【讨论】:

  • 谢谢@joeforker。我将在此处检查并更新结果。
猜你喜欢
  • 2019-05-31
  • 2019-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-12
  • 2022-07-01
  • 1970-01-01
  • 2018-05-12
相关资源
最近更新 更多