test('adding floating point numbers', () => {
const value = 0.1 + 0.2;
expect(value).toBe(0.3);
expect(value).toBeCloseTo(0.3); // This works.
});
String ๊ฐ ๊ฒ์ฆ
tobe ๋๋ toEqual ๋ ์์ง๋ง, toMatch ๋ฅผ ์ฌ์ฉํ์ฌ ์ ๊ท์์ ๋ํ ๋ฌธ์์ด์ ํ์ธํ ์ ์๋ค.
test('there is no I in team', () => {
expect('team').not.toMatch(/I/);
});
test('but there is a "stop" in Christoph', () => {
expect('Christoph').toMatch(/stop/);
});
function compileAndroidCode() {
throw new Error('you are using the wrong JDK!');
}
test('compiling android goes as expected', () => {
expect(() => compileAndroidCode()).toThrow();
expect(() => compileAndroidCode()).toThrow(Error);
// You can also use a string that must be contained in the error message
// or a regexp
expect(() => compileAndroidCode()).toThrow('you are using the wrong JDK');
expect(() => compileAndroidCode()).toThrow(/JDK/);
// Or you can match an exact error message using a regexp like below
// Test fails
expect(() => compileAndroidCode()).toThrow(/^you are using the wrong JDK$/);
// Test pass
expect(() => compileAndroidCode()).toThrow(/^you are using the wrong JDK!$/);
});