Mocha: μ–΄μ„€μ…˜ 였λ₯˜ λŒ€μ‹  μ‹œκ°„ 초과둜 비동기 ν…ŒμŠ€νŠΈκ°€ μ‹€νŒ¨ν•¨

에 λ§Œλ“  2014λ…„ 02μ›” 05일  Β·  25μ½”λ©˜νŠΈ  Β·  좜처: mochajs/mocha

이 ν…ŒμŠ€νŠΈ:

    it('returns the correct value', function(done) {
      var returnValue = 5;

      aPromise.then(function() {
        expect(returnValue).to.equal(42);
        done();
      });

    });

이 ν…ŒμŠ€νŠΈλŠ” μ–΄μ„€μ…˜ 였λ₯˜ λŒ€μ‹  timeout of 2000ms exceeded μ‹€νŒ¨ν•©λ‹ˆλ‹€. expect() ν˜ΈμΆœμ—μ„œ 였λ₯˜κ°€ λ°œμƒν•˜κ³  done() κ°€ μ‹€ν–‰λ˜μ§€ μ•Šκ³  이런 μ’…λ₯˜μ˜ μ½”λ“œλ₯Ό ν…ŒμŠ€νŠΈν•˜λŠ” 더 쒋은 방법이 μžˆλŠ”μ§€ κΆκΈˆν•©λ‹ˆλ‹€.

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

λ‚˜λŠ” λΉ„μŠ·ν•œ λ¬Έμ œμ— λΆ€λ”ͺμ³€κ³  κ²°κ΅­ 약속이 μžˆλŠ” 비동기 ν•¨μˆ˜λ₯Ό ν…ŒμŠ€νŠΈν•  λ•Œ done을 μ‚¬μš©ν•΄μ„œλŠ” μ•ˆ λœλ‹€λŠ” 것을 κΉ¨λ‹¬μ•˜μŠ΅λ‹ˆλ‹€. λŒ€μ‹  약속을 λ°˜ν™˜ν•˜μ‹­μ‹œμ˜€. λ”°λΌμ„œ μ‹œκ°„ 초과 없이 이 μž‘μ—…μ„ μˆ˜ν–‰ν•  수 μžˆμ–΄μ•Ό ν•©λ‹ˆλ‹€. 예:

it('returns the correct value', function() {
    var returnValue = 5;

    return aPromise.then(function() {
        expect(returnValue).to.equal(42);
    });
});

λͺ¨λ“  25 λŒ“κΈ€

aPromise κ°€ ν•΄κ²°λ˜λ‚˜μš”? 그렇지 μ•Šμ€ 경우 μ‹œκ°„ 초과λ₯Ό λ°œμƒμ‹œν‚€λŠ” 것 μ™Έμ—λŠ” μ„ νƒμ˜ 여지가 μ—†μŠ΅λ‹ˆλ‹€.

@NickHeiner 예, ν•΄κ²°λ©λ‹ˆλ‹€. expect() λŠ” returnValue κ°€ equal(42) κ°€ μ•„λ‹Œ 것을 μ°Ύμ•„ λ˜μ§‘λ‹ˆλ‹€.

@gurdiga μ‹œκ°„ μ΄ˆκ³Όκ°€ λ°œμƒν•˜κ³  μ–΄μ„€μ…˜ 였λ₯˜κ°€ μ•„λ‹Œ 경우

@hallas @NickHeiner μ‹€ν–‰ 쀑인 ν•­λͺ©μ€ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€. http://jsfiddle.net/gurdiga/p9vmj/.

@gurdiga λ‹Ήμ‹ μ˜ 약속에 자체 였λ₯˜κ°€ μžˆλŠ” 것 κ°™μŠ΅λ‹ˆλ‹€. λ‹Ήμ‹ μ˜ 약속에 .catch(done) λ₯Ό μΆ”κ°€ν•˜λ €κ³  ν•˜λ©΄ μ˜λ„ν•œ λŒ€λ‘œ μž‘λ™ν•  것이라고 μƒκ°ν•©λ‹ˆλ‹€.

@hallas μ™€μš°: _κ·Έ_κ°€ λ‹΅μ΄μ—ˆμŠ΅λ‹ˆλ‹€! :) aPromise.finally() λŠ” done 에 넣기에 μ™„λ²½ν•œ 것 κ°™μŠ΅λ‹ˆλ‹€: λ˜ν•œ Promiseκ°€ 해결될 λ•Œ ν˜ΈμΆœλ˜μ–΄μ•Ό ν•©λ‹ˆλ‹€. ;)

κ°μ‚¬ν•©λ‹ˆλ‹€!

λ‚΄ μžμ‹ μ΄ λ©μ²­ν•˜κ²Œ λŠκ»΄μ§„λ‹€.

λ‚˜λŠ” λ§ˆμΉ¨λ‚΄ 그것을 μ–»μ—ˆλ‹€κ³  μƒκ°ν•œλ‹€: μ–΄λ–€ 것이 ν”„λΌλ―ΈμŠ€μ˜ ν•Έλ“€λŸ¬ ν•¨μˆ˜μ—μ„œ 던질 λ•Œ, 그것이 .then() , .catch() λ˜λŠ” .finally() μ „λ‹¬λ˜λ“ , 였λ₯˜λŠ” Promise λΌμ΄λΈŒλŸ¬λ¦¬μ—μ„œ μ²˜λ¦¬ν•©λ‹ˆλ‹€. 이런 μ‹μœΌλ‘œ ν…ŒμŠ€νŠΈ λŸ¬λ„ˆλŠ” μ‹€μ œ 였λ₯˜λ₯Ό μ ˆλŒ€ 보지 λͺ»ν•˜κ³  done() λŠ” μ ˆλŒ€ ν˜ΈμΆœλ˜μ§€ μ•ŠμœΌλ©°(μ–΄μ„€μ…˜ 였λ₯˜κ°€ λ°œμƒν•˜κΈ° 전에 무언가가 λ°œμƒν–ˆκΈ° λ•Œλ¬Έμ—), λ”°λΌμ„œ ν…ŒμŠ€νŠΈ λŸ¬λ„ˆμ—μ„œ 얻을 수 μžˆλŠ” λͺ¨λ“  것은 μ‹œκ°„ 초과 였λ₯˜μž…λ‹ˆλ‹€.

약속을 λ²—μ–΄λ‚˜κΈ° μœ„ν•΄ setTimeout() .

    it('returns the correct value', function(done) {
      var returnValue = 5;

      aPromise.then(function() {
        setTimeout(function() {
          expect(returnValue).to.equal(42);
          done();
        });
      });
    });

이것이 μ μ ˆν•œ 였λ₯˜ λ©”μ‹œμ§€μ™€ ν…ŒμŠ€νŠΈ λŸ¬λ„ˆ λ™μž‘μ„ μ–»λŠ” μœ μΌν•œ λ°©λ²•μ΄λΌλŠ” 것을 μ•Œμ•˜μŠ΅λ‹ˆλ‹€.

done κ°€ .catch() λ˜λŠ” .finally() 에 μ „λ‹¬λ˜λ©΄ ν…ŒμŠ€νŠΈλŠ” μ–΄λ–€ κ²½μš°μ—λ„ ν†΅κ³Όλœ κ²ƒμœΌλ‘œ κ°„μ£Όλ˜λ―€λ‘œ μ–΄μ„€μ…˜ 였λ₯˜κ°€ 있으면 μ ˆλŒ€ λ³Ό 수 μ—†μŠ΅λ‹ˆλ‹€.

λ‚˜λŠ” λΉ„μŠ·ν•œ λ¬Έμ œμ— λΆ€λ”ͺμ³€κ³  κ²°κ΅­ 약속이 μžˆλŠ” 비동기 ν•¨μˆ˜λ₯Ό ν…ŒμŠ€νŠΈν•  λ•Œ done을 μ‚¬μš©ν•΄μ„œλŠ” μ•ˆ λœλ‹€λŠ” 것을 κΉ¨λ‹¬μ•˜μŠ΅λ‹ˆλ‹€. λŒ€μ‹  약속을 λ°˜ν™˜ν•˜μ‹­μ‹œμ˜€. λ”°λΌμ„œ μ‹œκ°„ 초과 없이 이 μž‘μ—…μ„ μˆ˜ν–‰ν•  수 μžˆμ–΄μ•Ό ν•©λ‹ˆλ‹€. 예:

