2017-05-26 110 views
1

測試HapiJS插件的最佳方式是什麼,例如添加路由和處理程序的插件。用Lab來測試HapiJS插件的最佳方法是什麼?

由於我必須創建一個Hapi.Server的實例來運行插件,我應該爲應用程序的根目錄定義所有插件的所有測試嗎?

我想設法Hapi.Server實例在我的插件的本地測試?

如果我選擇第二個選項,我的服務器將註冊所有的插件,包括那些被測試插件不依賴的插件。

解決此問題的最佳方法是什麼?

在此先感謝。

回答

2

如果您使用Glue(我強烈推薦它),您可以爲每個要執行的測試(或測試組)創建一個清單變量。清單隻需要包含該測試所需的插件即可正確執行。

並暴露某種init函數來實際啓動您的服務器。小例子:

import Lab = require("lab"); 
import Code = require('code'); 
import Path = require('path'); 
import Server = require('../path/to/init/server'); 
export const lab = Lab.script(); 
const it = lab.it; 
const describe = lab.describe; 

const config = {...}; 

const internals = { 
    manifest: { 
     connections: [ 
      { 
       host: 'localhost', 
       port: 0 
      } 
     ], 
     registrations: [ 
      { 
       plugin: { 
        register: '../http_routes', 
        options: config 
       } 
      }, 
      { 
       plugin: { 
        register: '../business_plugin', 
        options: config 
       } 
      } 
     ] 
    }, 
    composeOptions: { 
     relativeTo: 'some_path' 
    } 
}; 

describe('business plugin', function() { 

    it('should do some business', function (done) { 

     Server.init(internals.manifest, internals.composeOptions, function (err, server) { 
      // run your tests here 
     }); 
    }); 

}); 

init功能:

export const init = function (manifest: any, composeOptions: any, next: (err?: any, server?: Hapi.Server) => void) { 
    Glue.compose(manifest, composeOptions, function (err: any, server: Hapi.Server) { 

     if (err) { 
      return next(err); 
     } 

     server.start(function (err: any) { 

      return next(err, server); 
     }); 
    }); 
}; 
+0

感謝。我會用它。 – acmoune

相關問題