2017-10-12 29 views
0

的格式,我有這樣的測試:玩笑toEqual不及格由於JSON

it('Outputs correct ES6 code',() => { 

    const updateCodeGenerationOptions = actions.updateSourceCode(sampleJson) 
    expect(jsonCodeTools(undefined, updateCodeGenerationOptions)) 
    .toEqual(
     { 
     'options': {'outputCodeType': 'ES6'}, 
     'outputCodeFromJSON': '<pre>export class Foo {<br> constructor (object) {<br> this.shouldBeNumber1 = object.shouldBeNumber1; <br> this.shouldBeString1 = object.shouldBeString1; <br> this.shouldBeBoolean1 = object.shouldBeBoolean1; <br> this.shouldBeFragment1 = new shouldBeFragment1(object.shouldBeFragment1); <br> } <br>}<br>export class shouldBeFragment1{<br> constructor (object) {<br> this.FshouldBeNumber1 = object.FshouldBeNumber1; <br> this.FshouldBeString1 = object.FshouldBeString1; <br> this.FshouldBeBoolean1 = object.FshouldBeBoolean1; <br> } <br>}<br></pre>', 
     'sourceCode': JSON.stringify({ 
      'shouldBeNumber1': '1', 
      'shouldBe String1': '.1', 
      'shouldBeBoolean1': 'false', 
      'shouldBeFragment1': { 
      'FshouldBeNumber1': '1', 
      'FshouldBeString1': 'bar', 
      'FshouldBeBoolean1': 'false' 
      } 
     }) 
     } 
    ) 
}) 

當它運行時它失敗與此:

enter image description here

所以對我來說這僅僅是一個格式化問題,我將如何讓這個測試通過?

回答

0

您確實存在格式問題,但您的測試確實沒有通過。看看shouldBeFragment1;期望值是錯誤的,但實際值是一個對象。

對於格式化問題,您應該使用JSON.parse然後JSON.stringify實際輸出,以便它不是非常漂亮的打印(或者更好地從被測試單元中刪除漂亮的打印)。

+0

shouldBeFragment1預計不會是假的 – Nikos

+0

那麼,Jest輸出說它預計是假的。所以。 –

+0

我看不出你能表現出來嗎? – Nikos