2017-04-01 120 views
1

我有一个非常小的项目,我试图设置单元测试。使用tsc直接当项目编译罚款,然而,当要执行哪个使用卡玛 - 打字稿框架测试时我正在以下打字稿编译错误:karma-typescript找不到模块

错误:

错误[ compiler.karma-typescript]:src/drawing/canvas.model.ts(1,23): 错误TS2307:找不到模块'utils'。

ERROR [compiler.karma-typescript]:src/models/grid.model.ts(1,23):错误TS2307:找不到模块'utils'。

错误[compiler.karma-typescript]:src/utils/specs/obj.utils.spec.ts(1,23):错误TS2307:找不到 模块的'utils'。


项目结构: 我有一个项目成立,其构造为这样:

|-dist/ 
|-node_modules/ 
|-src/ 
| | 
| |-drawing/ 
| | | 
| | |-canvas.model.ts 
| | ________________________________ 
| | | import { Utils } from 'utils'; | Karma Fails (tsc is fine) 
| | -------------------------------- 
| | 
| |-models/ 
| | | 
| | |-grid.model.ts 
| | ________________________________ 
| | | import { Utils } from 'utils'; | Karma Fails (tsc is fine) 
| | -------------------------------- 
| | 
| |-utils/ 
| | | 
| | |-index.ts 
| | | _________________________ 
| | | | export module Utils {} | 
| | | ------------------------- 
| | | 
| | |-specs/ 
| | | | 
| | | |-obj.utils.spec.ts 
| | | ________________________________ 
| | | | import { Utils } from 'utils'; | Karma Fails (tsc is fine) 
| | | -------------------------------- 
| | | 
|-karma.config.js 
|-tsconfig.json 
|-package.json 

很清楚,我认为有之间的差异我tsconfig.json文件和内部用于karma-typescript的编译器设置。这是我如何拥有这两个文件结构。根据documentation for karma-typescript,应该有几个选项,我可以在我的karma.conf文件中配置,它会告诉karma-typescript尊重我的Typescript配置文件中的设置,即"paths"属性,这是我指定的位置打字稿在哪里寻找我的utils模块。

KARMA.CONF.JS

// Karma configuration 
 
// Generated on Fri Mar 31 2017 16:42:11 GMT-0700 (PDT) 
 
module.exports = function(config) { 
 
    config.set({ 
 
    // base path that will be used to resolve all patterns (eg. files, exclude) 
 
    basePath: '', 
 
    // frameworks to use 
 
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter 
 
    frameworks: ['jasmine', 'karma-typescript'], 
 
    // Plugins 
 
    plugins: [ 
 
     'karma-spec-reporter', 
 
     'karma-jasmine', 
 
     'karma-chrome-launcher', 
 
     'karma-jasmine-html-reporter', 
 
     'karma-typescript' 
 
    ], 
 
    // list of files/patterns to load in the browser 
 
    files: [{ 
 
     pattern: "./src/**/*.ts" 
 
    }], 
 
    // list of files to exclude 
 
    exclude: ['**/*.spec.js'], 
 

 
    // Karma Typescript compiler options 
 
    karmaTypescriptConfig: { 
 
     bundlerOptions: { 
 
     resolve: { 
 
      directories: ["src", "node_modules", "src/utils"] 
 
     } 
 
     } 
 
    }, 
 
    compilerOptions: { 
 
     module: 'commonjs', 
 
     moduleResolution: 'node', 
 
     paths: { 
 
     'utils': ['./src/utils'], 'utils/*': ['./src/utils/*'] 
 
     } 
 
    }, 
 
    tsconfig: './tsconfig.json', 
 

 

 
    // preprocess matching files before serving them to the browser 
 
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor 
 
    preprocessors: { 
 
     "**/*.ts": ["karma-typescript"] 
 
    }, 
 
    // test results reporter to use 
 
    // possible values: 'dots', 'progress' 
 
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter 
 
    reporters: ['progress', 'kjhtml', 'spec', "karma-typescript"], 
 
    // web server port 
 
    port: 9876, 
 
    // enable/disable colors in the output (reporters and logs) 
 
    colors: true, 
 
    // level of logging 
 
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG 
 
    logLevel: config.LOG_INFO, 
 
    // enable/disable watching file and executing tests whenever any file changes 
 
    autoWatch: true, 
 
    // start these browsers 
 
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher 
 
    browsers: ['Chrome'], 
 
    // Continuous Integration mode 
 
    // if true, Karma captures browsers, runs the tests and exits 
 
    singleRun: false, 
 
    // Concurrency level 
 
    // how many browser should be started simultaneous 
 
    concurrency: Infinity 
 
    }) 
 
}


