- 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`ใจใใใใจใใใใ
ใใฉใใญใฐใซใฏๅบใชใใ่ฌใ
ใใใฏใใฐใชใฎใใชใใชใฎใใปใปใ