๐ŸงŠ

Result outputs go wrong with using async tasks in `{before,after}xxx` #jest

  • beforeAll
  • afterAll
  • beforeEach
  • afterEach

jestใงใฏใ“ใฎ4ใคใŒใ‚ใ‚‹ใ‘ใฉใ€ใ“ใฎไธญใง้žๅŒๆœŸๅ‡ฆ็†ใ‚’ใ™ใ‚‹ใจใ€้€”็ซฏใซใƒ†ใ‚นใƒˆๅฎŸ่กŒๆ™‚ใฎใƒญใ‚ฐใฎไฝ“่ฃใŒใŠใ‹ใ—ใใชใ‚‹ใจใ„ใ†่ฉฑใ€‚

ใ‚ใจใ€`--forceExit`ใจใ„ใ†ใ‚ชใƒ—ใ‚ทใƒงใƒณใ‚’็ต„ใฟๅˆใ‚ใ›ใ‚‹ใจใพใŸ้•ใฃใŸไฝ“่ฃใซใชใฃใฆใ‚ˆใƒผใ‚ใ‹ใ‚‰ใ‚“ใจใ„ใ†่ฉฑใ€‚

ๅŒๆœŸๅ‡ฆ็†ใ ใ‘ใฎๆญฃๅธธใƒ‘ใ‚ฟใƒผใƒณ

beforeAll(() => {
  console.log('beforeAll');
});
beforeEach(() => {
  console.log('beforeEach');
});
afterEach(() => {
  console.log('afterEach');
});
afterAll(() => {
  console.log('afterAll');
});

describe('d1', () => {
  test('t11', () => { expect('ok'); });
});
describe('d2', () => {
  test('t21', () => { expect('ok'); });
  test('t22', () => { expect('ok'); });
});
describe('d3', () => {
  test('t31', () => { expect('ok'); });
});

ใ“ใ‚Œใ‚’ๆ™ฎ้€šใซๅฎŸ่กŒใ™ใ‚‹ใจใ€ใƒญใ‚ฐใฏๆƒณๅฎš้€šใ‚Šใ“ใ†ใชใ‚‹ใ€‚

pass  __tests__/foo.test.js
 d1
   โœ“ t11 (2ms)
 d2
   โœ“ t21
   โœ“ t22 (1ms)
 d3
   โœ“ t31 (1ms)

 console.log __tests__/foo.test.js:4
   beforeAll

 console.log __tests__/foo.test.js:8
   beforeEach

 console.log __tests__/foo.test.js:13
   afterEach

 console.log __tests__/foo.test.js:8
   beforeEach

 console.log __tests__/foo.test.js:13
   afterEach

 console.log __tests__/foo.test.js:8
   beforeEach

 console.log __tests__/foo.test.js:13
   afterEach

 console.log __tests__/foo.test.js:8
   beforeEach

 console.log __tests__/foo.test.js:13
   afterEach

 console.log __tests__/foo.test.js:16
   afterAll

test suites: 1 passed, 1 total
tests:       4 passed, 4 total
snapshots:   0 total
time:        0.874s, estimated 1s

ไฝ•ใฎๅ•้กŒใ‚‚ใชใ„ใ€‚

  • ใƒ†ใ‚นใƒˆใฎใƒ•ใ‚กใ‚คใƒซๅˆฅ็ตๆžœ
  • `console.log`
  • ใƒ†ใ‚นใƒˆๅ…จไฝ“ใซ้–ขใ™ใ‚‹ๆƒ…ๅ ฑ

ใจใ„ใ†ๆ„Ÿใ˜ใง่ฆ‹ใ‚„ใ™ใ„ใ€‚

้žๅŒๆœŸๅ‡ฆ็†ใ‚’`{before,after}xxx`ใงใ‚„ใ‚‹ใƒ‘ใ‚ฟใƒผใƒณ

ใŸใ ใ—ใ€้žๅŒๆœŸใ‚’ใพใœใ‚‹ใจ้€”็ซฏใซๅ‡บๅŠ›ใ•ใ‚Œใ‚‹ใƒญใ‚ฐใŒใŠใ‹ใ—ใใชใ‚‹ใ€‚

const sleep = msec => new promise(resolve => settimeout(resolve, msec));

// ใ“ใ“ใ ใ‘ๅค‰ใˆใฆใฟใ‚‹
beforeEach(done => {
  sleep(100).then(() => {
    console.log('beforeEach');
    done();
  });
});

ๅ„ใƒ†ใ‚นใƒˆใฎ็›ดๅ‰ใง100msๅพ…ใคใ‚ˆใ†ใซใ€‚

