Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

no-done-callback.md 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # Avoid using a callback in asynchronous tests and hooks (`no-done-callback`)
  2. When calling asynchronous code in hooks and tests, `jest` needs to know when the
  3. asynchronous work is complete to progress the current run.
  4. Originally the most common pattern to archive this was to use callbacks:
  5. ```js
  6. test('the data is peanut butter', done => {
  7. function callback(data) {
  8. try {
  9. expect(data).toBe('peanut butter');
  10. done();
  11. } catch (error) {
  12. done(error);
  13. }
  14. }
  15. fetchData(callback);
  16. });
  17. ```
  18. This can be very error prone however, as it requires careful understanding of
  19. how assertions work in tests or otherwise tests won't behave as expected.
  20. For example, if the `try/catch` was left out of the above code, the test would
  21. timeout rather than fail. Even with the `try/catch`, forgetting to pass the
  22. caught error to `done` will result in `jest` believing the test has passed.
  23. A more straightforward way to handle asynchronous code is to use Promises:
  24. ```js
  25. test('the data is peanut butter', () => {
  26. return fetchData().then(data => {
  27. expect(data).toBe('peanut butter');
  28. });
  29. });
  30. ```
  31. When a test or hook returns a promise, `jest` waits for that promise to resolve,
  32. as well as automatically failing should the promise reject.
  33. If your environment supports `async/await`, this becomes even simpler:
  34. ```js
  35. test('the data is peanut butter', async () => {
  36. const data = await fetchData();
  37. expect(data).toBe('peanut butter');
  38. });
  39. ```
  40. ## Rule details
  41. This rule checks the function parameter of hooks & tests for use of the `done`
  42. argument, suggesting you return a promise instead.
  43. The following patterns are considered warnings:
  44. ```js
  45. beforeEach(done => {
  46. // ...
  47. });
  48. test('myFunction()', done => {
  49. // ...
  50. });
  51. test('myFunction()', function (done) {
  52. // ...
  53. });
  54. ```
  55. The following patterns are not considered warnings:
  56. ```js
  57. beforeEach(async () => {
  58. await setupUsTheBomb();
  59. });
  60. test('myFunction()', () => {
  61. expect(myFunction()).toBeTruthy();
  62. });
  63. test('myFunction()', () => {
  64. return new Promise(done => {
  65. expect(myFunction()).toBeTruthy();
  66. done();
  67. });
  68. });
  69. ```