Jest: 개체, λ°°μ—΄ λ˜λŠ” λ¬Έμžμ—΄μΈμ§€ ν…ŒμŠ€νŠΈν•©λ‹ˆλ‹€.

에 λ§Œλ“  2017λ…„ 05μ›” 03일  Β·  29μ½”λ©˜νŠΈ  Β·  좜처: facebook/jest

ꡬ성 μš”μ†Œκ°€ 개체, λ°°μ—΄ λ˜λŠ” λ¬Έμžμ—΄μΈμ§€ ν™•μΈν•˜λŠ” 방법이 μžˆμŠ΅λ‹ˆκΉŒ? 이것은 chai의 'should.be.a'와 μœ μ‚¬ν•©λ‹ˆλ‹€. 예: validationResult.SSN[0].should.be.a('string').

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

μ›ν•˜λŠ” μ‚¬λžŒλ“€μ„ μœ„ν•œ κ°„λ‹¨ν•œ 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 μ—μ„œ μ›ν•©λ‹ˆλ‹€(ν™•μž₯은 μ „μžμ—μ„œλ§Œ μ‚¬μš©ν•  수 μžˆμœΌλ―€λ‘œ)

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

μ•„λ‹ˆμš”, μ—†μŠ΅λ‹ˆλ‹€. μ‚¬μš© κ°€λŠ₯ν•œ λͺ¨λ“  맀처 λͺ©λ‘μ€ 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 λͺ¨λ“ˆλ‘œ λž˜ν•‘ν–ˆμŠ΅λ‹ˆλ‹€.

https://www.npmjs.com/package/jest-tobetype

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κ°€ μ•½κ°„ 더 λ‚«μ§€λ§Œ λΉ„μŠ·ν•œ λ¬Έμ œκ°€ λ°œμƒν•˜κΈ° μ‰½μŠ΅λ‹ˆλ‹€.

링크:

https://www.npmjs.com/package/jest-tobetype

μ†”λ£¨μ…˜ @abritinbay에 κ°μ‚¬λ“œλ¦½λ‹ˆλ‹€

또 λ‹€λ₯Έ μ†”λ£¨μ…˜:

expect('example').toEqual(expect.any(String));
expect(123).toEqual(expect.any(String));

두 λ²ˆμ§ΈλŠ” λ‹€μŒκ³Ό 같이 μ‹€νŒ¨ν•©λ‹ˆλ‹€.

    Expected: Any<String>
    Received: 123
이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