2017-08-25 34 views
1

我一直在嘗試使用mocha-webpack和Travis CI爲我的資源庫設置自動化測試。我的測試在我的本地機器上運行良好,但他們還沒有通過Travis CI完成。我一直無法弄清楚這最後一個錯誤:Travis CI,摩卡測試,Webpack編譯錯誤:找不到模塊:'jQuery'

WEBPACK Failed to compile with 1 error(s) 
Error in ./src/ts/myfile.ts 
Module not found: 'jQuery' in '/home/travis/build/myname/myrepo/src/ts' 

基於關閉的錯誤信息,它看起來像的We​​bPack試圖通過我的WebPack加入到解決jQuery的模塊(我假設進口.ProvidePlugin調用,因爲myfile.ts中沒有jquery導入),而不是在node_modules中查找它。

測試腳本

mocha-webpack --webpack-config webpack.config.js --require jsdom-global/register 

依賴

"jquery": "^3.2.1" 

開發依賴

"@types/chai": "^4.0.4" 
"@types/jquery": "3.2.0" 
"@types/mocha": "^2.2.42" 
"chai": "^4.1.1" 
"css-loader": "^0.28.5" 
"jsdom": "^11.2.0", 
"jsdom-global": "^3.0.2" 
"mocha": "^3.5.0" 
"mocha-typescript": "^1.1.7" 
"mocha-webpack": "^1.0.0-rc.1" 
"sass-loader": "^6.0.6" 
"ts-loader": "^2.3.3" 
"typescript": "^2.4.2" 
"webpack": "^3.5.5" 

個webpack.config.js

const webpack = require("webpack"); 
module.exports = { 
    target: "node", 
    externals: ["jquery", "moment"], 
    resolve: { 
    extensions: [".ts", ".js"] 
    }, 
    module: { 
    loaders: [ 
     { test: /\.ts$/, loader: "ts-loader" }, 
     { test: /\.scss$/, loaders: ['css-loader/locals?modules', 'sass-loader'] }  
    ] 
    }, 
    plugins: [ 
    new webpack.ProvidePlugin({ 
     $: "jQuery", 
     jQuery: "jQuery" 
    }) 
    ] 
} 

特拉維斯

language: node_js 
node_js: 
    - "node" 

cache: 
    directories: 
    - "node_modules" 

tsconfig.json

{ 
    "compilerOptions": { 
     "module": "commonjs", 
     "noImplicitAny": true, 
     "removeComments": true, 
     "sourceMap": true, 
     "target": "es5", 
     "lib": ["es2016", "dom"], 
     "typeRoots": [ 
      "node_modules/@types" 
     ], 
     "experimentalDecorators": true // For the decorators in Mocha tests. 
    }, 
    "compileOnSave": true, 
    "include": [ 
     "src/**/*", 
     "test/*" 
    ] 
} 

回答

1

我想通了,通過一些試驗。

我webpack.config.js已定義的jquery作爲外部:

externals: ["jquery", "moment"] 

這導致能夠從環境中除去該模塊。但是,我似乎能夠得到它通過ProvidePlugin在我的本地框運行:

new webpack.ProvidePlugin({ 
    $: "jQuery", 
    jQuery: "jQuery" 
}) 

注意jQuery中的大寫Q值。對於我的本地環境,jQuery(未被刪除,因爲它沒有在外部行中定義)被定義爲jquery模塊,但是在travis-ci中,它並沒有找到。我仍然不確定爲什麼「jQuery」首先爲我工作。

通過從配置中的外部線路,並改變了jQuery是所有小寫字母,它解決了我的問題:

new webpack.ProvidePlugin({ 
    $: "jquery", 
    jQuery: "jquery" 
})