Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Karma, Typescript definition file not loading

I am working on setting up development environment with karma, webpack and typescript, But I am having an issue with karma not applying custom definition file on tests.

This is my project file structure:

// file structure
project/
    config/
        karma.conf.js
    tests/
        test-files.ts 
        ...        
    src/
        tsdefinitions.d.ts
        other-ts-files.ts
        ...
    tsconfig.json

And here is karma config part related to webpack and typescript

webpack: {
  devtool: 'eval-source-map',
  resolve: {
    extensions: ['.webpack.js', '.web.js', '.ts', '.tsx', '.js']
  },
  module: {
    loaders: [{
      test: /\.tsx?$/,
      loader: 'awesome-typescript-loader'
    }]
  }
},
webpackMiddleware: {stats: 'errors-only'},
mime: {'text/x-typescript': ['ts','tsx']},
basePath: '../',
files: [{
  pattern: './test/**/*.ts',
  watched: false
}],
preprocessors: {
  './test/**/*.ts': ['webpack', 'sourcemap']
}

And lastly tsconfig.json

{
  "compilerOptions": {
    "removeComments": true,
    "allowSyntheticDefaultImports": true,
    "baseUrl": "./",
    "sourceMap": true,
    "noImplicitAny": true,
    "allowJs": true,
    "module": "commonjs",
    "target": "es2015",
    "paths": {
      "jquery": ["node_modules/jquery/dist/jquery"]
    },
    "exclude": ["node_modules"],
    "files": [
      "src/**/*.ts"
    ]
  },
  "awesomeTypescriptLoaderOptions": {
    "useCache": false,
    "useBabel": true,
    "babelOptions": {
      "presets": [
        "es2015",
        "stage-0"
      ]
    }
  }
}

I am running karma test like this

./node_modules/.bin/karma start ./config/karma.conf.js

Now when I am just building project files using karma everything is ok, they are built with no errors.

But when I am running test build there's a lot of errors of missing Window interface properties that are declared inside tsdefinitions.d.ts file.

like image 496
antanas_sepikas Avatar asked Aug 26 '17 13:08

antanas_sepikas


1 Answers

I've been able to accomplish this by having my types in a separate folder from my source file, and including that in the tsconfig.json using the typeRoots field.

My tsconfig.json:

{
  "compilerOptions": {
    ...
    "typeRoots": [
      "./@types"
    ]
  },
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "src/**/*.test.ts"
  ]
}

karma.config.js:

const webpackConfig = require("./webpack.test");

module.exports = function(config) {
  config.set({
    basePath: "",
    frameworks: ["mocha", "chai"],
    plugins: [
      "karma-chai",
      "karma-chrome-launcher",
      "karma-mocha",
      "karma-mocha-reporter",
      "karma-sourcemap-loader",
      "karma-webpack"
    ],
    files: [
      "src/**/*.test.ts"
    ],
    exclude: [],
    webpack: webpackConfig,
    preprocessors: {
      "**/*.ts": ["webpack", "sourcemap"]
    },
    mime: { "text/x-typescript": ["ts", "tsx"] },
   ...
  });
};

Now I can define types for modules which don't have any types, like this:
@types/universal-url/index.d.ts:

declare module 'universal-url' {
  export {URL, URLSearchParams} from 'whatwg-url' 

  export function shim(): void
}

And my src/utils/parseUrl.ts file:

import { URL } from 'universal-url'

export function parseUrl(url: string) {
  return new URL(url)
}

Again the key is the typeRoots field of tsconfig.json. Here is the documentation: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html#types-typeroots-and-types

like image 120
Gordon Burgett Avatar answered Nov 04 '22 09:11

Gordon Burgett