it('returns the correct value', function() {
    var returnValue = 5;

    return aPromise.then(function() {
        expect(returnValue).to.equal(42);
    });
});

이 λ¬Έμ œλŠ” μ—¬μ „νžˆ μ‘΄μž¬ν•œλ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€. λ‚΄ λͺ¨λ“ˆμ΄ 약속을 μ‚¬μš©ν•˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ— 약속을 λ°˜ν™˜ν•˜μ—¬ ν•΄κ²°ν•  수 μ—†λŠ” μ‹œκ°„ 초과 λ¬Έμ œκ°€ λ°œμƒν•©λ‹ˆλ‹€.

it("works", function(done) {
    new Something()
    .on("eventA", function(result) {
        expect(result).to.be.true;
    })
    .on("eventB", function(result) {
        expect(result).to.be.false;
        done();
    });
});
  • Promise μΈμŠ€ν„΄μŠ€λ₯Ό λž˜ν•‘ν•˜λŠ” 것은 κ³Όλ„ν•œ 것 κ°™μŠ΅λ‹ˆλ‹€.
  • try / catch 각 μ£Όμž₯을 λž˜ν•‘ν•˜λŠ” 것도 과도해 보이며 더 μ€‘μš”ν•œ 것은 Error: done() called multiple times λ©λ‹ˆλ‹€.

아이디어:
http://staxmanade.com/2015/11/testing-asyncronous-code-with-mochajs-and-es7-async-await/

λΈ”λ‘œκ·Έ κ²Œμ‹œλ¬Όμ˜ ꢌμž₯ 사항과 κ΄€λ ¨ν•˜μ—¬ 비동기 ν•¨μˆ˜ 였λ₯˜ μ²˜λ¦¬μ— λŒ€ν•΄μ„œλŠ” λͺ¨λ₯΄μ§€λ§Œ 일반 μ•½μ†μ˜ 경우 try-catch ꢌμž₯ 사항이 μ΄μƒν•©λ‹ˆλ‹€. try-catchκ°€ μ—†λŠ” μ›λž˜ 약속 μ‹œλ„λŠ” 거의 μ •ν™•ν–ˆμœΌλ©° .catch(done) 만 μ‚¬μš©ν•˜λ©΄ λ©λ‹ˆλ‹€ then 두 번째 λ§€κ°œλ³€μˆ˜λ₯Ό .catch(done) done . (λ¬Όλ‘ , MochaλŠ” Promiseλ₯Ό 직접 μ§€μ›ν•˜κΈ° λ•Œλ¬Έμ— Promiseλ₯Ό λ°˜ν™˜ν•  μˆ˜λ„ μžˆμ§€λ§Œ κ·Έλ§Œν•œ κ°€μΉ˜κ°€ μžˆλŠ” 만큼...) 초기 Promise 예제의 λ¬Έμ œλŠ” try-catchκ°€ μ—†λŠ” 것이 μ•„λ‹ˆλΌ 두 번째 ν•Έλ“€λŸ¬κ°€ to then λŠ” 첫 번째 μ²˜λ¦¬κΈ°μ—μ„œ throw된 μ˜ˆμ™Έμ™€ ν•¨κ»˜ ν˜ΈμΆœλ˜μ§€ μ•ŠλŠ” 반면 λ‹€μŒ catch λŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€. ν”„λΌλ―ΈμŠ€κ°€ 그런 μ‹μœΌλ‘œ μ„€κ³„λœ κ·Όκ±°κ°€ λ¬΄μ—‡μΈμ§€λŠ” λͺ¨λ₯΄κ² μ§€λ§Œ ν”„λΌλ―ΈμŠ€κ°€ μ–΄λ–»κ²Œ μƒκ²ΌλŠ”μ§€ μ•Œ 수 μžˆμŠ΅λ‹ˆλ‹€. λ˜ν•œ, μ–΄λ–€ 이유둜 then κ°€ μ—¬λŸ¬ 개 μžˆλŠ” 경우 μ΅œμ’… .catch(done) ν•˜λ‚˜λ§Œ ν•„μš”ν•˜κ²Œ 되며, μ΄λŠ” ν•Έλ“€λŸ¬ λ‚΄λΆ€μ˜ try-catch보닀 μš°μ›”ν•œ μ μž…λ‹ˆλ‹€. 사싀 .catch(done) λŠ” μ‹œμž‘ν•˜κΈ°μ— 덜 μƒμš©κ΅¬μ μž…λ‹ˆλ‹€.)

API:

  1. 두 μ΄λ²€νŠΈκ°€ λͺ¨λ‘ μ˜¬λ°”λ₯Έ μˆœμ„œλ‘œ 호좜되고 μžˆμŠ΅λ‹ˆκΉŒ? 그렇지 μ•Šμ€ 경우 ν…ŒμŠ€νŠΈμ—μ„œ μ–΄λ–»κ²Œ 정상적인 μ‹€νŒ¨λ‘œ λ°”λ€Œμ—ˆμŠ΅λ‹ˆκΉŒ?
  2. 이벀트 ν•Έλ“€λŸ¬μ—μ„œ λ°œμƒν•œ μ˜ˆμ™ΈλŠ” 일반적으둜 μ–΄λ–»κ²Œ λ©λ‹ˆκΉŒ? 동기 μ½”λ“œμ—μ„œ μˆ˜ν–‰ν•˜λŠ” λ°©μ‹μœΌλ‘œ μ „νŒŒλ˜μ§€ μ•Šκ³  λŒ€μ‹  API(예: .on("error", function(error) {...}) )μ—μ„œ μˆ˜μ‹ λ˜λ„λ‘ λ˜μ–΄ μžˆλŠ” 경우, μˆ˜μ‹ ν•˜μ§€ μ•ŠλŠ” ν•œ Mocha에 λ„λ‹¬ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. λ¦¬μŠ€λ„ˆλŠ” 였λ₯˜μ™€ ν•¨κ»˜ done λ₯Ό ν˜ΈμΆœν•©λ‹ˆλ‹€(λ˜λŠ” λ¦¬μŠ€λ„ˆκ°€ 첫 번째 λ§€κ°œλ³€μˆ˜λ‘œ 였λ₯˜λ₯Ό 전달받은 경우 done λ₯Ό λ¦¬μŠ€λ„ˆμ™€ ν•¨κ»˜ μ‚¬μš©ν•©λ‹ˆλ‹€. 예: .on("error", done) . μ•„λ§ˆλ„ 그것은 λ˜ν•œ Promise의 .catch(done) 와 같이 이벀트 ν•Έλ“€λŸ¬λ‹Ή ν•œ 번이 μ•„λ‹ˆλΌ ν…ŒμŠ€νŠΈλ‹Ή ν•œ 번 μž‘μ„±ν•΄μ•Ό ν•©λ‹ˆλ‹€.
  1. 예, 그리고 "end" / "drain" 이벀트λ₯Ό μ‚¬μš©ν•˜μ—¬ λ‹€λ₯Έ 이벀트의 λΆ€μšΈμ΄ μ„€μ •λ˜μ—ˆλŠ”μ§€ ν™•μΈν•©λ‹ˆλ‹€.
  2. νƒ€μž„μ•„μ›ƒμ΄ λ°œμƒν•©λ‹ˆλ‹€. λ‚˜λŠ” ν¬λ°•ν•˜κ³  κΉ¨λ—ν•œ λŒ€μ•ˆμ„ 찾으렀고 λ…Έλ ₯ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

μ£„μ†‘ν•©λ‹ˆλ‹€. κ·€ν•˜μ˜ APIκ°€ 였λ₯˜λ₯Ό λ³΄κ³ ν•˜λŠ” 방법을 아직 λͺ¨λ₯΄κ² μŠ΅λ‹ˆλ‹€.

예, μ§€κΈˆκΉŒμ§€ μ €λŠ” _λ•Œ_ 무언가가 μ‹€νŒ¨ν–ˆμ„ λ•Œμ˜ μ‹œκ°„ μ œν•œμ— μ˜μ‘΄ν•΄ μ™”μŠ΅λ‹ˆλ‹€. 그런 λ‹€μŒ _μ–΄λ–»κ²Œ/μ™œ_ μ•Œμ•„λ‚΄κΈ° μœ„ν•΄ μˆ˜λ™μœΌλ‘œ νŒŒν—€μ³€μŠ΅λ‹ˆλ‹€. λ‹€ν–‰μŠ€λŸ½κ²Œλ„ λ¬Έμ œκ°€ 거의 λ°œμƒν•˜μ§€ μ•Šμ§€λ§Œ 더 λ‚˜μ€ λ””μžμΈμ΄ μ—†λ‹€λŠ” 핑계가 λ˜μ§€λŠ” μ•ŠμŠ΅λ‹ˆλ‹€.

@stevenvachon : 미리 μš©μ„œν•΄ μ£Όμ‹­μ‹œμ˜€. ν•˜μ§€λ§Œ κ·€ν•˜μ˜ μ˜ˆμ— 즉각적인 λ¬Έμ œλŠ” 보이지 μ•ŠμŠ΅λ‹ˆλ‹€. 이벀트 λ¦¬μŠ€λ„ˆμ—μ„œ λ§Œλ“  μ–΄μ„€μ…˜μ€ uncaughtException 맀핑을 톡해 Mochaμ—μ„œ μ²˜λ¦¬ν•΄μ•Ό ν•©λ‹ˆλ‹€(이벀트 이미터 κ΅¬ν˜„μ΄ λ¦¬μŠ€λ„ˆ 였λ₯˜λ₯Ό ν¬μ°©ν•˜κ³  error 이벀트 등을 λ‚΄λ³΄λ‚΄λŠ” 경우 μ œμ™Έ). ν•΄κ²°ν•˜λ‹€).

