Skip to content

Instantly share code, notes, and snippets.

@alternacrow
Last active August 3, 2023 12:57
Show Gist options
  • Save alternacrow/66ccf83ab36b8a71b75d8d2f62bc4401 to your computer and use it in GitHub Desktop.
Save alternacrow/66ccf83ab36b8a71b75d8d2f62bc4401 to your computer and use it in GitHub Desktop.
Jest

Jest

Memory Leak

[Bug]: Memory consumption issues on Node JS 16.11.0+ #11956
jestjs/jest#11956

Mock

wip

Jest

Setup / Teardown / Order of Execution

https://jestjs.io/ja/docs/setup-teardown

  • 全ての describe 内の handler は、テストの実行前に実行される。
  • beforeAll: 全てのテストの開始前に実行される。
  • afterAll: 全てのテストの終了後に実行される。
  • beforeEach: 各テストの開始前に実行される。
  • afterEach: 各テストの終了後に実行される。
実行順序の例その1
console.log("===start===");

beforeAll(() => {
  console.log("beforeAll:1");
});

afterAll(() => {
  console.log("afterAll:1");
});

beforeEach(() => {
  console.log("beforeEach:1");
});

afterEach(() => {
  console.log("afterEach:1");
});

describe("describe", () => {
  console.log("describe:1");

  test("test:a", () => {
    console.log("test:a");
  });

  test("test:b", () => {
    console.log("test:b");
  });

  console.log("describe:2");
});

console.log("=== end ===");
===start===
describe:1
describe:2
=== end ===

beforeAll:1
  beforeEach:1
    test:a
  afterEach:1
  beforeEach:1
    test:b
  afterEach:1
afterAll:1
実行順序の例その2
console.log("===start===");

beforeAll(() => {
  console.log("beforeAll:global");
});
afterAll(() => {
  console.log("afterAll:global");
});
beforeEach(() => {
  console.log("beforeEach:global");
});
afterEach(() => {
  console.log("afterEach:global");
});

describe("describe:a", () => {
  console.log("describe:a1");

  beforeAll(() => {
    console.log("beforeAll:a");
  });
  afterAll(() => {
    console.log("afterAll:a");
  });
  beforeEach(() => {
    console.log("beforeEach:a");
  });
  afterEach(() => {
    console.log("afterEach:a");
  });

  console.log("describe:a2");

  test("test:a1", () => {
    console.log("test:a1");
  });
  test("test:a2", () => {
    console.log("test:a2");
  });

  describe("describe:b", () => {
    console.log("describe:b1");

    beforeAll(() => {
      console.log("beforeAll:b");
    });
    afterAll(() => {
      console.log("afterAll:b");
    });
    beforeEach(() => {
      console.log("beforeEach:b");
    });
    afterEach(() => {
      console.log("afterEach:b");
    });

    console.log("describe:b2");

    test("test:b1", () => {
      console.log("test:b1");
    });
    test("test:b2", () => {
      console.log("test:b2");
    });

    console.log("describe:b3");
  });

  console.log("describe:a3");
});

console.log("=== end ===");
===start===
describe:a1
describe:a2
describe:b1
describe:b2
describe:b3
describe:a3
=== end ===

beforeAll:global
  beforeAll:a

    beforeEach:global
      beforeEach:a
        test:a1
      afterEach:a
    afterEach:global

    beforeEach:global
      beforeEach:a
        test:a2
      afterEach:a
    afterEach:global

    beforeAll:b

      beforeEach:global
        beforeEach:a
          beforeEach:b
            test:b1
          afterEach:b
        afterEach:a
      afterEach:global

      beforeEach:global
        beforeEach:a
          beforeEach:b
            test:b2
          afterEach:b
        afterEach:a
      afterEach:global

    afterAll:b

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