λ€μ ν μ€νΈ
async function check() {
throw new Error('Test');
}
expect(async () =>
await check()
).toThrow();
μ€ν¨νκ³ μ€λ₯λ₯Ό μ λλ‘ ν¬μ°©νμ§ λͺ»ν©λλ€ (μ΅μ λ²μ μ jestλ₯Ό μ¬μ©νκ³ μμ΅λλ€)
μ, νμ¬ μ§μλμ§ μμ΅λλ€. https://github.com/facebook/jest/issues/1377 μ°Έμ‘°
λμ μ
expect(async () => await check()).toThrow()
// Jest error: "Expected the function to throw an error. But it didn't throw anything."
μ΄κ²μ μλν©λλ€:
expect(check()).rejects.toEqual(new Error('Test'))
λ€ λ¬Έλ²μ΄ μ΄μν©λλ€...
λ€μκ³Ό κ°μ΄ μμ±ν μλ μμ΅λλ€.
let error;
try {
await check();
} catch (e) {
error = e;
}
expect(error).toEqual(new Error('Test'));
λ€μλ μ μκ² ν¨κ³Όμ μ΄μμ΅λλ€.
async function check() {
throw new Error('Test');
}
expect(check()).rejects.toEqual(new Error('Test'))
μ°Έμ‘°: https://facebook.github.io/jest/docs/en/tutorial-async.html#rejects
ν΄κ²° λ°©λ² λͺ©λ‘μ μΆκ°ν©λλ€.
νΉμ μ€λ₯ μ νμ ν¬μ°©νκ³ λ©μμ§μ μ κ²½ μ°μ§ μμΌλ €λ©΄ λ€μμ μννμμμ€.
async function check() {
throw new SomeSpecificError('Whatever');
}
await check()
.then(() => {
throw new Error('Should go to .catch, not enter .then');
})
.catch((err) => {
expect(err).toBeInstanceOf(SomeSpecificError);
});
await expect(check).rejects.toThrow(SomeSpecificError);
λ μλν΄μΌν©λλ€
await expect(check).rejects.toThrow(/DUPLICATES_DETECTED/);
λ΄κ° νλ μΌμ΄λ€.
async function check() {
throw new Error("Test");
}
await expect(check).rejects.toThrow(Error);
expect(received).rejects.toThrow()
received value must be a Promise.
Received:
function: [Function check]
μ€μ μ½μμ μ 곡νλ €λ©΄ νΈμΆν΄μΌ ν©λλ€.
async function check() {
throw new Error("Test");
}
await expect(check()).rejects.toThrow(Error);
λ μλν΄μΌν©λλ€
it(`some test`, async () => {
async function check() {
try {
return Promise.reject(await asyncMethod());
} catch (error) {
throw new Error("test");
}
}
await expect(check()).rejects.toThrow(Error);
});
μΌν μμλ€
λ²μ
23.4.2
23.1.2
λλ κ·Έκ²μ΄ μ½μμ΄ νμνλ€λ κ²μ μκ³ μμ§λ§ λΉμ μ΄ μ μνλ κ²½μ°λ κ·Έλ μ§ μμ΅λλ€ :)
μλ νμΈμ ! λ΄κ° λκ° μλͺ»νκ³ μμ μλ μμ§λ§ λΉλκΈ° νΈμΆμμ μ¬μ ν μ¬μ©μ μ§μ μ€λ₯μ λ¬Έμ κ° μμ΅λλ€. λ€μμ κ³ λ €νμΈμ:
class CustomErrorType {}
// ...
test('check throws custom error', async () => {
async function check() {
throw new CustomErrorType();
}
await expect(check()).rejects.toThrow(CustomErrorType);
});
λ€μ μΆλ ₯κ³Ό ν¨κ» ν μ€νΈκ° μ€ν¨ν©λλ€.
ν¨μμμ λ€μ μ νμ μ€λ₯κ° λ°μν΄μΌ ν©λλ€.
"μ¬μ©μ μ μ μ€λ₯ μ ν"
νμ§λ§ μ무κ²λ λμ§μ§ μμμ΅λλ€.
λ°λΌμ ν
μ€νΈλ μ€ν¨νμ§λ§ λμ Έμ§ ν΄λμ€κ° Error
λ μλ²½νκ² μλν©λλ€ . Error
λ₯Ό νμ₯νλ €κ³ ν λ
```μλ°μ€ν¬λ¦½νΈ
ν΄λμ€ CustomErrorType νμ₯ μ€λ₯ {}
````
κ·Έλ° λ€μ μ€λ₯λ
ν¨μμμ λ€μ μ νμ μ€λ₯κ° λ°μν΄μΌ ν©λλ€.
"μ¬μ©μ μ μ μ€λ₯ μ ν"
λμ λ€μμ λμ‘μ΅λλ€.
μ€λ₯
ν΄λΉ μνμμ 무μμ΄ μλͺ»λμλμ§μ λν λ¨μκ° μμ΅λκΉ? λλ λλ΄ 23.4.2λ₯Ό μ¬μ©νκ³ μμ΅λλ€.
@Marchelune class CustomErrorType extends Error {}
λ₯Ό μμ±ν λ μ€μ λ‘ μ ν΄λμ€λ₯Ό μ μνμ§ μμ κ²μ
λλ€. CustomErrorTypeμ λ³Έλ¬Έμ μ 곡νλ©΄ super()λ₯Ό νΈμΆνλ κ² μΈμλ μ무κ²λ νμ§ μλ μμ±μλΌλ μλν©λλ€.
λκ΅°κ°κ° μΈμ€ν΄μ€ Error ν΄λμ€μ λ€λ₯Έ κ²μ λμ§κ³ κ·Έ μ€λ λμ μ‘°μΈμ μ¬μ©νκΈ° μν΄ κ³ κ΅°λΆν¬νλ κ²½μ° (λ΄κ° κ·Έλ¬λ κ²μ²λΌ).
toThrow()
λ Error ν΄λμ€μ μΈμ€ν΄μ€μΈ throwλ κ°μ νμΈνκ³ κ·Έλ μ§ μμ κ²½μ° throwλ₯Ό κ°μ§νμ§ μμ΅λλ€. μ΄κ²μ λ¬Έμμ μμμμ λΆλͺ
νμ§ μμμ΅λλ€.
λΆλͺ
ν λ€μμ λμ§λλΌλ μ€ν¨ν©λλ€.
async function f() {throw 'aa'}
const res = await expect(f()).rejects.toThrow()`
κ·Έλ¬λ μ΄κ²μ μλν©λλ€(λ λμ λ°©λ²μ΄ μλμ§ νμ€νμ§ μμ).
async function f() {throw 'aa'}
const res = await expect(f()).rejects.toBeTruthy()`
@Karabur μ€ν toBeThruthy
λ toBeTruthy
μ¬μΌ ν©λλ€.
λ€μμ νΉμ μ€λ₯κ° λ°μνμ§ μκ³ μΌμΉνμ§ μμΌλ©΄ νμ€ν μ€λ¨λ κ²μμ 보μ₯νλ λͺ μμ ν μ€νΈμ λλ€.
yourCoolAsyncMethod('yeah it is')
.then(ok => { // Presumes not a Promise<void>
expect(ok).toBeUndefined();
done();
})
.catch(bad => {
expect(bad).toBeDefined();
expect(bad).toMatchInlineSnapshot(
`Specifically serious error!`
);
done();
});
μλ
νμΈμ @SimenBμ
λλ€.
λ΄κ° μ΄κ²μ κ°μ§κ³ μκ³ κ·Έκ²μ΄ μλνλ€λ©΄.
async function check() {
// do something
}
await expect(check()).rejects.toThrow(InternalServerErrorException);
InternalServerErrorExceptionμ΄ κ°μ²΄μ ν¨κ» λ°μνμ΅λλ€....
λν λ°μν μ€λ₯μ λν μμ±μ μ£Όμ₯νλ €λ©΄ μ΄λ»κ² ν΄μΌ ν©λκΉ? μλ₯Ό λ€μ΄:
λ€μμ νΉμ μ€λ₯κ° λ°μνμ§ μκ³ μΌμΉνμ§ μμΌλ©΄ νμ€ν μ€λ¨λ κ²μμ 보μ₯νλ λͺ μμ ν μ€νΈμ λλ€.
yourCoolAsyncMethod('yeah it is') .then(ok => { expect(ok).toBeUndefined(); done(); }) .catch(bad => { expect(bad).toBeDefined(); expect(bad).toMatchInlineSnapshot( `Specifically serious error!` ); done(); });
μ΄κ²μ λν λ¬Έμ λ μ½λμ μ€λ₯κ° μλ κ²½μ° ν μ€νΈλ₯Ό ν΅κ³Όνλ€λ κ²μ λλ€.
λ°λΌμ μ΄ ν μ€νΈλ ν μ€νΈ μ€μΈ μ½λμ μ€λ₯κ° μλμ§ μ¬λΆμ λ°λΌ μμλλ κ²°κ³Όκ° λ€λ₯΄κΈ° λλ¬Έμ μ€μ λ‘ λ§μ κ°μΉλ₯Ό μ 곡νμ§ μμ΅λλ€.
ν μ€νΈμλ μμ νλ¦ λλ μμ νλ¦μ΄ μμ΄μΌ νλ©° ν μ€νΈμ νμν κ²λ§ ν μ€νΈν΄μΌ ν©λλ€. μ΄λ° μ’ λ₯μ 무μμμ±μ κ°μ₯ ν¬μ§ μμ΅λλ€.
μλ νμΈμ @David-Tennant π
μ§μ ν΄μ£Όμ
μ κ°μ¬ν©λλ€. κ·Έ λΉμ λλ Promise<notVoid>
λ°©μμΌλ‘ μμ
νκ³ μμμ΅λλ€. Promise<void>
μλ μ ν¨ν κ³΅ν΅ μ루μ
μ 곡μ νμ§ μμλ€λ κ²μ κΉ¨λ¬μμ΅λλ€. λλ κ°μ μ λ§λ€μλ€λ μ견μΌλ‘ λ΅λ³μ μ
λ°μ΄νΈνμ΅λλ€. νλ¦μ λν΄μλ λμν©λλ€.
κ°μ¬ν©λλ€ π¨
μλ @futuredayv π
μλ νμΈμ @David-Tennant π
μ§μ ν΄μ£Όμ μ κ°μ¬ν©λλ€. κ·Έ λΉμ λλ
Promise<notVoid>
λ°©μμΌλ‘ μμ νκ³ μμμ΅λλ€.Promise<void>
μλ μ ν¨ν κ³΅ν΅ μ루μ μ 곡μ νμ§ μμλ€λ κ²μ κΉ¨λ¬μμ΅λλ€. λλ κ°μ μ λ§λ€μλ€λ μ견μΌλ‘ λ΅λ³μ μ λ°μ΄νΈνμ΅λλ€. νλ¦μ λν΄μλ λμν©λλ€.κ°μ¬ν©λλ€ π¨
λ¬Έμ λ ν
μ€νΈκ° 무ν¨λ₯Ό λ°ννλλΌλ ν
μ€νΈκ° μ¬μ ν ν΅κ³Όν κ²μ΄λΌλ μ μ
λλ€. μ΄λ κ²μ΄ λ²κ·Έμ¬μΌ ν©λκΉ?
done()
νΈμΆνλ©΄ "λ΄ ν
μ€νΈκ° μμλλ‘ ν΅κ³ΌλμμΌλ©° λ€μ ν
μ€νΈλ‘ λμ΄κ° μ€λΉκ° λμμ΅λλ€"λ₯Ό μλ―Έν©λλ€.
yourCoolAsyncMethod('yeah it is')
// -- Lets say your method does not throw, and returns void instead
.then(ok => { // Presumes not a Promise<void>
expect(ok).toBeUndefined(); // -- At this point if it was void, this would pass
done(); // -- Then your test would end and the catch would not be reached
// -- Now you have unknown behavior passing a test where it's suppose to catch a fail
})
.catch(bad => {
// -- This block does not run because done() is called above
expect(bad).toBeDefined();
expect(bad).toMatchInlineSnapshot(
`Specifically serious error!`
);
done();
});
μ¬μ© μ¬λ‘μ λν μ루μ
μ΄ λ¬΄μμΈμ§ νμ€νμ§ μμ΅λλ€. νμ§λ§ νΈμΆνμ§ μμμΌλ‘μ¨ μμν κ² done()
μλ₯Ό then
μλλ©΄ νμΈ ν
μ€νΈκ° μ€ν¨ νμΈνκΈ° μν΄ "λ€μ"μμ μ€λ₯λ₯Ό λμ§κ³ ,
yourCoolAsyncMethod('yeah it is')
.then(ok => throw new Error("This block of code should not be reached")
.catch(bad => {
expect(bad).toBeDefined();
expect(bad).toMatchInlineSnapshot(
`Specifically serious error!`
);
done();
});
λκ΅°κ°κ° μΈμ€ν΄μ€ Error ν΄λμ€μ λ€λ₯Έ κ²μ λμ§κ³ κ·Έ μ€λ λμ μ‘°μΈμ μ¬μ©νκΈ° μν΄ κ³ κ΅°λΆν¬νλ κ²½μ° (λ΄κ° κ·Έλ¬λ κ²μ²λΌ).
toThrow()
λ Error ν΄λμ€μ μΈμ€ν΄μ€μΈ throwλ κ°μ νμΈνκ³ κ·Έλ μ§ μμ κ²½μ° throwλ₯Ό κ°μ§νμ§ μμ΅λλ€. μ΄κ²μ λ¬Έμμ μμμμ λΆλͺ νμ§ μμμ΅λλ€.
λΆλͺ ν λ€μμ λμ§λλΌλ μ€ν¨ν©λλ€.async function f() {throw 'aa'} const res = await expect(f()).rejects.toThrow()`
κ·Έλ¬λ μ΄κ²μ μλν©λλ€(λ λμ λ°©λ²μ΄ μλμ§ νμ€νμ§ μμ).
async function f() {throw 'aa'} const res = await expect(f()).rejects.toBeTruthy()`
μ½κ° λ μ’μ λ°©λ²μ μ¬μ©νλ κ²μ
λλ€ toBeDefined()
λμ toBeTruthy()
:
async function f() {throw 'aa'}
const res = await expect(f()).rejects.toBeTruthy()`
κ°μ₯ μ μ©ν λκΈ
λμ μ
μ΄κ²μ μλν©λλ€:
λ€ λ¬Έλ²μ΄ μ΄μν©λλ€...