{"id":17239024,"url":"https://github.com/binarymuse/test-until","last_synced_at":"2025-04-14T02:32:52.535Z","repository":{"id":15493099,"uuid":"78258153","full_name":"BinaryMuse/test-until","owner":"BinaryMuse","description":"Utility method that returns a promise that resolves when a condition returns true","archived":false,"fork":false,"pushed_at":"2020-08-23T21:45:40.000Z","size":15,"stargazers_count":7,"open_issues_count":1,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-14T02:32:34.565Z","etag":null,"topics":["javascript","promises","unit-testing"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/test-until","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/BinaryMuse.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-01-07T04:20:49.000Z","updated_at":"2022-02-05T23:40:28.000Z","dependencies_parsed_at":"2022-08-07T08:01:09.055Z","dependency_job_id":null,"html_url":"https://github.com/BinaryMuse/test-until","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BinaryMuse%2Ftest-until","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BinaryMuse%2Ftest-until/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BinaryMuse%2Ftest-until/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BinaryMuse%2Ftest-until/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/BinaryMuse","download_url":"https://codeload.github.com/BinaryMuse/test-until/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248811005,"owners_count":21165210,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","promises","unit-testing"],"created_at":"2024-10-15T05:47:32.326Z","updated_at":"2025-04-14T02:32:52.506Z","avatar_url":"https://github.com/BinaryMuse.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# test-until\n\n[![Build Status](https://travis-ci.org/BinaryMuse/test-until.svg?branch=master)](https://travis-ci.org/BinaryMuse/test-until)\n\nA utility that returns a promise that resolves when the passed function returns true. It works like Jasmine's `waitsFor`, but Promise based.\n\n## Installation\n\nNode.js:\n\n`npm install --save[-dev] test-until`\n\n## Requirements\n\ntest-until requires a global variable `Promise` which needs to be a A+ compliant promise implementation (such as is available by default in [most modern browsers](http://caniuse.com/#feat=promises)). If you need to polyfill `Promise` for any reason (such as supporting IE 11), I recommend [promise-polyfill](https://github.com/taylorhakes/promise-polyfill).\n\n## Usage\n\n```javascript\nvar promise = until(checkFunc, message, timeout)\n```\n\n* `checkFunc` - A function that returns a truthy value once the promise should be resolved. `until` will call this function repeatedly until it returns `true` or the timeout elapses.\n* `message` *(optional)* - A message to help identify failing cases. For example, setting this to `\"value == 42\"` will reject with an error of `\"timed out waiting until value == 42\"` if it times out. Defaults to `\"something happens\"`.\n* `timeout` *(optional)* - The number of milliseconds to wait before timing out and rejecting the promise. Defaults to `1000`.\n\nThe three arguments can be supplied in any order depending on your preferences. For example, putting the message first can make the line read a little more like English:\n\n```javascript\nuntil('we know the answer to life, the universe, and everything', function () { return val === 42 }, 500)\n```\n\n## Example with Test Framework\n\nHere's an example using the [Mocha](https://mochajs.org/) testing framework.\n\n```javascript\nvar until = require('test-until')\n\ndescribe('something', function () {\n  it('tests things', function (done) {\n    var val = 0\n    setTimeout(function () { val = 42 }, 100)\n    var promise = until(function () { val === 42 })\n    promise.then(function() {\n      // after 100ms, `val` will be set to `42`\n      // and the promise returned from `until` will resolve\n      done()\n    })\n  })\n})\n```\n\ntest-until reads and works even better with access to `async`/`await` in your tests, allowing you to wait for multiple async conditions with no callbacks in a manner that reads much like English:\n\n```javascript\nimport until from 'test-until'\n\ndescribe('something', function () {\n  it('tests things', async function () {\n    let val = 0\n    let otherVal = 0\n    setTimeout(() =\u003e val = 42, 100)\n    setTimeout(() =\u003e otherVal = 2048, 200)\n    // Awaiting a rejected promise will cause a synchronous `throw`,\n    // which will reject the promise returned from the async function\n    // and will fail the test.\n    await until('val is 42', () =\u003e val === 42)\n    await until('otherVal is 2048', () =\u003e otherVal === 2048)\n  })\n})\n```\n\n## Advanced Usage\n\n### Setting the Default Timeout\n\nUse `until.setDefaultTimeout(ms)` to set the default timeout. You can easily set this to different values in different parts of your test suite by setting it in a `beforeEach`\n\n```javascript\nimport until from 'test-until'\n\n// Set a global default timeout\nbeforeEach(function () {\n  until.setDefaultTimeout(500)\n})\n\ndescribe('slow stuff', function () {\n  beforeEach(function () {\n    // Make it a bit longer for these tests\n    until.setDefaultTimeout(1000)\n  })\n  // ...\n})\n```\n\nPassing a falsy `ms` resets to the default of `1000`.\n\n### Setting the Error Message from Inside the Check Function\n\nThe check function gets called with a special argument called `setError` which allows the check function to specify an error to return if the `until` call times out. This can be useful when integrating `until` with other test assertions; for example, here's a snippet that will wait for `val` to be `42` and will reject with an actual Chai assertion error if it fails.\n\n```javascript\nimport until from 'test-until'\nimport {assert} from 'chai'\n\ndescribe('something', function () {\n  it('tests things', async function () {\n    let val = 0\n    setTimeout(() =\u003e val = 42, 100)\n    await until(setError =\u003e {\n      try {\n        assert.equal(val, 42)\n        return true\n      } catch (err) {\n        return setError(err) // explicitly set the error\n      }\n    })\n  })\n})\n```\n\n## Development\n\nThe test suite uses newer JavaScript features, so you need Node.js 6+ in order to run it. Run `npm test` to run the suite.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbinarymuse%2Ftest-until","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbinarymuse%2Ftest-until","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbinarymuse%2Ftest-until/lists"}