2017-04-01 119 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

謝謝!!這解決了它。 :) –