Skip to content

Instantly share code, notes, and snippets.

@kenmori
Last active August 1, 2021 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenmori/0fa7426b2719f337eca5dcaa40f43068 to your computer and use it in GitHub Desktop.
Save kenmori/0fa7426b2719f337eca5dcaa40f43068 to your computer and use it in GitHub Desktop.
answer

jest

import { add, addString, toNumber, filterOrange } from "./index";

it('adds 1 + 2 to equal 3', () => {
  expect(add(1, 2)).toBe(3);
});


it('adds undefined + 2 to equal NaN', () => {
  expect(add(undefined, 2)).toBe(NaN);
});


describe("addString", () => {
  it("passed parameter param and param2 as string, it should be ab", () => {
    const param = "a";
    const param2 = "b";

    const result = addString(param, param2);

    expect(result).toBe("ab")
  })

  it("passed parameter param and param2 as number, it should be -1", () => {
    const param = 1;
    const param2 = 1;

    const result = addString(param, param2);

    expect(result).toBe(-1)
  })
})


describe("toNumber", () => {
  it("passed parameter a and b as number, it should be -1", () => {
    const param = "-1";

    const result = toNumber(param);

    expect(result).toBe(-1)
  })

  it("passed parameter as string, it should be NaN", () => {
    const param = "aaa";

    const result = toNumber(param);

    expect(result).toBe(NaN)
  })

  it("passed parameter 10000desu as string, it should be 10000", () => {
    const param = "10000desu";

    const result = toNumber(param);

    expect(result).toBe(10000)
  })
})


describe("filterOrange", () => {
  it("passed parameter array include orange, it should be ['orange']", () => {
    const fruits = ["orange", "apple", "peach"];

    const result = filterOrange(fruits);

    expect(result).toStrictEqual(["orange"])
    // toEqualでも可
  })

  it("passed parameter array include orange, it should be empty arry", () => {
    const fruits = ["pine", "apple", "peach"];

    const result = filterOrange(fruits);

    expect(result).toStrictEqual([])
    // toEqualでも可
  })
})

toEqualとtoStrictEqualの違い

  • toEqualはオブジェクトのkeyまで見ない。同等なら通る

toStrictEqualは、2つのオブジェクトに同じデータが含まれていることだけでなく、それらが同じ構造を持っていることもチェックします。 オブジェクトが同じ値を持つだけでなく、同じキーを持つことを期待するのが一般的です。 より厳密な等式は、2つのオブジェクトが同一のキーを持たない場合をキャッチします。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment