Cara menggunakan JEST/NO-DONE-CALLBACK pada JavaScript
💼 This rule is enabled in the following configs: 💡 This rule provides suggestions that can be applied manually. When calling asynchronous code in hooks and tests, Originally
the most common pattern to achieve this was to use callbacks: test('the data is peanut butter', done => { function callback(data) { try { expect(data).toBe('peanut butter'); done(); } catch (error) { done(error); } } fetchData(callback); }); This can be very error-prone however, as it requires careful understanding of how assertions work in tests or otherwise tests won't behave as expected. For
example, if the A more straightforward way to handle asynchronous code is to use Promises: test('the data is peanut butter', () => { return fetchData().then(data => { expect(data).toBe('peanut butter'); }); }); When a test or hook returns a promise, If your environment supports test('the data is peanut butter', async () => { const data = await fetchData(); expect(data).toBe('peanut butter'); }); Rule detailsThis rule checks
the function parameter of hooks & tests for use of the The following patterns are considered warnings: beforeEach(done => { // ... }); test('myFunction()', done => { // ... }); test('myFunction()', function (done) { // ... }); The following patterns are not considered warnings: beforeEach(async () => { await setupUsTheBomb(); }); test('myFunction()', () => { expect(myFunction()).toBeTruthy(); }); test('myFunction()', () => { return new Promise(done => { expect(myFunction()).toBeTruthy(); done(); }); }); |