{"id":17239019,"url":"https://github.com/binarymuse/babel-plugin-chai-assert-async","last_synced_at":"2025-07-29T09:32:37.427Z","repository":{"id":66010480,"uuid":"93897261","full_name":"BinaryMuse/babel-plugin-chai-assert-async","owner":"BinaryMuse","description":"Convert synchronous chai assertions into asynchronous chai assertions","archived":false,"fork":false,"pushed_at":"2017-06-09T21:45:29.000Z","size":22,"stargazers_count":0,"open_issues_count":0,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-30T08:41:26.246Z","etag":null,"topics":["assertions","chai","testing"],"latest_commit_sha":null,"homepage":"","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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-06-09T21:14:01.000Z","updated_at":"2017-06-09T21:15:45.000Z","dependencies_parsed_at":null,"dependency_job_id":"f615c6e9-daad-4ef4-b252-dba336e40031","html_url":"https://github.com/BinaryMuse/babel-plugin-chai-assert-async","commit_stats":{"total_commits":7,"total_committers":1,"mean_commits":7.0,"dds":0.0,"last_synced_commit":"ffd094b723c833cbc1a2384e1d7f2e95aa401ce6"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BinaryMuse%2Fbabel-plugin-chai-assert-async","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BinaryMuse%2Fbabel-plugin-chai-assert-async/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BinaryMuse%2Fbabel-plugin-chai-assert-async/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BinaryMuse%2Fbabel-plugin-chai-assert-async/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/BinaryMuse","download_url":"https://codeload.github.com/BinaryMuse/babel-plugin-chai-assert-async/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228006288,"owners_count":17854990,"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":["assertions","chai","testing"],"created_at":"2024-10-15T05:47:30.994Z","updated_at":"2024-12-03T21:42:07.195Z","avatar_url":"https://github.com/BinaryMuse.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# babel-plugin-chai-assert-async\n\nTransforms `assert.async.*` calls into `test-until` expressions that resolve when the assertion passes, preserving error messages for failed assertions.\n\n[![Build Status](https://travis-ci.org/BinaryMuse/babel-plugin-chai-assert-async.svg?branch=master)](https://travis-ci.org/BinaryMuse/babel-plugin-chai-assert-async)\n\n## Installation\n\nAssuming you're [already using Babel](http://babeljs.io/docs/setup/), install the Babel plugin *and the peer dependency* with your package manager of choice:\n\n```sh\n$ npm install babel-plugin-chai-assert-async test-until\n```\n\n## Usage\n\n### Via `.babelrc`\n\n**.babelrc**\n\n```json\n{\n  \"plugins\": [\"chai-assert-async\"]\n}\n```\n\n### Via CLI\n\n```sh\n$ babel --plugins chai-assert-async script.js\n```\n\n### Via Node API\n\n```javascript\nrequire(\"babel-core\").transform(\"code\", {\n  plugins: [\"chai-assert-async\"]\n});\n```\n\n## Details\n\nDuring tests, it's sometime's useful to test values that are set asynchronously, or to wait for a condition to be true before continuing the test. Here's a convoluted example:\n\n```javascript\nfunction setValueAsync(obj) {\n  setTimeout(() =\u003e obj.val = 42, 100)\n}\n\ndescribe('setValueAsync', function() {\n  it('sets a value asynchronously', function() {\n    const obj = {val: 0}\n    setValueAsync(obj)\n    assert.equal(obj.val, 42) // will fail\n  })\n})\n```\n\nThis test will fail because `obj.val` does not equal `42` at the time the assertion is run.\n\nWe can use [a library like `test-until`](https://www.npmjs.com/package/test-until) to ensure the test doesn't continue until `obj.val` does equal `42`. Here's an example with Mocha, `async`/`await`\n\n```javascript\nimport until from 'test-until'\n// ....\nit('sets a value asynchronously', function() {\n  const obj = {val: 0}\n  setValueAsync(obj)\n  await until(() =\u003e obj.val === 42)\n})\n```\n\nHowever, in converting from the `assert.equal` version of this test to the `await until` version, we lost some information. In particular, it's more difficult to know exactly what we're testing, and if the `until` expression times out, we get a very generic error message like \"timed out waiting for something to happen\" instead of something useful like \"expected obj.val to equal 42\".\n\nThis transform allows you to convert something like `assert.equal(obj.val, 42)` into an equivalent `until` expression by writing `assert.async.equal(obj.val, 42)`:\n\n```javascript\nfunction setValueAsync(obj) {\n  setTimeout(() =\u003e obj.val = 42, 100)\n}\n\ndescribe('setValueAsync', function() {\n  it('sets a value asynchronously', async function() {\n    const obj = {val: 0}\n    setValueAsync(obj)\n    await assert.async.equal(obj.val, 42)\n  })\n})\n```\n\nIn this case, if the async assertion fails, the original error message like \"expected obj.val to equal 42\" will be preserved.\n\nIf you want to pass a timeout to `test-until`, you can specify it as an argument to `async`:\n\n```javascript\nawait assert.async(100).equal(obj.val, 42)\n```\n\n## Examples\n\n**Basic**\n\nIn\n\n```js\nasync function test() {\n  await assert.async.equal(thing, other);\n}\n```\n\nOut\n\n```js\n\"use strict\";\n\nimport until from 'test-until';\n\nasync function test() {\n  await until(async function (fail) {\n    try {\n      assert.equal(thing, other);\n      return true;\n    } catch (err) {\n      return fail(err);\n    }\n  });\n}\n```\n\n**With explicit timeout**\n\nIn\n\n```js\nasync function test() {\n  await assert.async(500).equal(thing, other);\n}\n```\n\nOut\n\n```js\n\"use strict\";\n\nimport until from 'test-until';\n\nasync function test() {\n  await until(async function (fail) {\n    try {\n      assert.equal(thing, other);\n      return true;\n    } catch (err) {\n      return fail(err);\n    }\n  }, 500);\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbinarymuse%2Fbabel-plugin-chai-assert-async","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbinarymuse%2Fbabel-plugin-chai-assert-async","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbinarymuse%2Fbabel-plugin-chai-assert-async/lists"}