μ΄ ν μ€νΈ:
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()
κ° μ€νλμ§ μκ³ μ΄λ° μ’
λ₯μ μ½λλ₯Ό ν
μ€νΈνλ λ μ’μ λ°©λ²μ΄ μλμ§ κΆκΈν©λλ€.
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:
.on("error", function(error) {...})
)μμ μμ λλλ‘ λμ΄ μλ κ²½μ°, μμ νμ§ μλ ν Mochaμ λλ¬νμ§ μμ΅λλ€. 리μ€λλ μ€λ₯μ ν¨κ» done
λ₯Ό νΈμΆν©λλ€(λλ 리μ€λκ° μ²« λ²μ§Έ 맀κ°λ³μλ‘ μ€λ₯λ₯Ό μ λ¬λ°μ κ²½μ° done
λ₯Ό 리μ€λμ ν¨κ» μ¬μ©ν©λλ€. μ: .on("error", done)
. μλ§λ κ·Έκ²μ λν Promiseμ .catch(done)
μ κ°μ΄ μ΄λ²€νΈ νΈλ€λ¬λΉ ν λ²μ΄ μλλΌ ν
μ€νΈλΉ ν λ² μμ±ν΄μΌ ν©λλ€."end"
/ "drain"
μ΄λ²€νΈλ₯Ό μ¬μ©νμ¬ λ€λ₯Έ μ΄λ²€νΈμ λΆμΈμ΄ μ€μ λμλμ§ νμΈν©λλ€.μ£μ‘ν©λλ€. κ·νμ 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 μλ£λ μ½λ°±μ μ κ±°νμ§λ§ μ΄λ¬ν μ€λ₯κ° λ€μ λ°μν©λλ€.
λΉμ μ μ½μμ κ²°μ½ ν΄κ²°λμ§ μμ κ² κ°μ΅λλ€.
κ°μ₯ μ μ©ν λκΈ
λλ λΉμ·ν λ¬Έμ μ λΆλͺμ³€κ³ κ²°κ΅ μ½μμ΄ μλ λΉλκΈ° ν¨μλ₯Ό ν μ€νΈν λ doneμ μ¬μ©ν΄μλ μ λλ€λ κ²μ κΉ¨λ¬μμ΅λλ€. λμ μ½μμ λ°ννμμμ€. λ°λΌμ μκ° μ΄κ³Ό μμ΄ μ΄ μμ μ μνν μ μμ΄μΌ ν©λλ€. μ: