2015-08-18 31 views
3

我有點卡在這個。我有一個由中間人,業力,茉莉花,babeljs組成的複雜堆棧來構建一個靜態網站。如何設置Karma + Jasmine使用ES6模塊

考慮到這是一個實驗,我想使用ES6模塊。但是,中間人方面的一切都很好,我很難設置業力+茉莉花來進行測試。

主要問題在於通天之內:如果你將其設置爲使用modules: "ignore"你在你的規格手動使用System.import所有你的模塊,這是我不想要的。我想用ES6語法,但如果我設置modules: "system",babeljs封裝了所有的考試進入System.register,與類似以下內容:

System.register(["mymodule"], function (_export) { 
    "use strict"; 

    var Mymodule; 
    return { 
    setters: [function (_mymodule) { 
     Mymodule = _mymodule["default"]; 
    }], 
    execute: function() { 

     console.log("I'm here!!!"); 
     console.log(Mymodule); 

     describe("Mymodule", function() { 

     it("has version", function() { 
      expect(Mymodule.VERSION).toEqual("1.0.0"); 
     }); 


     }); 
    } 
    }; 
}); 

所以測試不會自動執行。然後,我創建了下面的腳本來解決它(這包括所有規格之後包括):

basePath = "/base/spec/" 

modules = [] 

for own fileName, fileHash of window.__karma__.files 
    if fileName.indexOf(basePath) is 0 
    isRunner = fileName.indexOf("spec_runner") >= 0 
    isRunner ||= fileName.indexOf("spec_helper") >= 0 
    unless isRunner 
     moduleName = fileName.replace(basePath, "") 
     moduleName = moduleName.replace(".js", "") 
     modules.push(path: fileName, name: moduleName) 

mappedModules = {} 
baseSpecsPath = "http://localhost:9876" 

for module in modules 
    mappedModules[module.name] = baseSpecsPath + module.path 

System.config 
    baseURL: "http://localhost:4567/javascripts/" 
    map:  mappedModules 

for module in modules 
    System.import(module.name) 

這段代碼很簡單:準備SystemJS的地圖配置,我可以正確地從加載模塊我應用程序(位於http://localhost:4567)和包裹在System.register中的測試(位於http://localhost:9876)。

但是,我的測試沒有運行,並且沒有錯誤報告。更糟糕的是,我正確地將消息記錄下來「我在這裏!!!」和Mymodule正確登錄到控制檯。我甚至試圖記錄的值,描述爲,它正確的是Suite對象。那麼,爲什麼我的測試不能運行? (it塊永遠不會運行)

我有什麼解決方案?我可以改變安裝一下讓它工作,但我想保留以下內容:中間人,ES6模塊,沒有動態模塊加載(我的所有模塊最終都暴露在單個文件中或者需要一堆<script>標籤),茉莉花

+0

我正在挖掘茉莉花以瞭解(可能)爲什麼它不運行'describe'塊。我甚至試圖將它添加到窗口對象,但沒有改變任何東西 –

+0

如果有人感興趣,我甚至在Jasmine谷歌組織上打開了一個問題:https://groups.google.com/forum/#!topic/jasmine -js/M4qRl6jcyE8 –

回答

2

我終於解決了這個問題。我有這個文件作爲最後一個

basePath = "/base/spec/" 

modules = [] 

for own fileName, fileHash of window.__karma__.files 
    if fileName.indexOf(basePath) is 0 
    isRunner = fileName.indexOf("spec_runner") >= 0 
    isRunner ||= fileName.indexOf("spec_helper") >= 0 
    unless isRunner 
     moduleName = fileName.replace(basePath, "") 
     moduleName = moduleName.replace(".js", "") 
     modules.push(path: fileName, name: moduleName) 

mappedModules = {} 
baseSpecsPath = "http://localhost:9876" 
specModules = [] 

for module in modules 
    mappedModules[module.name] = baseSpecsPath + module.path 
    specModules.push(module.name) 

System.config 
    baseURL: "http://localhost:4567/javascripts/" 
    map:  mappedModules 

moduleImports = specModules.map (moduleName) -> 
    System.import(moduleName) 

Promise.all(moduleImports).then -> 
    window.__karma__.start  = window.__lastKarmaStart__ 
    window.__lastKarmaStart__ = null 
    delete window.__lastKarmaStart__ 
    window.__karma__.start() 

它下面的事情:

  • 臨時禁止因緣啓動功能,用空
  • 替換它獲取存儲在System.register每個測試,對於所有測試運行System.import(等待它們用Promise.all
  • 在所有進口完成之後,它將__karma__start d執行它,運行Jasmine