κ΅¬μ± μμκ° κ°μ²΄, λ°°μ΄ λλ λ¬Έμμ΄μΈμ§ νμΈνλ λ°©λ²μ΄ μμ΅λκΉ? μ΄κ²μ chaiμ 'should.be.a'μ μ μ¬ν©λλ€. μ: validationResult.SSN[0].should.be.a('string').
μλμ, μμ΅λλ€. μ¬μ© κ°λ₯ν λͺ¨λ λ§€μ² λͺ©λ‘μ https://facebook.github.io/jest/docs/en/expect.html μμ μ°Ύμ μ μμ΅λλ€.
lodash
μ κ°μ μΌλ° JavaScript λλ λμ°λ―Έ λΌμ΄λΈλ¬λ¦¬λ₯Ό μ¬μ©ν μλ μμ΅λλ€.
test('name', () => {
// array
expect(Array.isArray(['value'])).toBe(true);
// string
expect(typeof 'value').toBe('string');
// object
expect({value: 'value'}).toBeTruthy();
expect(typeof {value: 'value'}).toBe('object');
})
μ¬μν μ - μ΄κ²μ μ½μ κ²°κ³Όμ λμμ΄ λμ§ μμ΅λλ€.
expect(somePromise).resolves.toBe(...)
μ΄ μμ μμ μ νμ νμΈν λ°©λ²μ΄ μμ΅λλ€. λ΄μ© μ΄ λ¬΄μμΈμ§ μ κ²½ μ°μ§ μκ³ λ¬Έμμ΄ μΈ κ²½μ°. expects.stringContaining("")
μ΄ ν΄κ²° λ°©λ²μ΄ λκΈ°λ₯Ό ν¬λ§νμ§λ§ κ·Έκ²λ μλνμ§ μμ΅λλ€.
@abritinthebay μ κ° μ νν κ·Έ μν©μ΄κ³ μ΄κ²μ΄ Googleμ 첫 λ²μ§Έ κ²°κ³Όμ λλ€. μλ§λ μ΄κ²μ λ€μ μ΄μ΄μΌ ν κΉμ?
λ¬Όλ‘ μ‘°κΈ λ μκ°ν΄λ΄μΌ ν©λλ€. λ΄ ν΄κ²° λ°©λ²μ typeof
λΆλΆμ ββμννλλ‘ μ²΄μΈμ μΆκ°νλ κ²μ
λλ€. μ:
expect(somePromise.then(data => typeof data)).resolves.toBe("object");
μλνμ§λ§ μμ ν κΉ¨λνμ§λ μμ΅λλ€.
@thymikee μ ν κ²μ¬λ μΆ©λΆν μΌλ°μ μΈ μ¬μ© μ¬λ‘(보νΈμ )μ΄λ―λ‘ μ΄λ¬ν μ νμ΄ μλ ν μ€νΈ νλ μμν¬μ λν λ³λͺ μ μ¬μ§κ° μμ΅λλ€. ν μ€νΈ μ€μΈ λ΄μ©μ λͺ¨λ 컨ν μ€νΈλ₯Ό μμ΄λ²λ¦¬λ―λ‘ κ·νμ λμμ νμ©λμ§ μμ΅λλ€.
μ΄ expect(Array.isArray(['value'])).toBe(false);
μ€ν¨ν©λλ€.
expect(received).toBe(expected)
Expected value to be (using ===):
false
Received:
true.
λ°λΌμ μ°λ¦¬λ λμ°ν μ£Όμ₯ λ©μμ§λ₯Ό λ°κ±°λ μ΄λ¬ν μ’ λ₯μ κ²μ¬λ₯Ό μ§μνλλ‘ Jestλ₯Ό νμ₯ν΄μΌ ν©λλ€. μ¬μ©νλ λͺ¨λ μ¬λμ΄ μ΄λ¬ν κΈ°λ₯μ μ€μ€λ‘ ꡬνν΄μΌ νλ κ²κ³Ό λ¬λ¦¬ Jestμ μ μ§ κ΄λ¦¬μκ° μ΄ μμ μ ν λ² μννλ κ²μ΄ λ ν©λ¦¬μ μ΄μ§ μμ΅λκΉ?
expect.extend
λ₯Ό μ¬μ©νμ¬ μμ λ§μ 맀μ²λ₯Ό λ§λ€κ³ npm λͺ¨λλ‘ κ²μν©λλ€. μΈκΈ°λ₯Ό μ»μΌλ©΄ κ²°κ΅ Jest μ½μ΄μ λ³ν©ν μ μμ΅λλ€. ;)
μνλ μ¬λλ€μ μν κ°λ¨ν toBeType νμ₯
expect.extend({
toBeType(received, argument) {
const initialType = typeof received;
const type = initialType === "object" ? Array.isArray(received) ? "array" : initialType : initialType;
return type === argument ? {
message: () => `expected ${received} to be type ${argument}`,
pass: true
} : {
message: () => `expected ${received} to be type ${argument}`,
pass: false
};
}
});
describe("testing extended expect", () => {
it("tests normal types correctly", () => {
expect("").toBeType("string");
expect({}).toBeType("object");
expect(1).toBeType("number");
});
it("tests array types correctly", () => {
expect([]).toBeType("array");
});
it("works with promises", () => {
expect(Promise.resolve([])).resolves.toBeType("array");
});
});
ꡬννκΈ°κ° λ§€μ° κ°λ¨ν©λλ€. μ λ§ ν΅μ¬ tbhμ μμ΄μΌν©λλ€.
μ°Έκ³ - ν΄λΉ νμ₯μ μ€μ νμΌμ λ£λ κ²½μ° setupTestFrameworkScriptFile
setupFiles
μλ setupFiles
μμ μν©λλ€(νμ₯μ μ μμμλ§ μ¬μ©ν μ μμΌλ―λ‘)
@abritinthebay κ°μ¬ν©λλ€
κ·Έλμ μ¬λλ€μ΄ μν κ²½μ° npm λͺ¨λλ‘ λννμ΅λλ€.
describe("assertion framework", ()=> {
it("should check primitive types", () => {
expect(expect.toBeA).toBeA("function")
})
})
μ€ν¨: expect(...).toBeAλ ν¨μκ° μλλλ€.TypeError: expect(...).toBeAλ ν¨μκ° μλλλ€.
https://github.com/jest-community/jest-extended μλ μνλ λͺ¨λ μ ν μΌμΉμκ° μμ΅λλ€(μ μκ°μλ).
ν
μ€νΈμμ toBeInstanceOf
νμ΅λλ€.
expect($wrapper.vm.countries).toBeInstanceOf(Array);
expect.extendλ₯Ό μ¬μ©νμ¬ μμ λ§μ 맀μ²λ₯Ό λ§λ€κ³ npm λͺ¨λλ‘ κ²μν©λλ€. μΈκΈ°λ₯Ό μ»μΌλ©΄ κ²°κ΅ Jest μ½μ΄μ λ³ν©ν μ μμ΅λλ€. ;)
λ€, κ·Έλ¦¬κ³ λΉμ μ΄ κ·Έκ²μ μλ λμ λΉμ λ§μ λλ΄ νλ μμν¬λ₯Ό μμ±ν μλ μμ΅λλ€.
μ΄κ²μ GitHubμμ μ»μ μ μλ μ΅μ μ λ΅λ³μΌ μ μμ΅λλ€.
κ·Έλμ @abritinthebay λ μ νν μννμ΅λλ€ (νμ€ ν μμ²λ³΄λ€ ν¨μ¬ λ λ§μμ΅λλ€).
μ΄μ κ·Έ μ©κ°ν μνΌμ΄ λͺ¨λ μμ μ μννμΌλ―λ‘ λ€λ₯Έ λΌμ΄λΈλ¬λ¦¬λ₯Ό μ€μΉνμ§ μκ³ λ λλ¨Έμ§ μ¬λλ€μ΄ λ§μΉ¨λ΄ μ΄ λ§€μ²λ₯Ό μ»μ μ μλ μκΈ°λ μΈμ μ λκΉ? μ μ§ κ΄λ¦¬μλ μ΄κ²μ΄ Jestμ μνμ§ μλλ€λ μμ΄λμ΄λ₯Ό κ³μ μΆμ§νκ³ μμ΅λκΉ? μλλ©΄ μ΄κ²μ΄ λ μ΄λμμ λ¨μ΄μ§ κ²μ λκΉ?
μ°λ¦¬λ κ·Έκ²μ ν΅μ¬μΌλ‘ λ§λλ κ²μ λν΄ λ§€μ° μ격νλ©° μΌλ°μ μΌλ‘ μ€ν μΌμΉμλ₯Ό μΆκ°νμ§ μμ΅λλ€. Jest μ½μ΄λ μλΉν ν° μν€ν μ²μ΄λ©° μ°λ¦¬κ° μΆκ°νλ 맀μ²λ§λ€ μ μ§ κ΄λ¦¬ λΉμ©μ΄ μ¦κ°ν©λλ€.
μ€νμ κ²½μ° μΌλ°μ μΌλ‘ https://github.com/jest-community/jest-extendedλ₯Ό κΆμ₯ν©λλ€.
ν μ¬λμ μ€νμ ν΅μ¬ λΌμ΄λΈλ¬λ¦¬μ μνλ λ€λ₯Έ μ¬λ(λλ μ΄ κ²½μ° μ΅μ 7λͺ μ λ€λ₯Έ μ¬λ)μ μ λ§ μ μ©νκ³ λ Όλ¦¬μ μΈ κΈ°λ₯μ λλ€.
λΆλͺ ν μ μ§ κ΄λ¦¬μΈμΌλ‘μ κ·νμ ν¬νλ μ°λ¦¬μ λͺ¨λ κ²λ³΄λ€ μ°μ νλ©° κ·νλ μ°λ¦¬κ° μκ°νμ§ μλ λͺ¨λ μ’ λ₯μ μ°λ €λ₯Ό κ°μ§κ³ μμΌλ―λ‘ μ λ κ·Έκ²μ μ μ μΌλ‘ μ‘΄μ€ν©λλ€. νμ§λ§ μ¬κΈ° μλ λͺ¨λ μ¬λλ€μ΄ μ μ΄ κΈ°λ₯μ΄ ν΅μ¬ λΌμ΄λΈλ¬λ¦¬μ μνλ€κ³ μκ°νλμ§ μ΄ν΄λ³΄μκΈ° λ°λλλ€. μ¬κΈ°μ νμκ° μμΌλ©° μ΄λ₯Ό 무μνλ©΄ Jest ν΅μ¬ λΌμ΄λΈλ¬λ¦¬ μ¬μ©μ(μμ§ν λ§ν΄μ 90%λ jest-extendedμ λν΄ λ€μ΄λ³΄μ§λ λͺ»ν κ²μ λλ€)λ₯Ό μκ² λ©λλ€.
.to.be.an.instanceOf
λ μΌλ§λ λ§μ μ¬μ©μκ° μ νμ νμΈνλ €κ³ μκ°νλμ§κ° μλλ―λ‘ ν΄λΉ μ¬μ©μμ κ²½μ° μ€νμΌλ‘ 보λλΌλ μΆκ° λΌμ΄λΈλ¬λ¦¬ μμ΄ Jestμμ μ νμ νμΈνλ κΈ°λ₯μ ν¨κ³Όμ μΌλ‘ κ±°λΆνλ κ²μ
λλ€.
λ€ λ€μ΄μ. λΆλͺ ν λ§ν΄μ, "μ€ν" μ λ μ½κ² μ½κ±°λ ννν μ μλλ‘ μ€κ³λ ꡬ문μ μλ―Ένμ΅λλ€. μ€νμ μ μμ μ΄λ―Έ μ‘΄μ¬ νλ κΈ°λ₯μ λ³νμ λλ€.
μ΄ κ²½μ° λ€μμ΄ μμ΅λλ€.
// Supported
expect(typeof foo).toBe('string');
// Proposed Sugar
expect(foo).toBeType('string');
λ°λΌμ μ ν κ²μ¬λ₯Ό μ§μνμ§ μλ κ²μ μλλλ€. μ°λ¦¬λνλ€. μ°λ¦¬λ 첫 λ²μ§Έ μ΅μ
μ μ§μν©λλ€. μ΄ μ΅μ
μ μ¬μ©μκ° μ’μ κ²½νμ ν μ μλλ‘ λ²κ·Έλ₯Ό μμ νκ³ λ©μμ§λ₯Ό μ‘°μ νλ λ° λ§μ μκ°μ λ³΄λΈ ν΅μ¬ toBe
맀μ²λ₯Ό μ¬μ©ν©λλ€.
jest-extendedμλ κ±°μ 60κ°μ matchersκ° μμΌλ©° κ·Έ μ€ λ§μ κ²μ΄ μμν μ€νμ λλ€. μ΄λ¬ν 맀μ²μ κ²½μ° μ΅μν 7λͺ μ λ€λ₯Έ μ¬λλ€μ΄ μ λ§ μ μ©νλ€κ³ μκ°ν μ μμΌλ―λ‘ μ½μ΄μ μΆκ°νλ λ° μ¬μ©ν κ²½νμ λ°©λ²μ΄λΌλ©΄ μλ§ λ§€μ²λ₯Ό μ μ§ κ΄λ¦¬νλ λ° λͺ¨λ μκ°μ ν μ ν κ²μ λλ€.
μμ ν 곡μ νκΈ° μν΄ - λλΆλΆμ 맀μ²λ μ΄λ μ λλ "μ€ν"μ
λλ€. λ΄ λ§μ toBeGreaterThanOrEqual
λ expect(foo >= bar).toBe(true);
λν μ€νμΌ λΏμ
λλ€.
맀μ²λ μ€μ λ‘ _κ±°μ λͺ¨λ _ λΆμΈ λ¬Έμ λν μ€νμ λλ€. ;)
(νλ κ²μ΄ μλλΌ λ¨μ§ κ·Έκ²μ΄... λ§€μ° νλ¦Ών μ μ΄λΌλ κ²μ μ§μ νκΈ° μν΄ μ΄ λ§μ νλ κ²μ λλ€.)
abritinthebayκ° μ μν κ²μ²λΌ μ€νμ κ΄ν κ²μ΄ μλλΌ "νμ" λ° "λΆνμν"(ν΅μ¬ λΌμ΄λΈλ¬λ¦¬μ κ²½μ°) μ€νμ κ΄ν κ²μ λλ€. μ΄ μ€λ λμμ "μ΄λ΄, λͺ¨λ μ νμ νμΈν μ μλ€λ κ²μ ν μ€νΈ λΌμ΄λΈλ¬λ¦¬μ ν΅μ¬μ μμ΄μΌ νλ κ²"μ΄λΌκ³ λ§νλ λ§μ μ¬λλ€μ΄ μμ΅λλ€(μ¦, νμν©λλ€).
μ°λ¦¬μ λ§μ λ£κ±°λ λ£μ§ λ§μμμ€. λ€μ ν λ² μ μ§ κ΄λ¦¬μλ‘μ λ€λ₯Έ λ§μ μ°λ € μ¬νμ΄ μμ΅λλ€. νμ§λ§ λ΄ μκ°μ κ·Έκ²μ΄ λ΄μ¬μ μ΄μ§ μμ λ "λΉμ μ κ²μ λ³Έμ§μ μΌλ‘ λΆνμν μ€νμ λλ€"λΌκ³ λ§νλ κ²μ΄ μ¬λ°λ₯Έ μλ΅μ΄λΌκ³ μκ°νμ§ μμ΅λλ€. λͺ¨λ μ νμ νμΈνκ±°λ μ¦μ νμΈν μ μμ΅λλ€.
μ΄λμ, μ΄λ ΅μ§ μμμ :P?
expect(Array.isArray(['your', 'array'])).toBe(true);
expect(typeof something === "object").toBe(true);
// - or -
expect(something instanceof Object).toBe(true);
expect(typeof something === "string").toBe(true);
@nahumzs μλνλ λμ λ¬Έμ λ ν μ€νΈ μΆλ ₯μμ ββμ€ν¨ν λ 'κ±°μ§μ΄ μ¬μ€μΌ κ²μΌλ‘ μμλ¨'μ΄λΌκ³ νμλμ΄ κ·Έλ€μ§ λμμ΄ λμ§ μλλ€λ κ²μ λλ€. ;)
κ°λ κΈΈμΈ κ² κ°μμ :)
describe('type check', () => {
test('should be type string', () => {
expect(typeof '').toBe('string')
})
test('should be type number', () => {
expect(typeof 10).toBe('number')
})
test('should be type boolean', () => {
expect(typeof true).toBe('boolean')
})
test('should be type undefined', () => {
expect(typeof undefined).toBe('undefined')
})
test('should be type object', () => {
expect(typeof { foo: 'bar' }).toBe('object')
})
test('should be type function', () => {
expect(typeof function() {}).toBe('function')
})
test('should be type null', () => {
expect(typeof null).toBe('object')
})
})
@abritinthebayμμ μ 곡νλ ꡬνμ 리ν©ν λ§νμ΅λλ€. μμ νκΈ°μλ μ‘°κΈ νΈν κ² κ°μ΅λλ€.
```μλ°μ€ν¬λ¦½νΈ
κΈ°λ.νμ₯({
/ ** @param { } μμ λ¨
* @param {λ¬Έμμ΄|λ¬Έμμ΄[]} μΈμ
* @return {{ν΅κ³Ό:λΆμΈ, λ©μμ§:(ν¨μ():λ¬Έμμ΄)}}
*/
toBeType(μμ , μΈμ) {
μμ isCorrectType = μΈμ => {
const receiveType = μμ λ μ ν;
const checkForSingle = arg => {
const type = receivedType === 'object'
? Array.isArray(received)
? 'array'
: receivedType
: receivedType;
return type === arg;
};
const checkForArr = arg => {
const reducer = (prev, curr) => prev || isCorrectType(curr).isCorrect;
return arg.reduce(reducer, false);
};
return {
receivedType,
isCorrect: Array.isArray(arg)
? checkForArr(arg)
: checkForSingle(arg)
};
};
const {isCorrect, receivedType} = isCorrectType(arg);
return {
pass: isCorrect,
message: () => {
const toBe = Array.isArray(arg)
? arg.join(`' or '`)
: arg;
return `Expected '${received}' of '${receivedType}' type to be of '${toBe}' type(s)`;
}
};
}
});
λ΄ λͺ¨λ(μμ λ§ν¬λ¨)μ νμΈν΄μΌ ν©λλ€. κ·Έκ²μ κ·Έλ³΄λ€ μ‘°κΈ λν©λλ€. κ·Έλ¬λ κ·Έκ²μ΄ λΉμ μ μν΄ μλνλ€λ©΄: κ·Έκ²μ μ¬μ©νμμμ€!
κ°λ κΈΈμΈ κ² κ°μμ :)
describe('type check', () => { test('should be type string', () => { expect(typeof '').toBe('string') }) test('should be type number', () => { expect(typeof 10).toBe('number') }) test('should be type boolean', () => { expect(typeof true).toBe('boolean') }) test('should be type undefined', () => { expect(typeof undefined).toBe('undefined') }) test('should be type object', () => { expect(typeof { foo: 'bar' }).toBe('object') }) test('should be type function', () => { expect(typeof function() {}).toBe('function') }) test('should be type null', () => { expect(typeof null).toBe('object') }) })
κ·Έκ²μ 맀λ ₯μ²λΌ μλνλ©° λ―Έλλ₯Ό μν΄ λ μ½κΈ° μ½κ³ μ μ§ λ³΄μκ° μ½μ΅λλ€.
κ°λ κΈΈμΈ κ² κ°μμ :)
describe('type check', () => { test('should be type string', () => { expect(typeof '').toBe('string') }) test('should be type number', () => { expect(typeof 10).toBe('number') }) test('should be type boolean', () => { expect(typeof true).toBe('boolean') }) test('should be type undefined', () => { expect(typeof undefined).toBe('undefined') }) test('should be type object', () => { expect(typeof { foo: 'bar' }).toBe('object') }) test('should be type function', () => { expect(typeof function() {}).toBe('function') }) test('should be type null', () => { expect(typeof null).toBe('object') }) })
test('should be type object', () => {
expect(typeof { foo: 'bar' }).toBe('object')
// passes
expect(typeof ['foo', 'bar']).toBe('object')
// passes
expect(typeof null).toBe('object')
})
π
μ΄κ²μ΄ λ΄κ° μμ μ λμ¨μ μ μνλ μ΄μ μ λλ€. μ΄κ²μ μ²λ¦¬ν©λλ€.
InstanceOfκ° μ½κ° λ λ«μ§λ§ λΉμ·ν λ¬Έμ κ° λ°μνκΈ° μ½μ΅λλ€.
λ§ν¬:
μ루μ @abritinbayμ κ°μ¬λ립λλ€
λ λ€λ₯Έ μ루μ :
expect('example').toEqual(expect.any(String));
expect(123).toEqual(expect.any(String));
λ λ²μ§Έλ λ€μκ³Ό κ°μ΄ μ€ν¨ν©λλ€.
Expected: Any<String>
Received: 123
κ°μ₯ μ μ©ν λκΈ
μνλ μ¬λλ€μ μν κ°λ¨ν toBeType νμ₯
ꡬννκΈ°κ° λ§€μ° κ°λ¨ν©λλ€. μ λ§ ν΅μ¬ tbhμ μμ΄μΌν©λλ€.
μ°Έκ³ - ν΄λΉ νμ₯μ μ€μ νμΌμ λ£λ κ²½μ°
setupTestFrameworkScriptFile
setupFiles
μλsetupFiles
μμ μν©λλ€(νμ₯μ μ μμμλ§ μ¬μ©ν μ μμΌλ―λ‘)