{"name":"wait-for-expect","version":"0.3.0","description":"Wait for expectation to be true, useful for integration and end to end testing","main":"lib/index.js","typings":"lib/index.d.ts","scripts":{"test":"eslint src/**/*.ts && jest","build":"tsc --emitDeclarationOnly && babel src --out-dir lib --extensions \".ts,.tsx\"","build:watch":"npm run build -- --watch","type-check":"tsc --noEmit"},"repository":{"type":"git","url":"git+https://github.com/TheBrainFamily/wait-for-expect.git"},"keywords":["jest","expect","wait","async","await","promise","integration","testing","unit"],"author":{"name":"Lukasz Gandecki"},"license":"MIT","bugs":{"url":"https://github.com/TheBrainFamily/wait-for-expect/issues"},"homepage":"https://github.com/TheBrainFamily/wait-for-expect#readme","devDependencies":{"@babel/cli":"^7.0.0-beta.42","@babel/core":"^7.0.0-beta.42","@babel/plugin-proposal-class-properties":"^7.0.0-beta.42","@babel/plugin-proposal-object-rest-spread":"^7.0.0-beta.42","@babel/preset-env":"^7.0.0-beta.42","@babel/preset-typescript":"^7.0.0-beta.42","@types/eslint":"^4.16.0","@types/eslint-plugin-prettier":"^2.2.0","@types/jest":"^22.2.2","@types/node":"^9.6.1","@types/prettier":"^1.10.0","@types/typescript":"^2.0.0","babel-core":"^7.0.0-0","babel-jest":"^22.4.3","eslint":"^4.19.1","eslint-config-airbnb-base":"^12.1.0","eslint-config-prettier":"^2.9.0","eslint-plugin-import":"^2.9.0","eslint-plugin-prettier":"^2.6.0","eslint-plugin-typescript":"^0.11.0","jest":"^22.4.3","jest-serializer-ansi":"^1.0.3","prettier":"^1.11.1","types-installer":"^1.4.0","typescript":"^2.7.2","typescript-babel-jest":"^1.0.5","typescript-eslint-parser":"^14.0.0"},"jest":{"snapshotSerializers":["jest-serializer-ansi"],"moduleDirectories":["./node_modules","./src"],"moduleFileExtensions":["ts","tsx","js","jsx"],"transform":{"^.+\\.(js|jsx|ts|tsx)$":"babel-jest"},"testRegex":"/src/.*\\.spec\\.(js|ts|tsx)$"},"readme":"[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)\n[![CircleCI](https://circleci.com/gh/TheBrainFamily/wait-for-expect.svg?style=shield)](https://circleci.com/gh/TheBrainFamily/wait-for-expect)\n\n# wait-for-expect\nWait for expectation to be true, useful for integration and end to end testing\n\nThink things like calling external APIs, database operations, or even GraphQL subscriptions. \nWe will add examples for all of them soon, for now please enjoy the simple docs. :-)\n\n# Usage:\n\n```javascript\nconst waitForExpect = require(\"wait-for-expect\")\n\ntest(\"it waits for the number to change\", async () => {\n  let numberToChange = 10;\n  // we are using random timeout here to simulate a real-time example\n  // of an async operation calling a callback at a non-deterministic time\n  const randomTimeout = Math.floor(Math.random() * 300);\n\n  setTimeout(() => {\n    numberToChange = 100;\n  }, randomTimeout);\n\n  await waitForExpect(() => {\n    expect(numberToChange).toEqual(100);\n  });\n});\n```\n\ninstead of:\n\n```javascript\n\ntest(\"it waits for the number to change\", () => {\n  let numberToChange = 10;\n  const randomTimeout = Math.floor(Math.random() * 300);\n\n  setTimeout(() => {\n    numberToChange = 100;\n  }, randomTimeout);\n  \n  setTimeout(() => {\n    expect(numberToChange).toEqual(100);\n  }, 700);\n});\n```\n\nIt will check whether the expectation passes right away in the next available \"tick\" (very useful with, for example, integration testing of react when mocking fetches, like here: https://github.com/kentcdodds/react-testing-library#usage).\n\nIf it doesn't, it will keep repeating for the duration of, at most, the specified timeout, every 50 ms. The default timeout is 4.5 seconds to fit below the default 5 seconds that Jest waits for before throwing an error. \n\nNice thing about this simple tool is that if the expectation keeps failing till the timeout, it will check it one last time, but this time the same way your test runner would run it - so you basically get your expectation library error, the sam way like if you used setTimeout to wait but didn't wait long enough.\n\nTo show an example - if I change the expectation to wait for 105 in above code, you will get nice and familiar:\n\n```\n\n FAIL  src/waitForExpect.spec.js (5.042s)\n  ✕ it waits for the number to change (4511ms)\n\n  ● it waits for the number to change\n\n    expect(received).toEqual(expected)\n    \n    Expected value to equal:\n      105\n    Received:\n      100\n\n       9 |   }, 600);\n      10 |   await waitForExpect(() => {\n    > 11 |     expect(numberToChange).toEqual(105);\n      12 |   });\n      13 | });\n      14 | \n      \n      at waitForExpect (src/waitForExpect.spec.js:11:28)\n      at waitUntil.catch (src/index.js:61:5)\n\nTest Suites: 1 failed, 1 total\nTests:       1 failed, 1 total\nSnapshots:   0 total\nTime:        5.807s\n```\n\nYou can add multiple expectations to wait for, all of them have to pass, and if one of them don't, it will be marked.\nFor example, let's add another expectation for a different number, notice how jest tells you that that's the expectation that failed.\n\n```\n    expect(received).toEqual(expected)\n    \n    Expected value to equal:\n      110\n    Received:\n      105\n\n      11 |   await waitForExpect(() => {\n      12 |     expect(numberToChange).toEqual(100);\n    > 13 |     expect(numberThatWontChange).toEqual(110);\n      14 |   });\n      15 | });\n      16 | \n      \n      at waitForExpect (src/waitForExpect.spec.js:13:34)\n      at waitUntil.catch (src/index.js:61:5)\n```\n\n\n# API\nwaitForExpect takes 3 arguments, 2 optional.\n\n```javascript\n/**\n * Waits for predicate to not throw and returns a Promise\n *\n * @param  expectation  Function  Predicate that has to complete without throwing\n * @param  timeout  Number  Maximum wait interval, 4500ms by default\n * @param  interval  Number  Wait interval, 50ms by default\n * @return  Promise  Promise to return a callback result\n */\n```\n\n## Credit\nOriginally based on ideas from https://github.com/devlato/waitUntil.\nSimplified highly and rewritten for 0.1.0 version.\nSimplified even more and rewritten even more for 0.2.0 with guidance from Kent C. Dodds: https://github.com/kentcdodds/react-testing-library/pull/25","readmeFilename":"README.md","gitHead":"f8d5c63c53c76b1211d04c81a4342f18adab4ee1","_id":"wait-for-expect@0.3.0","_npmVersion":"5.6.0","_nodeVersion":"9.3.0","_npmUser":{"name":"lgandecki","email":"lgandecki@thebrain.pro"},"dist":{"integrity":"sha512-JK6rAOHexTzLL2lutbpHHZJsbA2rmJcn0lMTJgAZY6abqRYH1gQLAGXrg6krA5kEuAGLc0SCy8QftvfrgcUJig==","shasum":"a09a9804df14ecd732afdd4c7128b637126dcee4","tarball":"http://registry-npm.ywwl.com/wait-for-expect/download/wait-for-expect-0.3.0.tgz","fileCount":25,"unpackedSize":56973,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCsh44Wjq/sCxG8jOqi12YYMq24/lNtZKfX7FNI3nf9MAIhAIC9nE+aZ+/OhbZloB6KlC011H8KaUnZG5MZg1ruSjh7"}],"size":11730,"noattachment":false,"key":"/wait-for-expect/-/wait-for-expect-0.3.0.tgz"},"maintainers":[{"name":"lgandecki","email":"lgandecki@thebrain.pro"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/wait-for-expect_0.3.0_1522327397950_0.08405120702795066"},"_hasShrinkwrap":false,"publish_time":1522327398115,"_cnpm_publish_time":1522327398115,"dist-tags":{"latest":"4.0.0","next":"0.6.0","typescript":"0.3.0"}}