2017-07-14 352 views
1

我試圖用Pact庫執行一些測試,我收到一些錯誤。下面是測試配置:契約退出代碼1

const path = require('path'); 
const Pact = require('pact'); 
const expect = require('expect.js'); 
const config = require('../../../src/server/config'); 
const service = require('../../../src/routes/interactions/interactions.service'); 

describe('@component/interactions tests',() => { 
    const url = 'http://localhost'; 
    const port = 8989; 

    const provider = Pact({ 
     port: port, 
     log: path.resolve(process.cwd(), 'test/component/interactions/log/interactions-pact.log'), 
     dir: path.resolve(process.cwd(), 'test/component/interactions/pacts'), 
     spec: 2, 
     consumer: 'cx_issue', 
     provider: 'interaction', 
     // logLevel: 'WARN' 
    }); 

    config.settingsToExport.INTERACTION_URL = `${url}:${port}`; 

    before(done => { 
     provider.setup() 
      .then(() => { 
       done(); 
      }) 
      .catch(err => { 
       done(err); 
      }); 
    }); 

    after(done => { 
     provider.finalize() 
      .then(() => { 
       done(); 
      }) 
      .catch(err => { 
       done(err); 
      }); 
    }); 

    describe('#createInteraction',() => { 
     before(done => { 
      const INTERACTION_BODY = { 
       contact: 'contact1' 
      }; 
      const TERM = { 
       generate: '0dae5b93-9451-4b08-b7bb-f0b944fbcdf2', 
       matcher: '^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$' 
      }; 

      const pactInteractionCreate = { 
       state: 'Creates a new interaction', 
       uponReceiving: 'a new interaction is created successfully', 
       withRequest: { 
        method: 'POST', 
        path: '/interactions', 
        body: INTERACTION_BODY 
       }, 
       willRespondWith: { 
        status: 201, 
        body: { 
         id: Pact.Matchers.term(TERM) 
        } 
       } 
      }; 

      const promises = [ 
       provider.addInteraction(pactInteractionCreate) 
      ]; 
      Promise.all(promises) 
       .then(() => { 
        done(); 
       }); 
     }); 

     it('/api/interactions POST', done => { 

      const interaction = { 
       contact: 'The xx' 
      }; 

      service.createInteraction(interaction) 
       .then(response => { 
        expect(response.id).to.be.equal(TERM.generate); 
        done(); 
       }) 
       .catch(done); 
     }); 
    }); 
}); 

這是我package.json文件內容,與所有依賴我已經安裝:

{ 
    "name": "issueAPI", 
    "version": "1.0.0", 
    "private": true, 
    "main": "./src/index.js", 
    "scripts": { 
    "dev": "nodemon -e js ./src/index.js", 
    "start": "node ./src/index.js", 
    "linter": "node ./node_modules/eslint/bin/eslint.js ./src", 
    "test": "mocha test", 
    "test-component": "mocha test/component", 
    "install-test-build": "npm install && npm test && npm run linter", 
    "test-build": "npm test && npm run linter" 
    }, 
    "jshintConfig": { 
    "esversion": 6 
    }, 
    "dependencies": { 
    "ajv": "^4.11.3", 
    "body-parser": "^1.17.2", 
    "express": "^4.15.3", 
    "express-winston": "^2.4.0", 
    "request": "^2.81.0", 
    "winston": "^2.3.1", 
    "yamljs": "^0.2.9" 
    }, 
    "devDependencies": { 
    "@pact-foundation/pact-node": "^4.8.3", 
    "dotenv": "^4.0.0", 
    "eslint": "^4.2.0", 
    "eslint-config-node": "^1.6.0", 
    "expect.js": "^0.3.1", 
    "mocha": "^3.2.0", 
    "nodemon": "^1.11.0", 
    "pact": "^2.3.3", 
    "sinon": "^2.3.8", 
    "supertest": "^3.0.0" 
    } 
} 

這是錯誤我得到: enter image description here

基本上如果測試結果不好,我現在完全不介意。目前的主要問題是協議模擬服務器沒有啓動。

這裏的奇怪的事情是,我有其他項目,協議測試正常運行。我已經將項目中要測試的服務移到了那些無法正常執行這些測試的項目上,並且它正在運行(至少啓動了協議模擬服務器)。在這個其他項目中的依賴關係幾乎與問題項目相同:

