{"id":22076500,"url":"https://github.com/codecademy/jquery-expect","last_synced_at":"2025-06-20T09:33:35.861Z","repository":{"id":52880878,"uuid":"4441759","full_name":"Codecademy/jquery-expect","owner":"Codecademy","description":"Simple DOM assertion library","archived":false,"fork":false,"pushed_at":"2022-04-05T11:53:58.000Z","size":197,"stargazers_count":59,"open_issues_count":6,"forks_count":17,"subscribers_count":30,"default_branch":"master","last_synced_at":"2025-04-01T09:47:19.488Z","etag":null,"topics":[],"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/Codecademy.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":"2012-05-25T05:42:16.000Z","updated_at":"2022-04-05T11:53:04.000Z","dependencies_parsed_at":"2022-08-23T12:31:37.762Z","dependency_job_id":null,"html_url":"https://github.com/Codecademy/jquery-expect","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/Codecademy/jquery-expect","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Codecademy%2Fjquery-expect","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Codecademy%2Fjquery-expect/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Codecademy%2Fjquery-expect/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Codecademy%2Fjquery-expect/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Codecademy","download_url":"https://codeload.github.com/Codecademy/jquery-expect/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Codecademy%2Fjquery-expect/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260919286,"owners_count":23082728,"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-30T22:14:54.273Z","updated_at":"2025-06-20T09:33:30.846Z","avatar_url":"https://github.com/Codecademy.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# $expect [![Build Status](https://secure.travis-ci.org/Codecademy/jquery-expect.png)](http://travis-ci.org/Codecademy/jquery-expect)\nA DOM assertion library built on top of jQuery and based on [LearnBoost's expect.js](https://github.com/LearnBoost/expect.js)\n\n## Running tests\n\n    git clone https://github.com/Codecademy/jquery-expect.git\n    cd jquery-expect\n    npm install\n\n### Running in phantom.js\n\n    make test\n\n### Running in browser\n\n    make test-browser\n\nNavigate your browser to http://localhost:8080/test\n\n\n## API\nFor all API methods the last argument passed will override the default assertion error message.\n\n### exist\nCheck the existence by asserting that there is at least one element in the jQuery collection.\n\n```javascript\n$expect('div').to.exist();\n$expect('div').to.exist('Please add a div to the page!');\n\n```\n\n### Error message\nAs mentioned above, $expect ships with default error messages for each of the assertion API  \navailable. But also gives the user the ability to override these error messages.  \n\nThe argument following the last formal argument of any assertion method would be considered  \nas the error message overriding argument.\n\nYou could either pass in a simple string or a function that should return a string to override  \nthe default message.\n\nThe passed in function could act as a cleanup function before throwing the assertion error.  \nIt would be passed a boolean stating whether the assertion would throw or not.\n\n```javascript\n$expect('div').to.exist(function (willThrow) {\n  // Some cleanup code.\n  return 'Please add a div to the page.'\n});\n```\n\n### items / elements / length\nAsserts the `.length` property.\n\n```javascript\n$expect('ol \u003e li').to.have.items(4);\n```\n\n### above / greaterThan\nAsserts that the `.length` property is greater than a given number.\n\n```javascript\n$expect('li').to.be.above(4);\n```\n\n### below / lessThan\nAsserts that the `.length` property is less than a given number.\n\n```javascript\n$expect('li').to.be.lessThan(5);\n```\n\n### be / a / an\nAsserts that each element in a jQuery collection matches the passed in selector.  \n\n```javascript\n$expect('div').to.be('.widget');\n$expect('input').to.be('[type=text]');\n$expect('.win').to.be.a('div');\n$expect('.list').to.be.an('ol');\n```\n\nInternally calls `$().is` so it can be passed either a selector, a function, a jQuery object, or an element.  \nFor more info check out the [jQuery docs](http://api.jquery.com/is/).\n\n```javascript\n$expect('h1').to.be($headers);\n```\n\n### eql / equal\nAsserts that one jQuery collection has the exact same elements as another.  \nCan accept a jQuery collection or simply a selector\n\n```javascript\n$expect('li.first').to.be.equal('li:first');\n$expect('div').to.be.equal($('.all-the-divs'));\n```\n\n### attr\nAsserts the existence of an attribute and its equality if a value was passed in.\n\n```javascript\n$expect('.container').to.have.attr('id', 'content');\n$expect('.some-input').to.have.attr('value');\n```\n\n### text\nAsserts that an element has the exact same text.  \nIf a number is passed in then the length of the text would be checked.  \nIf a RegExp is passed in then it will be matched against the text.\n\n```javascript\n$expect('.link-1').to.have.text(10);\n$expect('.link-1').to.have.text(/code/i);\n$expect('.link-1').to.have.text('Codecademy');\n$expect('.link-2').to.have.text('Google', 'Why not?');\n```\n\n### match\nAsserts that the visible text of an element matches the given RegExp.\nAn alias for .text(RegExp).\n```javascript\n$expect('.link-1').to.match(/code/i);\n```\n\n### contain\nAsserts that an element contains a certain text.  \nBy default punctuation, whitespace, and case would be ignored. Pass in a second `true` argument to ensure a strict check.\n\n```javascript\n$expect('body').to.contain('author');\n$expect('.links').to.contain('people');\n$expect('.content').to.contain('Amjad', 'My name must exist and be capitalized');\n```\n\n### Dimension check\nAsserts the `width`, `innerWidth`, `outerWidth`, `height`, `innerHeight`,   \n`outerHeight`, `scrollTop`, and `scrollTop` of an element.  \nCan either pass in a number or a string with the operation prepended to it.\n\n```javascript\n$expect('.nav').to.have.width(250);\n$expect('.header').to.have.innerWidth('\u003e= 50')\n              .and.to.have.innerWidth('\u003c= 250');\n```\n\n### value / val\nAsserts that an input element has a certain value. Calls `$().val`.  \n\n```javascript\n$expect('input.password').to.have.val('PlainText');\n```\n\n### html\nAsserts that an element has an html string. Calls `$().html`.  \n\n```javascript\n$expect('body').to.have.html('\u003cdiv\u003efoo\u003c/div\u003e');\n```\n\n### htmlMatch\nAsserts that the html content of an element matches the given RegExp.\n\n```javascript\n$expect('body').to.matchHtml(/\u003cbody\u003e\u003ch1\u003eContent/);\n```\n\n### Traversing\nAsserts the existence of element in different directions of the DOM tree.  \nRelies on jQuery's traversal methods.\n\n```javascript\n$expect('body').to.have.children('.foos');\n$expect('#so-lonely').to.have.siblings('.party-elements');\n```\n\n`$().find` is aliased to `$().have`\n\n```javascript\n$expect('body').to.have('input');\n```\n\n### any\nAsserts that any element of a collection passes the given expectation callback.\n```javascript\n$expect('ul').to.have.children('li').that.any(function(li) {\n  $expect(li).to.have.text('List Item Content');\n})\n```\n\n### class\nAsserts the existence of a class or multiple space separated classes on each element of a collection.  \n\n```javascript\n$expect('input[type=text]').to.have.class('on field');\n```\n\n### Shorthand attributes\nConvenience methods for checking the following attributes and selectors:  \n`visible`, `hidden`, `selected`, `checked`, `disabled`, `empty`.\n\n```javascript\n$expect('h2').to.be.hidden();\n$expect('input.submit').not.to.be.hidden('Please hide the submit button for now!');\n$expect('body').not.to.be.empty();\n```\n\n### Chaining\nYou can chain assertions on an object just like you can chain methods in jQuery.  \n\n#### And\nChains assertions on the original object.  \n\n```javascript\n$expect('div.container').to.exist().and.not.be.empty().and.to.have.width('\u003e= 250');\n```\n\n#### that / which\nChains assertions on different elements after calling any of the traversal methods.\n\n```javascript\n$expect('ul.todos').to.exist()\n               .and.to.have.children('li.items').that.has.css('border', '1px solid red');\n               \t\t\t\t\t\t\t\t\t.and.has.attr('data-id');\n```\n\nWhen chaining on traveresed elements just as in jQuery you can always call `.end` to get  \nthe original object back.  \n\n```javascript\n$expect('div.container').to.have.siblings('.pane').that.has.css('float', 'left')\n\t\t\t\t   end().to.be('.loading');\n```\n\n## License\nMIT License.\nCopyright (c) 2022 Codecademy LLC\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodecademy%2Fjquery-expect","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodecademy%2Fjquery-expect","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodecademy%2Fjquery-expect/lists"}