이제 λ‚΄λΆ€ κ΅¬ν˜„μ΄ Promiseλ₯Ό μ‚¬μš©ν•˜μ§€λ§Œ Promiseλ₯Ό λ…ΈμΆœν•˜μ§€ μ•Šκ³  이벀트λ₯Ό λ‚΄λ³΄λ‚΄λŠ” 경우 μ£Όμž₯은 μ‹€μ œλ‘œ "먹을" κ²ƒμž…λ‹ˆλ‹€. 이 문제λ₯Ό ν•΄κ²°ν•˜λŠ” 방법은 unhandledRejection μž…λ‹ˆλ‹€.

λ‚˜λŠ” 일반적으둜 ν…ŒμŠ€νŠΈ 전에 μ‹€ν–‰λ˜λŠ” μ„€μ • μŠ€ν¬λ¦½νŠΈμ— 이것을 λ„£μŠ΅λ‹ˆλ‹€.

process.on('unhandledRejection', function (reason)
{
    throw reason;
});

μ°Έκ³ : λΈŒλΌμš°μ €μ—μ„œ μž‘λ™ν•˜λ €λ©΄ μΆ”κ°€ νŒ”κΏˆμΉ˜ κ·Έλ¦¬μŠ€κ°€ ν•„μš”ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

이것이 일반적인 μ‚¬μš© 사둀이기 λ•Œλ¬Έμ— Mochaκ°€ uncaughtException 처럼 이것을 μ§€μ›ν•˜κΈ°λ₯Ό λ°”λžλ‹ˆλ‹€. Promiseλ₯Ό μ‚¬μš©ν•œλ‹€κ³  ν•΄μ„œ ν˜ΈμΆœμžμ—κ²Œ λ°˜ν™˜ν•˜κ³  싢은 것은 μ•„λ‹™λ‹ˆλ‹€!

[email protected] 와 λ™μΌν•œ λ¬Έμ œκ°€

    it('Convert files into base64', (resolve) => {
        let files = Promise.all(promises);

        return files
            .then(([actual, expected]) => {
                assert.equal(actual, expected, 'Files not are equal');
                resolve();
            })
            .catch(error => resolve);
    });
   Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.

