2017-09-27 1 views
0

index.html 파일에 'Hello world! H1에서 '텍스트 : 나는 모든을 저장하고 다음과 같이 실행자바 스크립트 + 모카 + 차이. 왜 내 검사는 항상 통과합니까?

import {expect} from 'chai'; 
import jsdom from 'jsdom'; 
import fs from 'fs'; 

describe('index.html',() => { 
    it("should say 'Hello world!'",() => { 
     // read file content to variable 
     const index = fs.readFileSync('./src/index.html', "utf-8"); 

     // pass this variable to jsdom: 
     jsdom.env(index, function(err, window) { 
      const h1 = window.document.getElementByTagName('h1')[0]; // read our h1 
      expect(h1.innerHTML).to.equal("Helloooooooo World!");  //<---- passed 
      done(); 
      window.close(); 
     }); 
    }) 
}) 

: 여기

<!DOCTYPE HTML> 
<html> 
<head> 
    <title></title> 
<meta charset="UTF-8"> 
</head> 

<body> 
    <h1>Hello world!</h1> 
    <script src="bundle.js"></script> 
</body> 
</html> 

및 내 index.test.js입니다

"test": "mocha --reporter progress buildScripts/testSetup.js \"src/**/*.test.js\"" 

과 항상보고 "합격".

enter image description here

OO

enter image description here

답변

3

을 심지어 expect 문자열을 언급 할 수 있고, 너무 통과 당신은 it에 인수로 done를 선언해야합니다. 당신이 본질적이라고 done 때까지 기다려야 모카에게 그 첫번째 인수를 선언함으로써

it('Does x', (done) => { 
    someAsyncFunc((err, result) => { 
    done() 
    }) 
}) 

, 그렇지 않으면 그냥 그것을 당신의 expect 호출을 실행하는 데 기다릴 및 조기 의지하는 동기 테스트 따라서

을 고려 간단히 때 콜백을 호출 Mocha's docs

에서 성공적인

로 시험을 고려 테스트가 완료되었습니다. 콜백 (일반적으로 done이라고 함)을 it()에 추가함으로써 모카는 테스트를 완료하기 위해이 함수가 호출 될 때까지 기다려야한다는 것을 알게됩니다.

+0

아 .. 당신을 감사합니다! 이제 그것은 매력처럼 작동합니다! + 나는 또한 'getElement_s_ByTagName'이 있어야한다는 것을 발견했습니다. :) –

0

그것은해야한다 :

it("should say 'Hello world!'", (done) => { 
+0

예, 맞습니다! 고맙습니다! –

관련 문제