ใ™ใ‚‹ใจใ€

  console.log __tests__/foo.test.js:4
    beforeAll

  console.log __tests__/foo.test.js:8
    beforeEach

  console.log __tests__/foo.test.js:13
    afterEach

  console.log __tests__/foo.test.js:8
    beforeEach

  console.log __tests__/foo.test.js:13
    afterEach

  console.log __tests__/foo.test.js:8
    beforeEach

  console.log __tests__/foo.test.js:13
    afterEach

 pass  __tests__/foo.test.js
  d1
    โœ“ t11 (107ms)
  d2
    โœ“ t21 (102ms)
    โœ“ t22 (106ms)
  d3
    โœ“ t31 (102ms)

test suites: 1 passed, 1 total
tests:       4 passed, 4 total
snapshots:   0 total
time:        1.04s
  console.log __tests__/foo.test.js:8
    beforeEach

  console.log __tests__/foo.test.js:13
    afterEach

  console.log __tests__/foo.test.js:16
    afterAll

ใจใ„ใ†ใ‚ˆใ†ใซใชใ‚‹ใ€‚
ใ•ใฃใใจใƒญใ‚ฐใฎไฝ“่ฃใŒ้•ใ†๏ผใชใ‚“ใง๏ผ

Promiseใฎใจใ“ใ‚ใฏ`async / await`ใซใ—ใฆใ‚‚ๅŒใ˜็ตๆžœใซใชใ‚‹ใ€‚

// ใ“ใฃใกใงใ‚‚
beforeEach(async done => {
  await sleep(100);
  console.log('beforeEach');
  done();
});

// ใ“ใฃใกใงใ‚‚ไธ€็ท’ใ ใฃใŸ
beforeEach(async () => {
  await sleep(100);
  console.log('beforeEach');
});

ใƒญใ‚ฐใŒๆฐ—ๆŒใกๆ‚ชใ„ใ€‚

forceExitใ‚’ใคใ‘ใฆๅฎŸ่กŒใ™ใ‚‹

Jest process doesn’t quit after last test completes · Issue #1456 · facebook/jest · GitHub

ใ“ใฎissueใซใ‚‚ใ‚ใ‚‹ใ‚ˆใ†ใซใ€ๅ‡ฆ็†ใŒ็ต‚ใ‚ใ‚‰ใชใ„ใจjestใฎๅฎŸ่กŒใ‚‚็ต‚ใ‚ใ‚‰ใชใ„ๅ ดๅˆใŒใ‚ใ‚‹ใ€‚
ใใ‚“ใชๆ™‚ใซใ€ๅผทๅˆถ็ต‚ไบ†ใ—ใŸใ„ใ‚ˆใญใฃใฆ่ฉฑใงใ€`--forceExit`ใฃใฆใ‚ชใƒ—ใ‚ทใƒงใƒณใŒใ‚ใ‚‹ใ€‚

ใ•ใฃใใฎ้žๅŒๆœŸใ‚ณใƒผใƒ‰ใซใ“ใ‚Œใ‚’ไฝฟใ†ใจใƒปใƒปใ€

console.log __tests__/foo.test.js:4
  beforeAll

console.log __tests__/foo.test.js:8
  beforeEach

console.log __tests__/foo.test.js:11
  afterEach

console.log __tests__/foo.test.js:8
  beforeEach

console.log __tests__/foo.test.js:11
  afterEach

console.log __tests__/foo.test.js:8
  beforeEach

console.log __tests__/foo.test.js:11
  afterEach

pass  __tests__/foo.test.js
d1
  โœ“ t11 (108ms)
d2
  โœ“ t21 (102ms)
  โœ“ t22 (105ms)
d3
  โœ“ t31 (101ms)

test suites: 1 passed, 1 total
tests:       4 passed, 4 total
snapshots:   0 total
time:        1.043s

ใจใ€`afterAll`ใŒๅ‘ผใฐใ‚Œใฆใชใ„ใฎใซ็ต‚ใ‚ใ‚‹ใƒปใƒปใ‚ˆใ†ใซ่ฆ‹ใˆใ‚‹ใ€‚
ใŸใ ใ—ๅฎŸ้š›ใฏ`afterAll`ใฏๅ‹•ใ„ใฆใ‚‹ใฃใฝใใ€้ฉๅฝ“ใซ`fs.writeFile`ใจใ‹ใ‚„ใ‚‹ใจใ‚ใ‹ใ‚‹ใ€‚
ใ‘ใฉใ€ใƒญใ‚ฐใซใฏๅ‡บใชใ„ใ€‚่ฌŽใ€‚

ใ“ใ‚Œใฏใƒใ‚ฐใชใฎใ‹ใชใ‚“ใชใฎใ‹ใƒปใƒปใ€‚