.catch 이 잘λͺ»λ˜μ—ˆμŠ΅λ‹ˆλ‹€. error => resolve λŠ” function(error) { return resolve } 와 λ™μΌν•©λ‹ˆλ‹€. 즉, resolve κ°€ ν˜ΈμΆœλ˜μ§€ μ•Šκ³  였λ₯˜κ°€ λ¬΄μ‹œλ©λ‹ˆλ‹€. 당신이 μ›ν•˜λŠ” 것은 ν˜ΈμΆœν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€ resolve 될 였λ₯˜μ™€ error => resolve(error) . λ¬Όλ‘  X κ°€ ν˜ΈμΆœλ˜λŠ” 것과 λ™μΌν•œ 인수둜 Y ν•¨μˆ˜λ₯Ό λ‹¨μˆœνžˆ ν˜ΈμΆœν•˜λŠ” 콜백 ν•¨μˆ˜ X λ₯Ό μ „λ‹¬ν•˜λŠ” 것은 Y κ³Ό 같이 μ „λ‹¬ν•˜λŠ” 것과 κ°™μŠ΅λ‹ˆλ‹€. μ½œλ°±μ΄λ―€λ‘œ .catch(error => resolve(error)) 도 .catch(resolve) 둜 λ‹¨μˆœν™”ν•  수 μžˆμŠ΅λ‹ˆλ‹€. (당신은 ν†΅κ³Όν•˜μ§€ν•΄μ•Όν•©λ‹ˆλ‹€ resolve 당신이 그것을 ν†΅κ³Όν•œλ‹€λ©΄ 직접 then κ·ΈλŸ¬λ―€λ‘œν•˜κ³ , 톡과 ν”Όν•˜κΈ° μœ„ν•΄ ν•„μš”, then μ—μ˜ κ²°κ³Ό 맀개 λ³€μˆ˜λ₯Ό resolve 였λ₯˜λ‘œ μ²˜λ¦¬λ˜λŠ” 것을 λ°©μ§€ν•˜κΈ° μœ„ν•΄ then(()=>resolve()) .then(resolve) then(()=>resolve()) λŒ€μ‹  .then(resolve) ; ν•˜μ§€λ§Œ μ£Όμž₯에 λŒ€ν•΄ then μ½œλ°±μ„ μ‚¬μš©ν•˜κΈ° λ•Œλ¬Έμ— 이것은 λ‚˜νƒ€λ‚˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. .)

(λ˜ν•œ κ΄€μš©μ μœΌλ‘œ μ—¬κΈ° resolve λŠ” done 라인을 따라 이름을 지정해야 ν•©λ‹ˆλ‹€. 성곡과 μ‹€νŒ¨λ₯Ό λͺ¨λ‘ μ²˜λ¦¬ν•˜κ³  인수둜 ν˜ΈμΆœλ˜μ—ˆλŠ”μ§€ 여뢀에 따라 νŒλ‹¨ν•˜κΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€. λ”°λΌμ„œ Mocha의 였λ₯˜ λ©”μ‹œμ§€μ— μžˆλŠ” μ΄λ¦„μž…λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ 그것은 논점일 수 μžˆμŠ΅λ‹ˆλ‹€. 계속 μ½μœΌμ‹­μ‹œμ˜€.)

κ·ΈλŸ¬λ‚˜ 이 κ²½μš°μ—λŠ” Promiseλ₯Ό λ°˜ν™˜ν•˜κ³  ν…ŒμŠ€νŠΈ μ™„λ£Œ λ§€κ°œλ³€μˆ˜λ₯Ό μ „ν˜€ μ‚¬μš©ν•˜μ§€ μ•ŠμŒμœΌλ‘œμ¨ 훨씬 더 λ‹¨μˆœν™”ν•  수 μžˆμŠ΅λ‹ˆλ‹€. MochaλŠ” ν…ŒμŠ€νŠΈ 성곡 λ˜λŠ” μ‹€νŒ¨λ₯Ό λ‚˜νƒ€λ‚΄λŠ” κ²ƒμœΌλ‘œ 약속이 μ„±κ³΅ν•˜κ±°λ‚˜ μ‹€νŒ¨ν•  λ•ŒκΉŒμ§€ 기닀릴 것이기 λ•Œλ¬Έμž…λ‹ˆλ‹€(μ™„λ£Œ λ§€κ°œλ³€μˆ˜κ°€ μ—†λ‹€λ©΄ ν…ŒμŠ€νŠΈ κΈ°λŠ₯, λ‘˜ λ‹€ μ‚¬μš©λ˜λŠ” 경우의 λ™μž‘μ€ μ—¬μ „νžˆ β€‹β€‹ν•΄μ‹œ 아웃됨):

it('Convert files into base64', () => {
    let files = Promise.all(promises);
    return files
        .then(([actual, expected]) => {
            assert.equal(actual, expected, 'Files not are equal');
        })
});

@lsphilips λŠ” μ €μ—κ²Œ νš¨κ³Όμ μž…λ‹ˆλ‹€. 감사 ν•΄μš”!! mocha도 기본적으둜 이것을 μ§€μ›ν•˜κΈ°λ₯Ό λ°”λžλ‹ˆλ‹€. 방금 #2640을 λ§Œλ“€μ—ˆμŠ΅λ‹ˆλ‹€.

이 문제λ₯Ό ν•΄κ²°ν•˜λŠ” 데 μ‹œκ°„μ΄ κ±Έλ ΈμŠ΅λ‹ˆλ‹€! μœ„μ˜ 닡변을 λ°”νƒ•μœΌλ‘œ λ‹€μŒ 두 가지 μ˜΅μ…˜μ΄ μžˆμŠ΅λ‹ˆλ‹€.

npm install --save mocha expect.js q
./node_modules/mocha/bin/mocha test.spec.js

// test.spec.js

var $q = require('q');
var expect = require('expect.js');

describe('tests with done', function(){
    it('returns the correct value from promise', function(done) {
      var returnValue = 5;
      var def = $q.defer();
      def.promise.then((val) => {
        expect(val).to.equal(42);
        done();
      }).catch(done);
      def.resolve(returnValue)
    });
})

describe('tests returning promises', function(){
    it('returns the correct value from promise', function() {
      var returnValue = 5;
      var def = $q.defer();
      def.resolve(returnValue)
      return def.promise.then((val) => {
        expect(val).to.equal(42);
      });
    });
})
  tests with done
    1) returns the correct value from promise

  tests returning promises
    2) returns the correct value from promise


  0 passing (15ms)
  2 failing

  1) tests with done returns the correct value from promise:
     Error: expected 5 to equal 42
      at Assertion.assert (node_modules/expect.js/index.js:96:13)
      at Assertion.be.Assertion.equal (node_modules/expect.js/index.js:216:10)
      at def.promise.then (tests/test.spec.js:9:24)
      at _fulfilled (node_modules/q/q.js:854:54)
      at self.promiseDispatch.done (node_modules/q/q.js:883:30)
      at Promise.promise.promiseDispatch (node_modules/q/q.js:816:13)
      at node_modules/q/q.js:570:49
      at runSingle (node_modules/q/q.js:137:13)
      at flush (node_modules/q/q.js:125:13)
      at _combinedTickCallback (internal/process/next_tick.js:67:7)
      at process._tickCallback (internal/process/next_tick.js:98:9)

  2) tests returning promises returns the correct value from promise:
     Error: expected 5 to equal 42
      at Assertion.assert (node_modules/expect.js/index.js:96:13)
      at Assertion.be.Assertion.equal (node_modules/expect.js/index.js:216:10)
      at def.promise.then (tests/test.spec.js:22:24)
      at _fulfilled (node_modules/q/q.js:854:54)
      at self.promiseDispatch.done (node_modules/q/q.js:883:30)
      at Promise.promise.promiseDispatch (node_modules/q/q.js:816:13)
      at node_modules/q/q.js:570:49
      at runSingle (node_modules/q/q.js:137:13)
      at flush (node_modules/q/q.js:125:13)
      at _combinedTickCallback (internal/process/next_tick.js:67:7)
      at process._tickCallback (internal/process/next_tick.js:98:9)

@gurdiga setTimeout() 아이디어에 κ°μ‚¬λ“œλ¦½λ‹ˆλ‹€! λΉ„μŠ·ν•œ λ¬Έμ œκ°€ μžˆμ—ˆμ§€λ§Œ 이제 μ΅œμ†Œν•œ μ μ ˆν•œ 였λ₯˜ λ©”μ‹œμ§€λ₯Ό 받을 수 μžˆμŠ΅λ‹ˆλ‹€!

λ‚΄ μ‹œλ‚˜λ¦¬μ˜€μ—μ„œλŠ” Nightmareλ₯Ό μ‚¬μš©ν•˜μ—¬ end2end ν…ŒμŠ€νŠΈλ₯Ό μˆ˜ν–‰ν–ˆμŠ΅λ‹ˆλ‹€. λ‚˜λ₯Ό μœ„ν•œ 해결책은 .catch(done) μ‚¬μš©ν•˜λŠ” κ²ƒμ΄μ—ˆμŠ΅λ‹ˆλ‹€. μ•„λž˜ μ˜ˆμ œμ™€ 같이 λ‹€λ₯Έ catch 콜백 λ‚΄μ—μ„œ done(error) ν˜ΈμΆœν•  수 μžˆμŠ΅λ‹ˆλ‹€.

