{"id":19044638,"url":"https://github.com/bubao/expect_demo","last_synced_at":"2026-06-25T05:31:01.663Z","repository":{"id":207029374,"uuid":"99500012","full_name":"bubao/expect_demo","owner":"bubao","description":"一个翻译很烂还烂尾的 Expect 断言库文档笔记。","archived":false,"fork":false,"pushed_at":"2017-08-06T16:44:34.000Z","size":23,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-08T22:46:52.725Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/bubao.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}},"created_at":"2017-08-06T16:42:51.000Z","updated_at":"2017-08-06T16:44:35.000Z","dependencies_parsed_at":"2023-11-13T19:26:26.872Z","dependency_job_id":"5c045b9c-e659-466d-a123-1bd2793a64e0","html_url":"https://github.com/bubao/expect_demo","commit_stats":null,"previous_names":["bubao/expect_demo"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bubao%2Fexpect_demo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bubao%2Fexpect_demo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bubao%2Fexpect_demo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bubao%2Fexpect_demo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bubao","download_url":"https://codeload.github.com/bubao/expect_demo/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":232168858,"owners_count":18482484,"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":[],"created_at":"2024-11-08T22:46:56.201Z","updated_at":"2025-11-12T05:01:54.577Z","avatar_url":"https://github.com/bubao.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Expect 断言库\n\n\u003e 2017-08-06 21:52:34\n\n一个翻译很烂还烂尾的 [Expect](https://github.com/mjackson/expect) 笔记。\n\n## 运行\n\nif you install yarn,you can run the demo in this way:\n\n```sh\nyarn \nyarn test\n```\n\nor you can use npm\n\n```sh\nnpm install\nnpm run test\n``` \n\n## Demo说明\n\n### ToExist.js\n\ntoExist \u0026\u0026 toNotExist 断定给定的obj是否为真的。\n\n### ToBe.js\n\ntoBe \u0026\u0026 toNotBe 判断obj是否“===”设定值\n\ntoEqual \u0026\u0026 toNotEqual 判断obj是否“==”设定值\n\n### ToThrow.js\n\ntoThrow \u0026\u0026 toNotThrow 捕捉错误\n\n### ToBeA.js\n\ntoBeA \u0026\u0026 toNotBeA 断定obj是否是一个构造函数或者字符串\n\n\u003e 字符串里面写的是obj的类型。\n\n### ToMatch.js\n\ntoMatch \u0026\u0026 toNotMatch 查找是否存在\n\n### ToBeLessThan.js\n\ntoBeLessThan \u0026\u0026 toBeLessThanOrEqualTo \u0026\u0026 toBeGreaterThan \u0026\u0026 toBeGreaterThanOrEqualTo\n\n小于 \u0026\u0026 小于等于 \u0026\u0026 大于 \u0026\u0026 大于等于\n\n比较大小\n\n### ToIncludeKey.js\n\ntoIncludeKey(s) \u0026\u0026 toExcludeKey(s) 判断obj(数组或者方法或者其他东西)的key是否存在。\n\n### ToHaveBeenCalled.js\n\n- `toHaveBeenCalled()`断言间谍函数是否至少被调用过一次\n- `toNotHaveBeenCalled()`断言谍函数是否没被调用过\n- `toHaveBeenCalledWith()`断言给定的间谍函数已被调用，并带有预期的参数。\n\n# 其他\n\n## 链接断言(Chaining Assertions)\n\n每个断言都返回一个`Expectation`对象，所以你能链接断言。\n\n```js\nexpect(3.14)\n  .toExist()\n  .toBeLessThan(4)\n  .toBeGreaterThan(3)\n```\n\n\n\n## Spy\n\n`expect`能创建间谍功能，可以跟踪对其他功能进行的调用，并根据所使用的参数和上下文进行各种断言。\n\n```js\nvar video = {\n  play: function () {},\n  pause: function () {},\n  rewind: function () {}\n}\n\nvar spy = expect.spyOn(video, 'play')\n\nvideo.play('some', 'args')\n\nexpect(spy.calls.length).toEqual(1)\nexpect(spy.calls[0].context).toBe(video)\nexpect(spy.calls[0].arguments).toEqual([ 'some', 'args' ])\nexpect(spy).toHaveBeenCalled()\nexpect(spy).toHaveBeenCalledWith('some', 'args')\n\nspy.restore()\nexpect.restoreSpies()\n```\n\n### createSpy\n\n```js\nexpect.createSpy()\n```\n\n创建一个间谍函数\n\n```js\nvar spy = expect.createSpy()\n```\n\n### spyOn\n\n```js\nexpect.spyOn(target, method)\n```\n\n用间谍替代`target`中的`method`。\n\n```js\nvar video = {\n  play: function () {}\n}\n\nvar spy = expect.spyOn(video, 'play')\nvideo.play()\n\nspy.restore()\n```\n\n### restoreSpies\n\n恢复用`expect.spyOn()`创建的所有`spy`。这与在所创建的所有`spy`中调用`spy.restore()`相同。\n\n```js\nbeforeEach(function () {\n  expect.spyOn(profile, 'load')\n})\n\nafterEach(function () {\n  expect.restoreSpies()\n})\n\nit('works', function () {\n  profile.load()\n  expect(profile.load).toHaveBeenCalled()\n})\n```\n\n\n\n\n## Spy methods and properties\n\n### andCall\n\n```js\nspy.andCall(fn)\n```\n使调用函数fn当间谍。\n\n\u003e **原文：** Makes the spy invoke a function fn when called.\n\n```js\nvar dice = createSpy().andCall(function () {\n  return (Math.random() * 6) | 0\n})\n```\n\n### andCallThrough\n\n```js\nspy.andCallThrough()\n```\n\n使间谍呼叫的原始功能，它的间谍。\n\n\u003e **原文：** Makes the spy call the original function it's spying on.\n\n```js\nspyOn(profile, 'load').andCallThrough()\n\nvar getEmail = createSpy(function () {\n  return \"hi@gmail.com\"\n}).andCallThrough()\n```\n\n### andReturn\n\n```js\nspy.andReturn(object)\n```\n\n使spy返回一个值。\n\n```js\nvar dice = expect.createSpy().andReturn(3)\n```\n\n### andThrow\n\n```js\nspy.andThrow(error)\n```\n让spy抛出一个错误时调用。\n\n\u003e **原文：** Makes the spy throw an error when called.\n\n```js\nvar failing = expect.createSpy()\n  .andThrow(new Error('Not working'))\n```\n\n### restore\n\n```js\nspy.restore()\n```\n恢复最初创建的`expect.spyOn()`。\n\n\u003e **原文：** Restores a spy originally created with `expect.spyOn()`.\n\n### reset\n\n```js\nspy.reset()\n```\n清除所有保存调用间谍。\n\n\u003e **原文：** Clears out all saved calls to the spy.\n\n### calls\n\n```js\nspy.calls\n```\n\nAn array of objects representing all saved calls to the spy.\n\nYou can use the length of the calls array to make assertions about how many times you expect the spy to have been called.\n\n```js\nexpect(spy.calls.length).toEqual(3)\n```\n\nYou can also use the array to make assertions about each individual call. Each call object contains the following properties:\n\n**context**\n\n```js\nspy.calls[index].context\n```\n这个值的调用的执行上下文。\n\n\u003e **原文：** The this value of the call's execution context.\n\n**arguments**\n\n```js\nspy.calls[index].arguments\n```\n\n传递给特定调用的间谍的参数数组。\n\n\u003e **原文：** An array of the arguments passed to the spy for the particular call.\n\n### Extending expect\n\n你可以添加你自己的断言使用 `expect.extend` 和 `expect.assert`:\n\n```js\nexpect.extend({\n  toBeAColor() {\n    expect.assert(\n      this.actual.match(/^#[a-fA-F0-9]{3,6}$/),\n      'expected %s to be an HTML color',\n      this.actual\n    )\n    return this\n  }\n})\n\nexpect('#ff00ff').toBeAColor()\n```\n\n## 扩展(Extensions)\n\n- [expect-element](https://github.com/mjackson/expect-element) Adds assertions that are useful for DOM elements\n- [expect-jsx](https://github.com/algolia/expect-jsx) Adds things like expect(ReactComponent).toEqualJSX(\u003cTestComponent prop=\"yes\" /\u003e)\n- [expect-predicate](https://github.com/erikras/expect-predicate) Adds assertions based on arbitrary predicates\n- [expect-enzyme](https://github.com/PsychoLlama/expect-enzyme) Augments and extends expect to supercharge your enzyme assertions","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbubao%2Fexpect_demo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbubao%2Fexpect_demo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbubao%2Fexpect_demo/lists"}