这里是我的打字稿配置文件。请注意,我在tsconfig文件的"paths"部分中注册了"utils",该部分可协助Typescript编译器的module resolution process。这与正常的Typescript编译工作正常,但这可能是因为我的Typescript编译器实际上是尊重我的tsconfig文件中的设置。我正在使用Typescript 2.0.10。但看来,业力打字稿使用Typescript 2.2.2,这可能是错误的潜在来源。我将不得不针对该版本运行我的编译器,以查看是否可以生成相同的错误。

TSCONFIG.JSON

{ 
 
    "compileOnSave": true, 
 
    "compilerOptions": { 
 
    "baseUrl": ".", 
 
    "outDir": "./dist", 
 
    "paths": { 
 
     "utils/*": ["./src/utils/*"], 
 
     "utils": ["./src/utils"] 
 
    }, 
 
    "declaration": true, 
 
    "emitDecoratorMetadata": true, 
 
    "experimentalDecorators": true, 
 
    "lib": ["es5", "dom"], 
 
    "mapRoot": "./", 
 
    "module": "es6", 
 
    "moduleResolution": "node", 
 
    "sourceMap": true, 
 
    "target": "es5", 
 
    "rootDirs": ["./dist", "."] 
 
    }, 
 
    "exclude": ["./node_modules", "./dist", "**/*.d.ts"], 
 
    "include": ["./src/**/*.ts"] 
 
}

谁能帮我这个?我对Typescript很满意,但对Karma来说很新。为了让这些简单的单元测试正常运行,我一直在搜寻大约2天的文档,但无济于事。不以我至少有我的路径结构的方式。任何帮助,将不胜感激!


UPDATE: 我尝试更新我的打字稿的本地安装到2.2.2,以匹配噶打字稿的版本,这也是2.2.2。同样的错误,相同的情况 - 我的本地版本编译得很好,但Karma-Typescript版本很差。

回答

4

在Karma配置中存在一个小错误,compilerOptionstsconfig属性应该在karmaTypescriptConfig属性中。

鉴于你的项目结构例子,这里有两个tsckarma-typescript最小工作配置例如:

Karma.conf.js

module.exports = function(config) { 
    config.set({ 

     frameworks: ["jasmine", "karma-typescript"], 

     files: [ 
      { pattern: "src/**/*.ts" } 
     ], 

     karmaTypescriptConfig: { 
      compilerOptions: { 
       module: "commonjs" 
      }, 
      tsconfig: "./tsconfig.json", 
     }, 

     preprocessors: { 
      "**/*.ts": ["karma-typescript"] 
     }, 

     reporters: ["progress", "kjhtml", "spec", "karma-typescript"], 

     browsers: ["Chrome"] 
    }); 
}; 

tsconfig.json

{ 
    "compileOnSave": true, 
    "compilerOptions": { 
     "baseUrl": ".", 
     "outDir": "./dist", 
     "paths": { 
      "utils/*": ["./src/utils/*"], 
      "utils": ["./src/utils"] 
     }, 
     "module": "es6", 
     "moduleResolution": "node", 
     "sourceMap": true, 
     "target": "es5" 
    } 
} 

我测试了这个使用[email protected][email protected]

而且,请注意,karma-typescript只有打字稿作为一个开发的依赖,让你使用任何打字稿版本1.6.2从高达:)

+0

谢谢!!这解决了它。 :) –