"dependencies": { 
    "ajv": "^4.11.3", 
    "body-parser": "^1.16.1", 
    "dotenv": "^4.0.0", 
    "express": "^4.14.0", 
    "jsonwebtoken": "^7.4.1", 
    "jwt-simple": "^0.5.1", 
    "morgan": "^1.8.1", 
    "mustache-express": "^1.2.4", 
    "node-env-file": "^0.1.8", 
    "request": "^2.79.0", 
    "when": "^3.7.8" 
    }, 
    "devDependencies": { 
    "@pact-foundation/pact-node": "^4.8.3", 
    "eslint": "^3.17.1", 
    "eslint-config-node": "^1.6.0", 
    "expect.js": "^0.3.1", 
    "mocha": "^3.2.0", 
    "nodemon": "^1.11.0", 
    "pact": "^2.3.3", 
    "sinon": "^1.17.7", 
    "supertest": "^3.0.0", 
    "nock": "^9.0.13" 
    } 

這種情況是怎麼回事?

編輯:我已經推出了pact測試與DEBUG標誌,這是日誌中生成: enter image description here enter image description here

+0

我想知道它是否與https://github.com/pact-foundation/pact-js/issues/64有關? –

回答

1

它看起來像服務器無法正確啓動,判斷退出碼爲1.這可能是端口衝突端口8989,所以這是值得檢查。

它可能與https://github.com/pact-foundation/pact-js/issues/64(Windows文件路徑長度有限)有關。

您能不能請DEBUGlogLevel: 'DEBUG'登錄並記下它輸出的內容,並分享任何*.log文件。這可能是一個參數在啓動服務器時未被正確格式化。

正如您所看到的,它無法啓動底層的Ruby進程,因此我們需要深入瞭解底層。

+0

我已經用DEBUG日誌啓動了測試,並且在問題中顯示了輸出。沒有生成日誌文件,這是我擁有的所有信息。我認爲這不是端口衝突,因爲如果我在其他項目中執行協議測試,它可以正常工作,但在這一個中,即使我更改了端口號,它也總是給我那個錯誤。 – christiansr85

+0

是的!這是一個路徑長度的問題......但我很困惑,因爲我沒有看到控制檯日誌中的任何路徑超過260個字符。無論如何,我在較短的位置路徑下載了該項目,並啓動了服務器。 – christiansr85

+0

@ christiansr85有趣。那麼你能回答你自己的問題嗎?這可能對其他人有用。 –

0

看來,我認爲服務器無法啓動正常,這通常是因爲該端口已被使用。要看到,如果是這樣的話,在你的節點命令行(CMD,輸入node去節點命令行),複製/粘貼以下:

require('http').createServer(function(){}).listen(11108); 

的「聽」功能需要的端口號碼,我從你的屏幕截圖中拿走。看看什麼節點輸出;我的猜測是它會顯示如下:

Error: listen EADDRINUSE :::11108 
    at Object.exports._errnoException (util.js:1018:11) 
    at exports._exceptionWithHostPort (util.js:1041:20) 
    at Server._listen2 (net.js:1258:14) 
    at listen (net.js:1294:10) 
    at Server.listen (net.js:1390:5) 
    at repl:1:44 
    at sigintHandlersWrap (vm.js:22:35) 
    at sigintHandlersWrap (vm.js:73:12) 
    at ContextifyScript.Script.runInThisContext (vm.js:21:12) 
    at REPLServer.defaultEval (repl.js:340:29) 

這意味着實際上存在端口衝突。看看你的任務管理器,並確保在後臺運行的節點沒有任何實例可能干擾你的測試,從而啓動一個新的服務器。您可能還想檢查任何版本的ruby.exe正在運行;不幸的是,我們必須將ruby.exe與pact-node打包在一起,並且它使用ruby轉換了一個新的模擬服務器實例,因爲這是它寫入的語言。不時發生的情況是,節點簡單地「失去了跟蹤「ruby實例,並沒有正確關閉它。

這是一個衆所周知的問題,我們正在積極嘗試通過將所有核心協議代碼合併到一個共享庫中來解決問題,該共享庫可用於所有語言和操作系統而無需旋轉增加一個額外的實例。請忍受我們,因爲我們得到了一些這些扭結:)

+0

如果是港口衝突,衝突是不是在8989港口? 11108看起來是PID。 –

+0

啊,是的,誤讀了端口號。 –

+0

@J_A_X Matthew指出,端口是8989,而11108是由Pact生成的內部PID,它在每次測試的執行過程中都是不同的。無論如何,我也試圖改變端口號,我仍然有同樣的問題。 – christiansr85

0

最後,經過@Matthew研究員答案,讀他的鏈接https://github.com/pact-foundation/pact-js/issues/64後,我搬到我的項目,以更短的路徑位置,simimlar到D:\myproject\,然後pact模擬服務器發起和我的測試中獲得通過。因此,直到找到更好的解決方案,我將在該短路徑目錄下工作,以避免出現更多問題。

請閱讀前面的鏈接,以獲取有關此錯誤的更多信息,因爲有很好的解釋。