describe('Clicking in any bad reputation tag', () => {
    it('open the bad reputation modal', (done) => {
      nightmare
        .select('#per-page', '50')
        .waitForAjax()
        .click('[data-reputation="bad"]')
        .evaluate(function() {
          return document.querySelector('.vue-modal .ls-modal-title').innerText
        })
        .then(function(title) {
          title.should.equal('Sua segmentação teve uma avaliação ruim!')
          done()
        })
        .catch((error) => {
          screenshot(nightmare)
          done(error)
        })
    })
  })

@itumoraes μž‘λ™ν•˜μ§€λ§Œ λŒ€μ‹  λ‹€μŒμ„ μˆ˜ν–‰ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

describe('Clicking in any bad reputation tag', () => {
    it('open the bad reputation modal', () => {
      return nightmare
        .select('#per-page', '50')
        .waitForAjax()
        .click('[data-reputation="bad"]')
        .evaluate(function() {
          return document.querySelector('.vue-modal .ls-modal-title').innerText
        })
        .then(function(title) {
          title.should.equal('Sua segmentação teve uma avaliação ruim!')
        })
    })
  })

Promiseλ₯Ό λ°˜ν™˜ν•˜λ©΄ done() λ₯Ό ν˜ΈμΆœν•  ν•„μš”κ°€ μ—†μŠ΅λ‹ˆλ‹€. Mocha, Express 및 Mongooseμ—μ„œ Async/Await μ‚¬μš©ν•˜κΈ° λ‚΄ λΈ”λ‘œκ·Έ κ²Œμ‹œλ¬Ό μ°Έμ‘°

μ•„λž˜ 슀크립트λ₯Ό μ‚¬μš©ν–ˆμ§€λ§Œ λ™μΌν•œ μ‹œκ°„ 초과 초과 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€.

마이슀크립트:

describe("getBillingDetail", 비동기 ν•¨μˆ˜(){
this.timeout(55000);
it.only("μ§€μ •λœ μœ νš¨ν•œ μž‘μ—… 이름 확인", 비동기 ν•¨μˆ˜(μ™„λ£Œ){
this.timeout(55000);
var κ²°κ³Ό = λŒ€κΈ° url.getBillingDetail('12254785565647858');
console.log(κ²°κ³Ό);
assert.equal(κ²°κ³Ό, μ°Έ);
});
});

였λ₯˜: 55000ms의 μ‹œκ°„ μ΄ˆκ³Όκ°€ μ΄ˆκ³Όλ˜μ—ˆμŠ΅λ‹ˆλ‹€. 비동기 ν…ŒμŠ€νŠΈ 및 ν›„ν¬μ˜ 경우 "done()"이 ν˜ΈμΆœλ˜μ—ˆλŠ”μ§€ ν™•μΈν•˜μ‹­μ‹œμ˜€. Promiseλ₯Ό λ°˜ν™˜ν•˜λŠ” 경우 ν•΄κ²°λ˜λŠ”μ§€ ν™•μΈν•˜μ‹­μ‹œμ˜€.

μ—¬λŸ¬ 개의 λ‹«νžŒ λ¬Έμ œμ—μ„œ λ™μΌν•œ 철자λ₯Ό μ€‘μ§€ν•˜μ‹­μ‹œμ˜€. μ™„λ£Œλœ μ½œλ°±μ„ 비동기 ν•¨μˆ˜μ— μ „λ‹¬ν•˜μ§€ λ§ˆμ‹­μ‹œμ˜€. 비동기 ν…ŒμŠ€νŠΈμ— λŒ€ν•œ λ¬Έμ„œ 읽기

@Munter μ™„λ£Œλœ μ½œλ°±μ„ μ œκ±°ν–ˆμ§€λ§Œ μ΄λŸ¬ν•œ 였λ₯˜κ°€ λ‹€μ‹œ λ°œμƒν•©λ‹ˆλ‹€.

λ‹Ήμ‹ μ˜ 약속은 κ²°μ½” ν•΄κ²°λ˜μ§€ μ•Šμ€ 것 κ°™μŠ΅λ‹ˆλ‹€.

이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