Skip to main content
Module

x/chai/test/expect.js

BDD / TDD assertion framework for node.js and the browser that can be paired with any testing framework.
Go to Latest
File
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936
describe('expect', function () { var expect = chai.expect; it('chai.version', function() { expect(chai).to.have.property('version'); }); it('assertion', function(){ expect('test').to.be.a('string'); expect('foo').to.equal('foo'); }); describe('safeguards', function () { before(function () { chai.util.addProperty(chai.Assertion.prototype, 'tmpProperty', function () { new chai.Assertion(42).equal(42); }); chai.util.overwriteProperty(chai.Assertion.prototype, 'tmpProperty', function (_super) { return function () { _super.call(this); }; }); chai.util.addMethod(chai.Assertion.prototype, 'tmpMethod', function () { new chai.Assertion(42).equal(42); }); chai.util.overwriteMethod(chai.Assertion.prototype, 'tmpMethod', function (_super) { return function () { _super.call(this); }; }); chai.util.addChainableMethod(chai.Assertion.prototype, 'tmpChainableMethod', function () { new chai.Assertion(42).equal(42); }, function () { new chai.Assertion(42).equal(42); }); chai.util.overwriteChainableMethod(chai.Assertion.prototype, 'tmpChainableMethod', function (_super) { return function () { _super.call(this); }; }, function (_super) { return function () { _super.call(this); }; }); }); after(function () { delete chai.Assertion.prototype.tmpProperty; delete chai.Assertion.prototype.tmpMethod; delete chai.Assertion.prototype.tmpChainableMethod; }); describe('proxify', function () { if (typeof Proxy === 'undefined' || typeof Reflect === 'undefined') return; it('throws when invalid property follows expect', function () { err(function () { expect(42).pizza; }, 'Invalid Chai property: pizza', true); }); it('throws when invalid property follows language chain', function () { err(function () { expect(42).to.pizza; }, 'Invalid Chai property: pizza', true); }); it('throws when invalid property follows property assertion', function () { err(function () { expect(42).ok.pizza; }, 'Invalid Chai property: pizza', true); }); it('throws when invalid property follows overwritten property assertion', function () { err(function () { expect(42).tmpProperty.pizza; }, 'Invalid Chai property: pizza', true); }); it('throws when invalid property follows uncalled method assertion', function () { err(function () { expect(42).equal.pizza; }, 'Invalid Chai property: equal.pizza. See docs for proper usage of "equal".', true); }); it('throws when invalid property follows called method assertion', function () { err(function () { expect(42).equal(42).pizza; }, 'Invalid Chai property: pizza', true); }); it('throws when invalid property follows uncalled overwritten method assertion', function () { err(function () { expect(42).tmpMethod.pizza; }, 'Invalid Chai property: tmpMethod.pizza. See docs for proper usage of "tmpMethod".', true); }); it('throws when invalid property follows called overwritten method assertion', function () { err(function () { expect(42).tmpMethod().pizza; }, 'Invalid Chai property: pizza', true); }); it('throws when invalid property follows uncalled chainable method assertion', function () { err(function () { expect(42).a.pizza; }, 'Invalid Chai property: pizza', true); }); it('throws when invalid property follows called chainable method assertion', function () { err(function () { expect(42).a('number').pizza; }, 'Invalid Chai property: pizza', true); }); it('throws when invalid property follows uncalled overwritten chainable method assertion', function () { err(function () { expect(42).tmpChainableMethod.pizza; }, 'Invalid Chai property: pizza', true); }); it('throws when invalid property follows called overwritten chainable method assertion', function () { err(function () { expect(42).tmpChainableMethod().pizza; }, 'Invalid Chai property: pizza', true); }); it('doesn\'t throw if invalid property is excluded via config', function () { expect(function () { expect(42).then; }).to.not.throw(); }); }); describe('length guard', function () { var fnLengthDesc = Object.getOwnPropertyDescriptor(function () {}, 'length'); if (!fnLengthDesc.configurable) return; it('doesn\'t throw when `.length` follows `.expect`', function () { expect(function () { expect('foo').length; }).to.not.throw(); }); it('doesn\'t throw when `.length` follows language chain', function () { expect(function () { expect('foo').to.length; }).to.not.throw(); }); it('doesn\'t throw when `.length` follows property assertion', function () { expect(function () { expect('foo').ok.length; }).to.not.throw(); }); it('doesn\'t throw when `.length` follows overwritten property assertion', function () { expect(function () { expect('foo').tmpProperty.length; }).to.not.throw(); }); it('throws when `.length` follows uncalled method assertion', function () { err(function () { expect('foo').equal.length; }, 'Invalid Chai property: equal.length. See docs for proper usage of "equal".', true); }); it('doesn\'t throw when `.length` follows called method assertion', function () { expect(function () { expect('foo').equal('foo').length; }).to.not.throw(); }); it('throws when `.length` follows uncalled overwritten method assertion', function () { err(function () { expect('foo').tmpMethod.length; }, 'Invalid Chai property: tmpMethod.length. See docs for proper usage of "tmpMethod".', true); }); it('doesn\'t throw when `.length` follows called overwritten method assertion', function () { expect(function () { expect('foo').tmpMethod().length; }).to.not.throw(); }); it('throws when `.length` follows uncalled chainable method assertion', function () { err(function () { expect('foo').a.length; }, 'Invalid Chai property: a.length. Due to a compatibility issue, "length" cannot directly follow "a". Use "a.lengthOf" instead.', true); }); it('doesn\'t throw when `.length` follows called chainable method assertion', function () { expect(function () { expect('foo').a('string').length; }).to.not.throw(); }); it('throws when `.length` follows uncalled overwritten chainable method assertion', function () { err(function () { expect('foo').tmpChainableMethod.length; }, 'Invalid Chai property: tmpChainableMethod.length. Due to a compatibility issue, "length" cannot directly follow "tmpChainableMethod". Use "tmpChainableMethod.lengthOf" instead.', true); }); it('doesn\'t throw when `.length` follows called overwritten chainable method assertion', function () { expect(function () { expect('foo').tmpChainableMethod().length; }).to.not.throw(); }); }); }); it('no-op chains', function() { function test(chain) { // tests that chain exists expect(expect(1)[chain]).not.undefined; // tests methods expect(1)[chain].equal(1); // tests properties that assert expect(false)[chain].false; // tests not expect(false)[chain].not.true; // tests chainable methods expect([1, 2, 3])[chain].contains(1); } [ 'to', 'be', 'been', 'is' , 'and', 'has', 'have', 'with' , 'that', 'which', 'at', 'of' , 'same', 'but', 'does', 'still', 'also' ].forEach(test); }); describe("fail", function() { it('should accept a message as the 3rd argument', function () { err(function() { expect.fail(0, 1, 'this has failed'); }, /this has failed/); }); it('should accept a message as the only argument', function () { err(function() { expect.fail('this has failed'); }, /this has failed/); }); it('should produce a default message when called without any arguments', function () { err(function() { expect.fail(); }, /expect\.fail()/); }); }); it('true', function(){ expect(true).to.be.true; expect(false).to.not.be.true; expect(1).to.not.be.true; err(function(){ expect('test', 'blah').to.be.true; }, "blah: expected 'test' to be true") }); it('ok', function(){ expect(true).to.be.ok; expect(false).to.not.be.ok; expect(1).to.be.ok; expect(0).to.not.be.ok; err(function(){ expect('', 'blah').to.be.ok; }, "blah: expected '' to be truthy"); err(function(){ expect('test').to.not.be.ok; }, "expected 'test' to be falsy"); }); it('false', function(){ expect(false).to.be.false; expect(true).to.not.be.false; expect(0).to.not.be.false; err(function(){ expect('', 'blah').to.be.false; }, "blah: expected '' to be false") }); it('null', function(){ expect(null).to.be.null; expect(false).to.not.be.null; err(function(){ expect('', 'blah').to.be.null; }, "blah: expected '' to be null") }); it('undefined', function(){ expect(undefined).to.be.undefined; expect(null).to.not.be.undefined; err(function(){ expect('', 'blah').to.be.undefined; }, "blah: expected '' to be undefined") }); it('exist', function(){ var foo = 'bar' , bar; expect(foo).to.exist; expect(foo).to.exists; expect(bar).to.not.exist; expect(bar).to.not.exists; expect(0).to.exist; expect(false).to.exist; expect('').to.exist; err(function () { expect(bar, 'blah').to.exist; }, "blah: expected undefined to exist"); err(function () { expect(foo).to.not.exist(foo); }, "expected 'bar' to not exist"); }); it('arguments', function(){ var args = (function(){ return arguments; })(1,2,3); expect(args).to.be.arguments; expect([]).to.not.be.arguments; expect(args).to.be.an('arguments').and.be.arguments; expect([]).to.be.an('array').and.not.be.Arguments; err(function () { expect([], 'blah').to.be.arguments; }, "blah: expected [] to be arguments but got Array"); }); it('.equal()', function(){ var foo; expect(undefined).to.equal(foo); err(function(){ expect(undefined).to.equal(null); }, "expected undefined to equal null") }); it('typeof', function(){ expect('test').to.be.a('string'); err(function(){ expect('test').to.not.be.a('string'); }, "expected 'test' not to be a string"); (function () { expect(arguments).to.be.an('arguments'); })(1, 2); expect(5).to.be.a('number'); expect(new Number(1)).to.be.a('number'); expect(Number(1)).to.be.a('number'); expect(true).to.be.a('boolean'); expect(new Array()).to.be.a('array'); expect(new Object()).to.be.a('object'); expect({}).to.be.a('object'); expect([]).to.be.a('array'); expect(function() {}).to.be.a('function'); expect(null).to.be.a('null'); if (typeof Symbol === 'function') { expect(Symbol()).to.be.a('symbol'); } err(function(){ expect(5).to.not.be.a('number', 'blah'); }, "blah: expected 5 not to be a number"); err(function(){ expect(5, 'blah').to.not.be.a('number'); }, "blah: expected 5 not to be a number"); }); it('instanceof', function(){ function Foo(){} expect(new Foo()).to.be.an.instanceof(Foo); // Normally, `instanceof` requires that the constructor be a function or an // object with a callable `@@hasInstance`. But in some older browsers such // as IE11, `instanceof` also accepts DOM-related interfaces such as // `HTMLElement`, despite being non-callable objects in those browsers. // See: https://github.com/chaijs/chai/issues/1000. if (typeof document !== 'undefined' && typeof document.createElement !== 'undefined' && typeof HTMLElement !== 'undefined') { expect(document.createElement('div')).to.be.an.instanceof(HTMLElement); } err(function(){ expect(new Foo()).to.an.instanceof(1, 'blah'); }, "blah: The instanceof assertion needs a constructor but number was given."); err(function(){ expect(new Foo(), 'blah').to.an.instanceof(1); }, "blah: The instanceof assertion needs a constructor but number was given."); err(function(){ expect(new Foo()).to.an.instanceof('batman'); }, "The instanceof assertion needs a constructor but string was given."); err(function(){ expect(new Foo()).to.an.instanceof({}); }, "The instanceof assertion needs a constructor but Object was given."); err(function(){ expect(new Foo()).to.an.instanceof(true); }, "The instanceof assertion needs a constructor but boolean was given."); err(function(){ expect(new Foo()).to.an.instanceof(null); }, "The instanceof assertion needs a constructor but null was given."); err(function(){ expect(new Foo()).to.an.instanceof(undefined); }, "The instanceof assertion needs a constructor but undefined was given."); err(function(){ function Thing(){}; var t = new Thing(); Thing.prototype = 1337; expect(t).to.an.instanceof(Thing); }, 'The instanceof assertion needs a constructor but function was given.', true) if (typeof Symbol !== 'undefined' && typeof Symbol.hasInstance !== 'undefined') { err(function(){ expect(new Foo()).to.an.instanceof(Symbol()); }, "The instanceof assertion needs a constructor but symbol was given."); err(function() { var FakeConstructor = {}; var fakeInstanceB = 4; FakeConstructor[Symbol.hasInstance] = function (val) { return val === 3; }; expect(fakeInstanceB).to.be.an.instanceof(FakeConstructor); }, 'expected 4 to be an instance of an unnamed constructor') err(function() { var FakeConstructor = {}; var fakeInstanceB = 4; FakeConstructor[Symbol.hasInstance] = function (val) { return val === 4; }; expect(fakeInstanceB).to.not.be.an.instanceof(FakeConstructor); }, 'expected 4 to not be an instance of an unnamed constructor') } err(function(){ expect(3).to.an.instanceof(Foo, 'blah'); }, "blah: expected 3 to be an instance of Foo"); err(function(){ expect(3, 'blah').to.an.instanceof(Foo); }, "blah: expected 3 to be an instance of Foo"); }); it('within(start, finish)', function(){ expect(5).to.be.within(5, 10); expect(5).to.be.within(3, 6); expect(5).to.be.within(3, 5); expect(5).to.not.be.within(1, 3); expect('foo').to.have.length.within(2, 4); expect('foo').to.have.lengthOf.within(2, 4); expect([ 1, 2, 3 ]).to.have.length.within(2, 4); expect([ 1, 2, 3 ]).to.have.lengthOf.within(2, 4); err(function(){ expect(5).to.not.be.within(4, 6, 'blah'); }, "blah: expected 5 to not be within 4..6"); err(function(){ expect(5, 'blah').to.not.be.within(4, 6); }, "blah: expected 5 to not be within 4..6"); err(function(){ expect(10).to.be.within(50, 100, 'blah'); }, "blah: expected 10 to be within 50..100"); err(function () { expect('foo').to.have.length.within(5, 7, 'blah'); }, "blah: expected \'foo\' to have a length within 5..7"); err(function () { expect('foo', 'blah').to.have.length.within(5, 7); }, "blah: expected \'foo\' to have a length within 5..7"); err(function () { expect('foo').to.have.lengthOf.within(5, 7, 'blah'); }, "blah: expected \'foo\' to have a length within 5..7"); err(function () { expect([ 1, 2, 3 ]).to.have.length.within(5, 7, 'blah'); }, "blah: expected [ 1, 2, 3 ] to have a length within 5..7"); err(function () { expect([ 1, 2, 3 ]).to.have.lengthOf.within(5, 7, 'blah'); }, "blah: expected [ 1, 2, 3 ] to have a length within 5..7"); err(function () { expect(null).to.be.within(0, 1, 'blah'); }, "blah: expected null to be a number or a date"); err(function () { expect(null, 'blah').to.be.within(0, 1); }, "blah: expected null to be a number or a date"); err(function () { expect(1).to.be.within(null, 1, 'blah'); }, "blah: the arguments to within must be numbers"); err(function () { expect(1, 'blah').to.be.within(null, 1); }, "blah: the arguments to within must be numbers"); err(function () { expect(1).to.be.within(0, null, 'blah'); }, "blah: the arguments to within must be numbers"); err(function () { expect(1, 'blah').to.be.within(0, null); }, "blah: the arguments to within must be numbers"); err(function () { expect(null).to.not.be.within(0, 1, 'blah'); }, "blah: expected null to be a number or a date"); err(function () { expect(1).to.not.be.within(null, 1, 'blah'); }, "blah: the arguments to within must be numbers"); err(function () { expect(1).to.not.be.within(0, null, 'blah'); }, "blah: the arguments to within must be numbers"); err(function () { expect(1).to.have.length.within(5, 7, 'blah'); }, "blah: expected 1 to have property 'length'"); err(function () { expect(1, 'blah').to.have.length.within(5, 7); }, "blah: expected 1 to have property 'length'"); err(function () { expect(1).to.have.lengthOf.within(5, 7, 'blah'); }, "blah: expected 1 to have property 'length'"); if (typeof Map === 'function') { expect(new Map).to.have.length.within(0, 0); expect(new Map).to.have.lengthOf.within(0, 0); var map = new Map; map.set('a', 1); map.set('b', 2); map.set('c', 3); expect(map).to.have.length.within(2, 4); expect(map).to.have.lengthOf.within(2, 4); err(function () { expect(map).to.have.length.within(5, 7, 'blah'); }, "blah: expected Map{ 'a' => 1, 'b' => 2, 'c' => 3 } to have a size within 5..7"); err(function () { expect(map).to.have.lengthOf.within(5, 7, 'blah'); }, "blah: expected Map{ 'a' => 1, 'b' => 2, 'c' => 3 } to have a size within 5..7"); } if (typeof Set === 'function') { expect(new Set).to.have.length.within(0, 0); expect(new Set).to.have.lengthOf.within(0, 0); var set = new Set; set.add(1); set.add(2); set.add(3); expect(set).to.have.length.within(2, 4); expect(set).to.have.lengthOf.within(2, 4); err(function () { expect(set).to.have.length.within(5, 7, 'blah'); }, "blah: expected Set{ 1, 2, 3 } to have a size within 5..7"); err(function () { expect(set).to.have.lengthOf.within(5, 7, 'blah'); }, "blah: expected Set{ 1, 2, 3 } to have a size within 5..7"); } }); it('within(start, finish) (dates)', function(){ var now = new Date(); var oneSecondAgo = new Date(now.getTime() - 1000); var oneSecondAfter = new Date(now.getTime() + 1000); var nowISO = now.toISOString(); var beforeISO = oneSecondAgo.toISOString(); var afterISO = oneSecondAfter.toISOString(); expect(now).to.be.within(oneSecondAgo, oneSecondAfter); expect(now).to.be.within(now, oneSecondAfter); expect(now).to.be.within(now, now); expect(oneSecondAgo).to.not.be.within(now, oneSecondAfter); err(function(){ expect(now).to.not.be.within(now, oneSecondAfter, 'blah'); }, "blah: expected " + nowISO + " to not be within " + nowISO + ".." + afterISO); err(function(){ expect(now, 'blah').to.not.be.within(oneSecondAgo, oneSecondAfter); }, "blah: expected " + nowISO + " to not be within " + beforeISO + ".." + afterISO); err(function () { expect(now).to.have.length.within(5, 7, 'blah'); }, "blah: expected " + nowISO + " to have property 'length'"); err(function () { expect('foo').to.have.lengthOf.within(now, 7, 'blah'); }, "blah: the arguments to within must be numbers"); err(function () { expect(now).to.be.within(now, 1, 'blah'); }, "blah: the arguments to within must be dates"); err(function () { expect(now).to.be.within(null, now, 'blah'); }, "blah: the arguments to within must be dates"); err(function () { expect(now).to.be.within(now, undefined, 'blah'); }, "blah: the arguments to within must be dates"); err(function () { expect(now, 'blah').to.be.within(1, now); }, "blah: the arguments to within must be dates"); err(function () { expect(now, 'blah').to.be.within(now, 1); }, "blah: the arguments to within must be dates"); err(function () { expect(null).to.not.be.within(now, oneSecondAfter, 'blah'); }, "blah: expected null to be a number or a date"); }); it('above(n)', function(){ expect(5).to.be.above(2); expect(5).to.be.greaterThan(2); expect(5).to.not.be.above(5); expect(5).to.not.be.above(6); expect('foo').to.have.length.above(2); expect('foo').to.have.lengthOf.above(2); expect([ 1, 2, 3 ]).to.have.length.above(2); expect([ 1, 2, 3 ]).to.have.lengthOf.above(2); err(function(){ expect(5).to.be.above(6, 'blah'); }, "blah: expected 5 to be above 6"); err(function(){ expect(5, 'blah').to.be.above(6); }, "blah: expected 5 to be above 6"); err(function(){ expect(10).to.not.be.above(6, 'blah'); }, "blah: expected 10 to be at most 6"); err(function () { expect('foo').to.have.length.above(4, 'blah'); }, "blah: expected \'foo\' to have a length above 4 but got 3"); err(function () { expect('foo', 'blah').to.have.length.above(4); }, "blah: expected \'foo\' to have a length above 4 but got 3"); err(function () { expect('foo').to.have.lengthOf.above(4, 'blah'); }, "blah: expected \'foo\' to have a length above 4 but got 3"); err(function () { expect([ 1, 2, 3 ]).to.have.length.above(4, 'blah'); }, "blah: expected [ 1, 2, 3 ] to have a length above 4 but got 3"); err(function () { expect([ 1, 2, 3 ]).to.have.lengthOf.above(4, 'blah'); }, "blah: expected [ 1, 2, 3 ] to have a length above 4 but got 3"); err(function () { expect(null).to.be.above(0, 'blah'); }, "blah: expected null to be a number or a date"); err(function () { expect(null, 'blah').to.be.above(0); }, "blah: expected null to be a number or a date"); err(function () { expect(1).to.be.above(null, 'blah'); }, "blah: the argument to above must be a number"); err(function () { expect(1, 'blah').to.be.above(null); }, "blah: the argument to above must be a number"); err(function () { expect(null).to.not.be.above(0, 'blah'); }, "blah: expected null to be a number or a date"); err(function () { expect(1).to.not.be.above(null, 'blah'); }, "blah: the argument to above must be a number"); err(function () { expect(1).to.have.length.above(0, 'blah'); }, "blah: expected 1 to have property 'length'"); err(function () { expect(1, 'blah').to.have.length.above(0); }, "blah: expected 1 to have property 'length'"); err(function () { expect(1).to.have.lengthOf.above(0, 'blah'); }, "blah: expected 1 to have property 'length'"); if (typeof Map === 'function') { expect(new Map).to.have.length.above(-1); expect(new Map).to.have.lengthOf.above(-1); var map = new Map; map.set('a', 1); map.set('b', 2); map.set('c', 3); expect(map).to.have.length.above(2); expect(map).to.have.lengthOf.above(2); err(function () { expect(map).to.have.length.above(5, 'blah'); }, "blah: expected Map{ 'a' => 1, 'b' => 2, 'c' => 3 } to have a size above 5 but got 3"); err(function () { expect(map).to.have.lengthOf.above(5, 'blah'); }, "blah: expected Map{ 'a' => 1, 'b' => 2, 'c' => 3 } to have a size above 5 but got 3"); } if (typeof Set === 'function') { expect(new Set).to.have.length.above(-1); expect(new Set).to.have.lengthOf.above(-1); var set = new Set; set.add(1); set.add(2); set.add(3); expect(set).to.have.length.above(2); expect(set).to.have.lengthOf.above(2); err(function () { expect(set).to.have.length.above(5, 'blah'); }, "blah: expected Set{ 1, 2, 3 } to have a size above 5 but got 3"); err(function () { expect(set).to.have.lengthOf.above(5, 'blah'); }, "blah: expected Set{ 1, 2, 3 } to have a size above 5 but got 3"); } }); it('above(n) (dates)', function(){ var now = new Date(); var oneSecondAgo = new Date(now.getTime() - 1000); var oneSecondAfter = new Date(now.getTime() + 1000); expect(now).to.be.above(oneSecondAgo); expect(now).to.be.greaterThan(oneSecondAgo); expect(now).to.not.be.above(now); expect(now).to.not.be.above(oneSecondAfter); err(function(){ expect(now).to.be.above(oneSecondAfter, 'blah'); }, "blah: expected " + now.toISOString() + " to be above " + oneSecondAfter.toISOString()); err(function(){ expect(10).to.not.be.above(6, 'blah'); }, "blah: expected 10 to be at most 6"); err(function () { expect(now).to.have.length.above(4, 'blah'); }, "blah: expected " + now.toISOString() + " to have property 'length'"); err(function () { expect([ 1, 2, 3 ]).to.have.length.above(now, 'blah'); }, "blah: the argument to above must be a number"); err(function () { expect(null).to.be.above(now, 'blah'); }, "blah: expected null to be a number or a date"); err(function () { expect(now).to.be.above(null, 'blah'); }, "blah: the argument to above must be a date"); err(function () { expect(null).to.have.length.above(0, 'blah'); }, "blah: Target cannot be null or undefined."); }); it('least(n)', function(){ expect(5).to.be.at.least(2); expect(5).to.be.at.least(5); expect(5).to.not.be.at.least(6); expect('foo').to.have.length.of.at.least(2); expect('foo').to.have.lengthOf.at.least(2); expect([ 1, 2, 3 ]).to.have.length.of.at.least(2); expect([ 1, 2, 3 ]).to.have.lengthOf.at.least(2); err(function(){ expect(5).to.be.at.least(6, 'blah'); }, "blah: expected 5 to be at least 6"); err(function(){ expect(5, 'blah').to.be.at.least(6); }, "blah: expected 5 to be at least 6"); err(function(){ expect(10).to.not.be.at.least(6, 'blah'); }, "blah: expected 10 to be below 6"); err(function () { expect('foo').to.have.length.of.at.least(4, 'blah'); }, "blah: expected \'foo\' to have a length at least 4 but got 3"); err(function () { expect('foo', 'blah').to.have.length.of.at.least(4); }, "blah: expected \'foo\' to have a length at least 4 but got 3"); err(function () { expect('foo').to.have.lengthOf.at.least(4, 'blah'); }, "blah: expected \'foo\' to have a length at least 4 but got 3"); err(function () { expect([ 1, 2, 3 ]).to.have.length.of.at.least(4, 'blah'); }, "blah: expected [ 1, 2, 3 ] to have a length at least 4 but got 3"); err(function () { expect([ 1, 2, 3 ]).to.have.lengthOf.at.least(4, 'blah'); }, "blah: expected [ 1, 2, 3 ] to have a length at least 4 but got 3"); err(function () { expect([ 1, 2, 3, 4 ]).to.not.have.length.of.at.least(4, 'blah'); }, "blah: expected [ 1, 2, 3, 4 ] to have a length below 4"); err(function () { expect([ 1, 2, 3, 4 ]).to.not.have.lengthOf.at.least(4, 'blah'); }, "blah: expected [ 1, 2, 3, 4 ] to have a length below 4"); err(function () { expect(null).to.be.at.least(0, 'blah'); }, "blah: expected null to be a number or a date"); err(function () { expect(null, 'blah').to.be.at.least(0); }, "blah: expected null to be a number or a date"); err(function () { expect(1).to.be.at.least(null, 'blah'); }, "blah: the argument to least must be a number"); err(function () { expect(1, 'blah').to.be.at.least(null); }, "blah: the argument to least must be a number"); err(function () { expect(null).to.not.be.at.least(0, 'blah'); }, "blah: expected null to be a number or a date"); err(function () { expect(1).to.not.be.at.least(null, 'blah'); }, "blah: the argument to least must be a number"); err(function () { expect(1).to.have.length.at.least(0, 'blah'); }, "blah: expected 1 to have property 'length'"); err(function () { expect(1, 'blah').to.have.length.at.least(0); }, "blah: expected 1 to have property 'length'"); err(function () { expect(1).to.have.lengthOf.at.least(0, 'blah'); }, "blah: expected 1 to have property 'length'"); if (typeof Map === 'function') { expect(new Map).to.have.length.of.at.least(0); expect(new Map).to.have.lengthOf.at.least(0); var map = new Map; map.set('a', 1); map.set('b', 2); map.set('c', 3); expect(map).to.have.length.of.at.least(3); expect(map).to.have.lengthOf.at.least(3); err(function () { expect(map).to.have.length.of.at.least(4, 'blah'); }, "blah: expected Map{ 'a' => 1, 'b' => 2, 'c' => 3 } to have a size at least 4 but got 3"); err(function () { expect(map).to.have.lengthOf.at.least(4, 'blah'); }, "blah: expected Map{ 'a' => 1, 'b' => 2, 'c' => 3 } to have a size at least 4 but got 3"); } if (typeof Set === 'function') { expect(new Set).to.have.length.of.at.least(0); expect(new Set).to.have.lengthOf.at.least(0); var set = new Set; set.add(1); set.add(2); set.add(3); expect(set).to.have.length.of.at.least(3); expect(set).to.have.lengthOf.at.least(3); err(function () { expect(set).to.have.length.of.at.least(4, 'blah'); }, "blah: expected Set{ 1, 2, 3 } to have a size at least 4 but got 3"); err(function () { expect(set).to.have.lengthOf.at.least(4, 'blah'); }, "blah: expected Set{ 1, 2, 3 } to have a size at least 4 but got 3"); } }); it('below(n)', function(){ expect(2).to.be.below(5); expect(2).to.be.lessThan(5); expect(2).to.not.be.below(2); expect(2).to.not.be.below(1); expect('foo').to.have.length.below(4); expect('foo').to.have.lengthOf.below(4); expect([ 1, 2, 3 ]).to.have.length.below(4); expect([ 1, 2, 3 ]).to.have.lengthOf.below(4); err(function(){ expect(6).to.be.below(5, 'blah'); }, "blah: expected 6 to be below 5"); err(function(){ expect(6, 'blah').to.be.below(5); }, "blah: expected 6 to be below 5"); err(function(){ expect(6).to.not.be.below(10, 'blah'); }, "blah: expected 6 to be at least 10"); err(function () { expect('foo').to.have.length.below(2, 'blah'); }, "blah: expected \'foo\' to have a length below 2 but got 3"); err(function () { expect('foo', 'blah').to.have.length.below(2); }, "blah: expected \'foo\' to have a length below 2 but got 3"); err(function () { expect('foo').to.have.lengthOf.below(2, 'blah'); }, "blah: expected \'foo\' to have a length below 2 but got 3"); err(function () { expect([ 1, 2, 3 ]).to.have.length.below(2, 'blah'); }, "blah: expected [ 1, 2, 3 ] to have a length below 2 but got 3"); err(function () { expect([ 1, 2, 3 ]).to.have.lengthOf.below(2, 'blah'); }, "blah: expected [ 1, 2, 3 ] to have a length below 2 but got 3"); err(function () { expect(null).to.be.below(0, 'blah'); }, "blah: expected null to be a number or a date"); err(function () { expect(null, 'blah').to.be.below(0); }, "blah: expected null to be a number or a date"); err(function () { expect(1).to.be.below(null, 'blah'); }, "blah: the argument to below must be a number"); err(function () { expect(1, 'blah').to.be.below(null); }, "blah: the argument to below must be a number"); err(function () { expect(null).to.not.be.below(0, 'blah'); }, "blah: expected null to be a number or a date"); err(function () { expect(1).to.not.be.below(null, 'blah'); }, "blah: the argument to below must be a number"); err(function () { expect(1).to.have.length.below(0, 'blah'); }, "blah: expected 1 to have property 'length'"); err(function () { expect(1, 'blah').to.have.length.below(0); }, "blah: expected 1 to have property 'length'"); err(function () { expect(1).to.have.lengthOf.below(0, 'blah'); }, "blah: expected 1 to have property 'length'"); if (typeof Map === 'function') { expect(new Map).to.have.length.below(1); expect(new Map).to.have.lengthOf.below(1); var map = new Map; map.set('a', 1); map.set('b', 2); map.set('c', 3); expect(map).to.have.length.below(4); expect(map).to.have.lengthOf.below(4); err(function () { expect(map).to.have.length.below(2, 'blah'); }, "blah: expected Map{ 'a' => 1, 'b' => 2, 'c' => 3 } to have a size below 2 but got 3"); err(function () { expect(map).to.have.lengthOf.below(2, 'blah'); }, "blah: expected Map{ 'a' => 1, 'b' => 2, 'c' => 3 } to have a size below 2 but got 3"); } if (typeof Set === 'function') { expect(new Set).to.have.length.below(1); expect(new Set).to.have.lengthOf.below(1); var set = new Set; set.add(1); set.add(2); set.add(3); expect(set).to.have.length.below(4); expect(set).to.have.lengthOf.below(4); err(function () { expect(set).to.have.length.below(2, 'blah'); }, "blah: expected Set{ 1, 2, 3 } to have a size below 2 but got 3"); err(function () { expect(set).to.have.lengthOf.below(2, 'blah'); }, "blah: expected Set{ 1, 2, 3 } to have a size below 2 but got 3"); } }); it('below(n) (dates)', function(){ var now = new Date(); var oneSecondAgo = new Date(now.getTime() - 1000); var oneSecondAfter = new Date(now.getTime() + 1000); expect(now).to.be.below(oneSecondAfter); expect(oneSecondAgo).to.be.lessThan(now); expect(now).to.not.be.below(oneSecondAgo); expect(oneSecondAfter).to.not.be.below(oneSecondAgo); err(function(){ expect(now).to.be.below(oneSecondAgo, 'blah'); }, "blah: expected " + now.toISOString() + " to be below " + oneSecondAgo.toISOString()); err(function(){ expect(now).to.not.be.below(oneSecondAfter, 'blah'); }, "blah: expected " + now.toISOString() + " to be at least " + oneSecondAfter.toISOString()); err(function () { expect('foo').to.have.length.below(2, 'blah'); }, "blah: expected \'foo\' to have a length below 2 but got 3"); err(function () { expect(null).to.be.below(now, 'blah'); }, "blah: expected null to be a number or a date"); err(function () { expect(1).to.be.below(null, 'blah'); }, "blah: the argument to below must be a number"); err(function () { expect(now).to.not.be.below(null, 'blah'); }, "blah: the argument to below must be a date"); err(function () { expect(now).to.have.length.below(0, 'blah'); }, "blah: expected " + now.toISOString() + " to have property 'length'"); err(function () { expect('asdasd').to.have.length.below(now, 'blah'); }, "blah: the argument to below must be a number"); }); it('most(n)', function(){ expect(2).to.be.at.most(5); expect(2).to.be.at.most(2); expect(2).to.not.be.at.most(1); expect('foo').to.have.length.of.at.most(4); expect('foo').to.have.lengthOf.at.most(4); expect([ 1, 2, 3 ]).to.have.length.of.at.most(4); expect([ 1, 2, 3 ]).to.have.lengthOf.at.most(4); err(function(){ expect(6).to.be.at.most(5, 'blah'); }, "blah: expected 6 to be at most 5"); err(function(){ expect(6, 'blah').to.be.at.most(5); }, "blah: expected 6 to be at most 5"); err(function(){ expect(6).to.not.be.at.most(10, 'blah'); }, "blah: expected 6 to be above 10"); err(function () { expect('foo').to.have.length.of.at.most(2, 'blah'); }, "blah: expected \'foo\' to have a length at most 2 but got 3"); err(function () { expect('foo', 'blah').to.have.length.of.at.most(2); }, "blah: expected \'foo\' to have a length at most 2 but got 3"); err(function () { expect('foo').to.have.lengthOf.at.most(2, 'blah'); }, "blah: expected \'foo\' to have a length at most 2 but got 3"); err(function () { expect([ 1, 2, 3 ]).to.have.length.of.at.most(2, 'blah'); }, "blah: expected [ 1, 2, 3 ] to have a length at most 2 but got 3"); err(function () { expect([ 1, 2, 3 ]).to.have.lengthOf.at.most(2, 'blah'); }, "blah: expected [ 1, 2, 3 ] to have a length at most 2 but got 3"); err(function () { expect([ 1, 2 ]).to.not.have.length.of.at.most(2, 'blah'); }, "blah: expected [ 1, 2 ] to have a length above 2"); err(function () { expect([ 1, 2 ]).to.not.have.lengthOf.at.most(2, 'blah'); }, "blah: expected [ 1, 2 ] to have a length above 2"); err(function () { expect(null).to.be.at.most(0, 'blah'); }, "blah: expected null to be a number or a date"); err(function () { expect(null, 'blah').to.be.at.most(0); }, "blah: expected null to be a number or a date"); err(function () { expect(1).to.be.at.most(null, 'blah'); }, "blah: the argument to most must be a number"); err(function () { expect(1, 'blah').to.be.at.most(null); }, "blah: the argument to most must be a number"); err(function () { expect(null).to.not.be.at.most(0, 'blah'); }, "blah: expected null to be a number or a date"); err(function () { expect(1).to.not.be.at.most(null, 'blah'); }, "blah: the argument to most must be a number"); err(function () { expect(1).to.have.length.of.at.most(0, 'blah'); }, "blah: expected 1 to have property 'length'"); err(function () { expect(1, 'blah').to.have.length.of.at.most(0); }, "blah: expected 1 to have property 'length'"); err(function () { expect(1).to.have.lengthOf.at.most(0, 'blah'); }, "blah: expected 1 to have property 'length'"); if (typeof Map === 'function') { expect(new Map).to.have.length.of.at.most(0); expect(new Map).to.have.lengthOf.at.most(0); var map = new Map; map.set('a', 1); map.set('b', 2); map.set('c', 3); expect(map).to.have.length.of.at.most(3); expect(map).to.have.lengthOf.at.most(3); err(function () { expect(map).to.have.length.of.at.most(2, 'blah'); }, "blah: expected Map{ 'a' => 1, 'b' => 2, 'c' => 3 } to have a size at most 2 but got 3"); err(function () { expect(map).to.have.lengthOf.at.most(2, 'blah'); }, "blah: expected Map{ 'a' => 1, 'b' => 2, 'c' => 3 } to have a size at most 2 but got 3"); } if (typeof Set === 'function') { expect(new Set).to.have.length.of.at.most(0); expect(new Set).to.have.lengthOf.at.most(0); var set = new Set; set.add(1); set.add(2); set.add(3); expect(set).to.have.length.of.at.most(3); expect(set).to.have.lengthOf.at.most(3); err(function () { expect(set).to.have.length.of.at.most(2, 'blah'); }, "blah: expected Set{ 1, 2, 3 } to have a size at most 2 but got 3"); err(function () { expect(set).to.have.lengthOf.at.most(2, 'blah'); }, "blah: expected Set{ 1, 2, 3 } to have a size at most 2 but got 3"); } }); it('most(n) (dates)', function(){ var now = new Date(); var oneSecondBefore = new Date(now.getTime() - 1000); var oneSecondAfter = new Date(now.getTime() + 1000); var nowISO = now.toISOString(); var beforeISO = oneSecondBefore.toISOString(); var afterISO = oneSecondAfter.toISOString(); expect(now).to.be.at.most(oneSecondAfter); expect(now).to.be.at.most(now); expect(now).to.not.be.at.most(oneSecondBefore); err(function(){ expect(now).to.be.at.most(oneSecondBefore, 'blah'); }, "blah: expected " + nowISO + " to be at most " + beforeISO); err(function(){ expect(now).to.not.be.at.most(now, 'blah'); }, "blah: expected " + nowISO + " to be above " + nowISO); err(function () { expect(now).to.have.length.of.at.most(2, 'blah'); }, "blah: expected " + nowISO + " to have property 'length'"); err(function () { expect('foo', 'blah').to.have.length.of.at.most(now); }, "blah: the argument to most must be a number"); err(function () { expect([ 1, 2, 3 ]).to.not.have.length.of.at.most(now, 'blah'); }, "blah: the argument to most must be a number"); err(function () { expect(null).to.be.at.most(now, 'blah'); }, "blah: expected null to be a number or a date"); err(function () { expect(now, 'blah').to.be.at.most(null); }, "blah: the argument to most must be a date"); err(function () { expect(1).to.be.at.most(now, 'blah'); }, "blah: the argument to most must be a number"); err(function () { expect(now, 'blah').to.be.at.most(1); }, "blah: the argument to most must be a date"); err(function () { expect(now).to.not.be.at.most(undefined, 'blah'); }, "blah: the argument to most must be a date"); }); it('match(regexp)', function(){ expect('foobar').to.match(/^foo/) expect('foobar').to.matches(/^foo/) expect('foobar').to.not.match(/^bar/) err(function(){ expect('foobar').to.match(/^bar/i, 'blah') }, "blah: expected 'foobar' to match /^bar/i"); err(function(){ expect('foobar', 'blah').to.match(/^bar/i) }, "blah: expected 'foobar' to match /^bar/i"); err(function(){ expect('foobar').to.matches(/^bar/i, 'blah') }, "blah: expected 'foobar' to match /^bar/i"); err(function(){ expect('foobar').to.not.match(/^foo/i, 'blah') }, "blah: expected 'foobar' not to match /^foo/i"); }); it('lengthOf(n)', function(){ expect('test').to.have.length(4); expect('test').to.have.lengthOf(4); expect('test').to.not.have.length(3); expect('test').to.not.have.lengthOf(3); expect([1,2,3]).to.have.length(3); expect([1,2,3]).to.have.lengthOf(3); err(function(){ expect(4).to.have.length(3, 'blah'); }, 'blah: expected 4 to have property \'length\''); err(function(){ expect(4, 'blah').to.have.length(3); }, 'blah: expected 4 to have property \'length\''); err(function(){ expect(4).to.have.lengthOf(3, 'blah'); }, 'blah: expected 4 to have property \'length\''); err(function(){ expect('asd').to.not.have.length(3, 'blah'); }, "blah: expected 'asd' to not have a length of 3"); err(function(){ expect('asd').to.not.have.lengthOf(3, 'blah'); }, "blah: expected 'asd' to not have a length of 3"); if (typeof Map === 'function') { expect(new Map).to.have.length(0); expect(new Map).to.have.lengthOf(0); var map = new Map; map.set('a', 1); map.set('b', 2); map.set('c', 3); expect(map).to.have.length(3); expect(map).to.have.lengthOf(3); err(function(){ expect(map).to.not.have.length(3, 'blah'); }, "blah: expected Map{ 'a' => 1, 'b' => 2, 'c' => 3 } to not have a size of 3"); err(function(){ expect(map).to.not.have.lengthOf(3, 'blah'); }, "blah: expected Map{ 'a' => 1, 'b' => 2, 'c' => 3 } to not have a size of 3"); } if (typeof Set === 'function') { expect(new Set).to.have.length(0); expect(new Set).to.have.lengthOf(0); var set = new Set; set.add(1); set.add(2); set.add(3); expect(set).to.have.length(3); expect(set).to.have.lengthOf(3); err(function(){ expect(set).to.not.have.length(3, 'blah'); }, "blah: expected Set{ 1, 2, 3 } to not have a size of 3"); err(function(){ expect(set).to.not.have.lengthOf(3, 'blah');; }, "blah: expected Set{ 1, 2, 3 } to not have a size of 3"); } }); it('eql(val)', function(){ expect('test').to.eql('test'); expect({ foo: 'bar' }).to.eql({ foo: 'bar' }); expect(1).to.eql(1); expect('4').to.not.eql(4); if (typeof Symbol === 'function') { var sym = Symbol(); expect(sym).to.eql(sym); } err(function(){ expect(4).to.eql(3, 'blah'); }, 'blah: expected 4 to deeply equal 3'); }); if ('undefined' !== typeof Buffer) { it('Buffer eql()', function () { expect(Buffer.from([ 1 ])).to.eql(Buffer.from([ 1 ])); err(function () { expect(Buffer.from([ 0 ])).to.eql(Buffer.from([ 1 ])); }, 'expected Buffer[ 0 ] to deeply equal Buffer[ 1 ]'); }); } it('equal(val)', function(){ expect('test').to.equal('test'); expect(1).to.equal(1); if (typeof Symbol === 'function') { var sym = Symbol(); expect(sym).to.equal(sym); } err(function(){ expect(4).to.equal(3, 'blah'); }, 'blah: expected 4 to equal 3'); err(function(){ expect(4, 'blah').to.equal(3); }, 'blah: expected 4 to equal 3'); err(function(){ expect('4').to.equal(4, 'blah'); }, "blah: expected '4' to equal 4"); }); it('deep.equal(val)', function(){ expect({ foo: 'bar' }).to.deep.equal({ foo: 'bar' }); expect({ foo: 'bar' }).not.to.deep.equal({ foo: 'baz' }); err(function(){ expect({foo: 'bar'}).to.deep.equal({foo: 'baz'}, 'blah'); }, "blah: expected { foo: 'bar' } to deeply equal { foo: 'baz' }"); err(function(){ expect({foo: 'bar'}, 'blah').to.deep.equal({foo: 'baz'}); }, "blah: expected { foo: 'bar' } to deeply equal { foo: 'baz' }"); err(function(){ expect({foo: 'bar'}).to.not.deep.equal({foo: 'bar'}, 'blah'); }, "blah: expected { foo: 'bar' } to not deeply equal { foo: 'bar' }"); err(function(){ expect({foo: 'bar'}, 'blah').to.not.deep.equal({foo: 'bar'}); }, "blah: expected { foo: 'bar' } to not deeply equal { foo: 'bar' }"); }); it('deep.equal(/regexp/)', function(){ expect(/a/).to.deep.equal(/a/); expect(/a/).not.to.deep.equal(/b/); expect(/a/).not.to.deep.equal({}); expect(/a/g).to.deep.equal(/a/g); expect(/a/g).not.to.deep.equal(/b/g); expect(/a/i).to.deep.equal(/a/i); expect(/a/i).not.to.deep.equal(/b/i); expect(/a/m).to.deep.equal(/a/m); expect(/a/m).not.to.deep.equal(/b/m); }); it('deep.equal(Date)', function(){ var a = new Date(1, 2, 3) , b = new Date(4, 5, 6); expect(a).to.deep.equal(a); expect(a).not.to.deep.equal(b); expect(a).not.to.deep.equal({}); }); it('empty', function(){ function FakeArgs() {}; FakeArgs.prototype.length = 0; expect('').to.be.empty; expect('foo').not.to.be.empty; expect([]).to.be.empty; expect(['foo']).not.to.be.empty; expect(new FakeArgs).to.be.empty; expect({arguments: 0}).not.to.be.empty; expect({}).to.be.empty; expect({foo: 'bar'}).not.to.be.empty; if (typeof WeakMap === 'function') { err(function(){ expect(new WeakMap, 'blah').not.to.be.empty; }, "blah: .empty was passed a weak collection"); } if (typeof WeakSet === 'function') { err(function(){ expect(new WeakSet, 'blah').not.to.be.empty; }, "blah: .empty was passed a weak collection"); } if (typeof Map === 'function') { expect(new Map).to.be.empty; // Not using Map constructor args because not supported in IE 11. var map = new Map; map.set('a', 1); expect(map).not.to.be.empty; err(function(){ expect(new Map).not.to.be.empty; }, "expected Map{} not to be empty"); map = new Map; map.key = 'val'; expect(map).to.be.empty; err(function(){ expect(map).not.to.be.empty; }, "expected Map{} not to be empty"); } if (typeof Set === 'function') { expect(new Set).to.be.empty; // Not using Set constructor args because not supported in IE 11. var set = new Set; set.add(1); expect(set).not.to.be.empty; err(function(){ expect(new Set).not.to.be.empty; }, "expected Set{} not to be empty"); set = new Set; set.key = 'val'; expect(set).to.be.empty; err(function(){ expect(set).not.to.be.empty; }, "expected Set{} not to be empty"); } err(function(){ expect('', 'blah').not.to.be.empty; }, "blah: expected \'\' not to be empty"); err(function(){ expect('foo').to.be.empty; }, "expected \'foo\' to be empty"); err(function(){ expect([]).not.to.be.empty; }, "expected [] not to be empty"); err(function(){ expect(['foo']).to.be.empty; }, "expected [ \'foo\' ] to be empty"); err(function(){ expect(new FakeArgs).not.to.be.empty; }, "expected FakeArgs{} not to be empty"); err(function(){ expect({arguments: 0}).to.be.empty; }, "expected { arguments: +0 } to be empty"); err(function(){ expect({}).not.to.be.empty; }, "expected {} not to be empty"); err(function(){ expect({foo: 'bar'}).to.be.empty; }, "expected { foo: \'bar\' } to be empty"); err(function(){ expect(null, 'blah').to.be.empty; }, "blah: .empty was passed non-string primitive null"); err(function(){ expect(undefined).to.be.empty; }, ".empty was passed non-string primitive undefined"); err(function(){ expect().to.be.empty; }, ".empty was passed non-string primitive undefined"); err(function(){ expect(null).to.not.be.empty; }, ".empty was passed non-string primitive null"); err(function(){ expect(undefined).to.not.be.empty; }, ".empty was passed non-string primitive undefined"); err(function(){ expect().to.not.be.empty; }, ".empty was passed non-string primitive undefined"); err(function(){ expect(0).to.be.empty; }, ".empty was passed non-string primitive +0"); err(function(){ expect(1).to.be.empty; }, ".empty was passed non-string primitive 1"); err(function(){ expect(true).to.be.empty; }, ".empty was passed non-string primitive true"); err(function(){ expect(false).to.be.empty; }, ".empty was passed non-string primitive false"); if (typeof Symbol !== 'undefined') { err(function(){ expect(Symbol()).to.be.empty; }, ".empty was passed non-string primitive Symbol()"); err(function(){ expect(Symbol.iterator).to.be.empty; }, ".empty was passed non-string primitive Symbol(Symbol.iterator)"); } err(function(){ expect(function() {}, 'blah').to.be.empty; }, "blah: .empty was passed a function"); if (FakeArgs.name === 'FakeArgs') { err(function(){ expect(FakeArgs).to.be.empty; }, ".empty was passed a function FakeArgs"); } }); it('NaN', function() { expect(NaN).to.be.NaN; expect(undefined).not.to.be.NaN; expect(Infinity).not.to.be.NaN; expect('foo').not.to.be.NaN; expect({}).not.to.be.NaN; expect(4).not.to.be.NaN; expect([]).not.to.be.NaN; err(function(){ expect(NaN, 'blah').not.to.be.NaN; }, "blah: expected NaN not to be NaN"); err(function(){ expect(undefined).to.be.NaN; }, "expected undefined to be NaN"); err(function(){ expect(Infinity).to.be.NaN; }, "expected Infinity to be NaN"); err(function(){ expect('foo').to.be.NaN; }, "expected 'foo' to be NaN"); err(function(){ expect({}).to.be.NaN; }, "expected {} to be NaN"); err(function(){ expect(4).to.be.NaN; }, "expected 4 to be NaN"); err(function(){ expect([]).to.be.NaN; }, "expected [] to be NaN"); }); it('finite', function() { expect(4).to.be.finite; expect(-10).to.be.finite; err(function(){ expect(NaN, 'blah').to.be.finite; }, "blah: expected NaN to be a finite number"); err(function(){ expect(Infinity).to.be.finite; }, "expected Infinity to be a finite number"); err(function(){ expect('foo').to.be.finite; }, "expected \'foo\' to be a finite number"); err(function(){ expect([]).to.be.finite; }, "expected [] to be a finite number"); err(function(){ expect({}).to.be.finite; }, "expected {} to be a finite number"); }); it('property(name)', function(){ expect('test').to.have.property('length'); expect({a: 1}).to.have.property('toString'); expect(4).to.not.have.property('length'); expect({ 'foo.bar': 'baz' }) .to.have.property('foo.bar'); expect({ foo: { bar: 'baz' } }) .to.not.have.property('foo.bar'); // Properties with the value 'undefined' are still properties var obj = { foo: undefined }; Object.defineProperty(obj, 'bar', { get: function() { } }); expect(obj).to.have.property('foo'); expect(obj).to.have.property('bar'); expect({ 'foo.bar[]': 'baz'}) .to.have.property('foo.bar[]'); err(function(){ expect('asd').to.have.property('foo'); }, "expected 'asd' to have property 'foo'"); err(function(){ expect('asd', 'blah').to.have.property('foo'); }, "blah: expected 'asd' to have property 'foo'"); err(function(){ expect({ foo: { bar: 'baz' } }) .to.have.property('foo.bar'); }, "expected { foo: { bar: 'baz' } } to have property 'foo.bar'"); err(function() { expect({a: {b: 1}}).to.have.own.nested.property("a.b"); }, "The \"nested\" and \"own\" flags cannot be combined."); err(function() { expect({a: {b: 1}}, 'blah').to.have.own.nested.property("a.b"); }, "blah: The \"nested\" and \"own\" flags cannot be combined."); err(function () { expect(null, 'blah').to.have.property("a"); }, "blah: Target cannot be null or undefined."); err(function () { expect(undefined, 'blah').to.have.property("a"); }, "blah: Target cannot be null or undefined."); err(function () { expect({a:1}, 'blah').to.have.property(null) }, "blah: the argument to property must be a string, number, or symbol"); }); it('property(name, val)', function(){ expect('test').to.have.property('length', 4); expect('asd').to.have.property('constructor', String); expect({a: 1}).to.have.property('toString', Object.prototype.toString); expect('test').to.not.have.property('length', 3); expect('test').to.not.have.property('foo', 4); expect({a: {b: 1}}).to.not.have.property('a', {b: 1}); var deepObj = { green: { tea: 'matcha' } , teas: [ 'chai', 'matcha', { tea: 'konacha' } ] }; expect(deepObj).to.have.nested.property('green.tea', 'matcha'); expect(deepObj).to.have.nested.property('teas[1]', 'matcha'); expect(deepObj).to.have.nested.property('teas[2].tea', 'konacha'); expect(deepObj).to.have.property('teas') .that.is.an('array') .with.nested.property('[2]') .that.deep.equals({tea: 'konacha'}); err(function(){ expect(deepObj).to.have.nested.property('teas[3]'); }, "expected { green: { tea: 'matcha' }, …(1) } to have nested property 'teas[3]'"); err(function(){ expect(deepObj).to.have.nested.property('teas[3]', 'bar'); }, "expected { green: { tea: 'matcha' }, …(1) } to have nested property 'teas[3]'"); err(function(){ expect(deepObj).to.have.nested.property('teas[3].tea', 'bar'); }, "expected { green: { tea: 'matcha' }, …(1) } to have nested property 'teas[3].tea'"); var arr = [ [ 'chai', 'matcha', 'konacha' ] , [ { tea: 'chai' } , { tea: 'matcha' } , { tea: 'konacha' } ] ]; expect(arr).to.have.nested.property('[0][1]', 'matcha'); expect(arr).to.have.nested.property('[1][2].tea', 'konacha'); err(function(){ expect(arr).to.have.nested.property('[2][1]'); }, "expected [ …(2) ] to have nested property '[2][1]'"); err(function(){ expect(arr).to.have.nested.property('[2][1]', 'none'); }, "expected [ …(2) ] to have nested property '[2][1]'"); err(function(){ expect(arr).to.have.nested.property('[0][3]', 'none'); }, "expected [ …(2) ] to have nested property '[0][3]'"); err(function(){ expect('asd').to.have.property('length', 4, 'blah'); }, "blah: expected 'asd' to have property 'length' of 4, but got 3"); err(function(){ expect('asd', 'blah').to.have.property('length', 4); }, "blah: expected 'asd' to have property 'length' of 4, but got 3"); err(function(){ expect('asd').to.not.have.property('length', 3, 'blah'); }, "blah: expected 'asd' to not have property 'length' of 3"); err(function(){ expect('asd').to.have.property('constructor', Number, 'blah'); }, "blah: expected 'asd' to have property 'constructor' of [Function Number], but got [Function String]"); err(function() { expect({a: {b: 1}}).to.have.own.nested.property("a.b", 1, 'blah'); }, "blah: The \"nested\" and \"own\" flags cannot be combined."); err(function() { expect({a: {b: 1}}, 'blah').to.have.own.nested.property("a.b", 1); }, "blah: The \"nested\" and \"own\" flags cannot be combined."); }); it('deep.property(name, val)', function () { var obj = {a: {b: 1}}; expect(obj).to.have.deep.property('a', {b: 1}); expect(obj).to.not.have.deep.property('a', {b: 7}); expect(obj).to.not.have.deep.property('a', {z: 1}); expect(obj).to.not.have.deep.property('z', {b: 1}); err(function () { expect(obj).to.have.deep.property('a', {b: 7}, 'blah'); }, "blah: expected { a: { b: 1 } } to have deep property 'a' of { b: 7 }, but got { b: 1 }"); err(function () { expect(obj).to.have.deep.property('z', {b: 1}, 'blah'); }, "blah: expected { a: { b: 1 } } to have deep property 'z'"); err(function () { expect(obj).to.not.have.deep.property('a', {b: 1}, 'blah'); }, "blah: expected { a: { b: 1 } } to not have deep property 'a' of { b: 1 }"); }); it('own.property(name)', function(){ expect('test').to.have.own.property('length'); expect('test').to.have.ownProperty('length'); expect('test').to.haveOwnProperty('length'); expect('test').to.not.have.own.property('iDontExist'); expect('test').to.not.have.ownProperty('iDontExist'); expect('test').to.not.haveOwnProperty('iDontExist'); expect({a: 1}).to.not.have.own.property('toString'); expect({a: 1}).to.not.have.ownProperty('toString'); expect({a: 1}).to.not.haveOwnProperty('toString'); expect({ length: 12 }).to.have.own.property('length'); expect({ length: 12 }).to.have.ownProperty('length'); expect({ length: 12 }).to.haveOwnProperty('length'); expect({ length: 12 }).to.not.have.own.property('iDontExist'); expect({ length: 12 }).to.not.have.ownProperty('iDontExist'); expect({ length: 12 }).to.not.haveOwnProperty('iDontExist'); // Chaining property's value expect('test').to.have.own.property('length').that.is.a('number'); expect('test').to.have.ownProperty('length').that.is.a('number'); expect('test').to.haveOwnProperty('length').that.is.a('number'); err(function(){ expect({ length: 12 }, 'blah').to.have.own.property('iDontExist'); }, "blah: expected { length: 12 } to have own property 'iDontExist'"); err(function(){ expect({ length: 12 }).to.not.have.own.property('length'); }, "expected { length: 12 } to not have own property 'length'"); err(function(){ expect({ length: 12 }, 'blah').to.have.ownProperty('iDontExist'); }, "blah: expected { length: 12 } to have own property 'iDontExist'"); err(function(){ expect({ length: 12 }).to.not.have.ownProperty('length'); }, "expected { length: 12 } to not have own property 'length'"); err(function(){ expect({ length: 12 }, 'blah').to.haveOwnProperty('iDontExist'); }, "blah: expected { length: 12 } to have own property 'iDontExist'"); err(function(){ expect({ length: 12 }).to.not.haveOwnProperty('length'); }, "expected { length: 12 } to not have own property 'length'"); }); it('own.property(name, value)', function(){ expect('test').to.have.own.property('length', 4); expect('test').to.have.ownProperty('length', 4); expect('test').to.haveOwnProperty('length', 4); expect('test').to.not.have.own.property('length', 1337); expect('test').to.not.have.ownProperty('length', 1337); expect('test').to.not.haveOwnProperty('length', 1337); expect({a: 1}).to.not.have.own.property('toString', Object.prototype.toString); expect({a: 1}).to.not.have.ownProperty('toString', Object.prototype.toString); expect({a: 1}).to.not.haveOwnProperty('toString', Object.prototype.toString); expect({a: {b: 1}}).to.not.have.own.property('a', {b: 1}); expect({a: {b: 1}}).to.not.have.ownProperty('a', {b: 1}); expect({a: {b: 1}}).to.not.haveOwnProperty('a', {b: 1}); expect({ length: 12 }).to.have.own.property('length', 12); expect({ length: 12 }).to.have.ownProperty('length', 12); expect({ length: 12 }).to.haveOwnProperty('length', 12); expect({ length: 12 }).to.not.have.own.property('length', 15); expect({ length: 12 }).to.not.have.ownProperty('length', 15); expect({ length: 12 }).to.not.haveOwnProperty('length', 15); // Chaining property's value expect('test').to.have.own.property('length', 4).that.is.a('number'); expect('test').to.have.ownProperty('length', 4).that.is.a('number'); expect('test').to.haveOwnProperty('length', 4).that.is.a('number'); var objNoProto = Object.create(null); objNoProto.a = 'a'; expect(objNoProto).to.have.own.property('a'); expect(objNoProto).to.have.ownProperty('a'); expect(objNoProto).to.haveOwnProperty('a'); err(function(){ expect({ length: 12 }).to.have.own.property('iDontExist', 12, 'blah'); }, "blah: expected { length: 12 } to have own property 'iDontExist'"); err(function(){ expect({ length: 12 }, 'blah').to.have.own.property('iDontExist', 12); }, "blah: expected { length: 12 } to have own property 'iDontExist'"); err(function() { expect({ length: 12 }).to.not.have.own.property('length', 12); }, "expected { length: 12 } to not have own property 'length' of 12"); err(function() { expect({ length: 12 }).to.have.own.property('length', 15); }, "expected { length: 12 } to have own property 'length' of 15, but got 12"); err(function(){ expect({ length: 12 }).to.have.ownProperty('iDontExist', 12, 'blah'); }, "blah: expected { length: 12 } to have own property 'iDontExist'"); err(function(){ expect({ length: 12 }, 'blah').to.have.ownProperty('iDontExist', 12); }, "blah: expected { length: 12 } to have own property 'iDontExist'"); err(function() { expect({ length: 12 }).to.not.have.ownProperty('length', 12); }, "expected { length: 12 } to not have own property 'length' of 12"); err(function() { expect({ length: 12 }).to.have.ownProperty('length', 15); }, "expected { length: 12 } to have own property 'length' of 15, but got 12"); err(function(){ expect({ length: 12 }).to.haveOwnProperty('iDontExist', 12, 'blah'); }, "blah: expected { length: 12 } to have own property 'iDontExist'"); err(function(){ expect({ length: 12 }, 'blah').to.haveOwnProperty('iDontExist', 12); }, "blah: expected { length: 12 } to have own property 'iDontExist'"); err(function() { expect({ length: 12 }).to.not.haveOwnProperty('length', 12); }, "expected { length: 12 } to not have own property 'length' of 12"); err(function() { expect({ length: 12 }).to.haveOwnProperty('length', 15); }, "expected { length: 12 } to have own property 'length' of 15, but got 12"); }); it('deep.own.property(name, val)', function () { var obj = {a: {b: 1}}; expect(obj).to.have.deep.own.property('a', {b: 1}); expect(obj).to.have.deep.ownProperty('a', {b: 1}); expect(obj).to.deep.haveOwnProperty('a', {b: 1}); expect(obj).to.not.have.deep.own.property('a', {z: 1}); expect(obj).to.not.have.deep.ownProperty('a', {z: 1}); expect(obj).to.not.deep.haveOwnProperty('a', {z: 1}); expect(obj).to.not.have.deep.own.property('a', {b: 7}); expect(obj).to.not.have.deep.ownProperty('a', {b: 7}); expect(obj).to.not.deep.haveOwnProperty('a', {b: 7}); expect(obj).to.not.have.deep.own.property('toString', Object.prototype.toString); expect(obj).to.not.have.deep.ownProperty('toString', Object.prototype.toString); expect(obj).to.not.deep.haveOwnProperty('toString', Object.prototype.toString); err(function () { expect(obj).to.have.deep.own.property('a', {z: 7}, 'blah'); }, "blah: expected { a: { b: 1 } } to have deep own property 'a' of { z: 7 }, but got { b: 1 }"); err(function () { expect(obj).to.have.deep.own.property('z', {b: 1}, 'blah'); }, "blah: expected { a: { b: 1 } } to have deep own property 'z'"); err(function () { expect(obj).to.not.have.deep.own.property('a', {b: 1}, 'blah'); }, "blah: expected { a: { b: 1 } } to not have deep own property 'a' of { b: 1 }"); err(function () { expect(obj).to.have.deep.ownProperty('a', {z: 7}, 'blah'); }, "blah: expected { a: { b: 1 } } to have deep own property 'a' of { z: 7 }, but got { b: 1 }"); err(function () { expect(obj).to.have.deep.ownProperty('z', {b: 1}, 'blah'); }, "blah: expected { a: { b: 1 } } to have deep own property 'z'"); err(function () { expect(obj).to.not.have.deep.ownProperty('a', {b: 1}, 'blah'); }, "blah: expected { a: { b: 1 } } to not have deep own property 'a' of { b: 1 }"); err(function () { expect(obj).to.deep.haveOwnProperty('a', {z: 7}, 'blah'); }, "blah: expected { a: { b: 1 } } to have deep own property 'a' of { z: 7 }, but got { b: 1 }"); err(function () { expect(obj).to.deep.haveOwnProperty('z', {b: 1}, 'blah'); }, "blah: expected { a: { b: 1 } } to have deep own property 'z'"); err(function () { expect(obj).to.not.deep.haveOwnProperty('a', {b: 1}, 'blah'); }, "blah: expected { a: { b: 1 } } to not have deep own property 'a' of { b: 1 }"); }); it('nested.property(name)', function(){ expect({ 'foo.bar': 'baz'}) .to.not.have.nested.property('foo.bar'); expect({ foo: { bar: 'baz' } }) .to.have.nested.property('foo.bar'); expect({ 'foo': [1, 2, 3] }) .to.have.nested.property('foo[1]'); expect({ 'foo.bar[]': 'baz'}) .to.have.nested.property('foo\\.bar\\[\\]'); err(function(){ expect({ 'foo.bar': 'baz' }) .to.have.nested.property('foo.bar'); }, "expected { 'foo.bar': 'baz' } to have nested property 'foo.bar'"); err(function () { expect({a:1}, 'blah').to.have.nested.property({'a':'1'}); }, "blah: the argument to property must be a string when using nested syntax"); }); it('nested.property(name, val)', function(){ expect({ foo: { bar: 'baz' } }) .to.have.nested.property('foo.bar', 'baz'); expect({ foo: { bar: 'baz' } }) .to.not.have.nested.property('foo.bar', 'quux'); expect({ foo: { bar: 'baz' } }) .to.not.have.nested.property('foo.quux', 'baz'); expect({a: {b: {c: 1}}}).to.not.have.nested.property('a.b', {c: 1}); err(function(){ expect({ foo: { bar: 'baz' } }) .to.have.nested.property('foo.bar', 'quux', 'blah'); }, "blah: expected { foo: { bar: 'baz' } } to have nested property 'foo.bar' of 'quux', but got 'baz'"); err(function(){ expect({ foo: { bar: 'baz' } }) .to.not.have.nested.property('foo.bar', 'baz', 'blah'); }, "blah: expected { foo: { bar: 'baz' } } to not have nested property 'foo.bar' of 'baz'"); }); it('deep.nested.property(name, val)', function () { var obj = {a: {b: {c: 1}}}; expect(obj).to.have.deep.nested.property('a.b', {c: 1}); expect(obj).to.not.have.deep.nested.property('a.b', {c: 7}); expect(obj).to.not.have.deep.nested.property('a.b', {z: 1}); expect(obj).to.not.have.deep.nested.property('a.z', {c: 1}); err(function () { expect(obj).to.have.deep.nested.property('a.b', {c: 7}, 'blah'); }, "blah: expected { a: { b: { c: 1 } } } to have deep nested property 'a.b' of { c: 7 }, but got { c: 1 }"); err(function () { expect(obj).to.have.deep.nested.property('a.z', {c: 1}, 'blah'); }, "blah: expected { a: { b: { c: 1 } } } to have deep nested property 'a.z'"); err(function () { expect(obj).to.not.have.deep.nested.property('a.b', {c: 1}, 'blah'); }, "blah: expected { a: { b: { c: 1 } } } to not have deep nested property 'a.b' of { c: 1 }"); }); it('ownPropertyDescriptor(name)', function(){ expect('test').to.have.ownPropertyDescriptor('length'); expect('test').to.haveOwnPropertyDescriptor('length'); expect('test').not.to.have.ownPropertyDescriptor('foo'); var obj = {}; var descriptor = { configurable: false, enumerable: true, writable: true, value: NaN }; Object.defineProperty(obj, 'test', descriptor); expect(obj).to.have.ownPropertyDescriptor('test', descriptor); err(function(){ expect(obj).not.to.have.ownPropertyDescriptor('test', descriptor, 'blah'); }, /^blah: expected the own property descriptor for 'test' on \{ test: NaN \} to not match \{ [^\}]+ \}$/); err(function(){ expect(obj, 'blah').not.to.have.ownPropertyDescriptor('test', descriptor); }, /^blah: expected the own property descriptor for 'test' on \{ test: NaN \} to not match \{ [^\}]+ \}$/); err(function(){ var wrongDescriptor = { configurable: false, enumerable: true, writable: false, value: NaN }; expect(obj).to.have.ownPropertyDescriptor('test', wrongDescriptor, 'blah'); }, /^blah: expected the own property descriptor for 'test' on \{ test: NaN \} to match \{ [^\}]+ \}, got \{ [^\}]+ \}$/); err(function(){ expect(obj).to.have.ownPropertyDescriptor('test2', 'blah'); }, "blah: expected { test: NaN } to have an own property descriptor for 'test2'"); err(function(){ expect(obj, 'blah').to.have.ownPropertyDescriptor('test2'); }, "blah: expected { test: NaN } to have an own property descriptor for 'test2'"); expect(obj).to.have.ownPropertyDescriptor('test').and.have.property('enumerable', true); }); it('string()', function(){ expect('foobar').to.have.string('bar'); expect('foobar').to.have.string('foo'); expect('foobar').to.not.have.string('baz'); err(function(){ expect(3).to.have.string('baz', 'blah'); }, "blah: expected 3 to be a string"); err(function(){ expect(3, 'blah').to.have.string('baz'); }, "blah: expected 3 to be a string"); err(function(){ expect('foobar').to.have.string('baz', 'blah'); }, "blah: expected 'foobar' to contain 'baz'"); err(function(){ expect('foobar', 'blah').to.have.string('baz'); }, "blah: expected 'foobar' to contain 'baz'"); err(function(){ expect('foobar').to.not.have.string('bar', 'blah'); }, "blah: expected 'foobar' to not contain 'bar'"); }); it('include()', function(){ expect(['foo', 'bar']).to.include('foo'); expect(['foo', 'bar']).to.include('foo'); expect(['foo', 'bar']).to.include('bar'); expect([1,2]).to.include(1); expect(['foo', 'bar']).to.not.include('baz'); expect(['foo', 'bar']).to.not.include(1); expect({a: 1}).to.include({'toString': Object.prototype.toString}); // .include should work with Error objects and objects with a custom // `@@toStringTag`. expect(new Error('foo')).to.include({message: 'foo'}); if (typeof Symbol !== 'undefined' && typeof Symbol.toStringTag !== 'undefined') { var customObj = {a: 1}; customObj[Symbol.toStringTag] = 'foo'; expect(customObj).to.include({a: 1}); } var obj1 = {a: 1} , obj2 = {b: 2}; expect([obj1, obj2]).to.include(obj1); expect([obj1, obj2]).to.not.include({a: 1}); expect({foo: obj1, bar: obj2}).to.include({foo: obj1}); expect({foo: obj1, bar: obj2}).to.include({foo: obj1, bar: obj2}); expect({foo: obj1, bar: obj2}).to.not.include({foo: {a: 1}}); expect({foo: obj1, bar: obj2}).to.not.include({foo: obj1, bar: {b: 2}}); if (typeof Map === 'function') { var map = new Map(); var val = [{a: 1}]; map.set('a', val); map.set('b', 2); map.set('c', -0); map.set('d', NaN); expect(map).to.include(val); expect(map).to.not.include([{a: 1}]); expect(map).to.include(2); expect(map).to.not.include(3); expect(map).to.include(0); expect(map).to.include(NaN); } if (typeof Set === 'function') { var set = new Set(); var val = [{a: 1}]; set.add(val); set.add(2); set.add(-0); set.add(NaN); expect(set).to.include(val); expect(set).to.not.include([{a: 1}]); expect(set).to.include(2); expect(set).to.not.include(3); if (set.has(0)) { // This test is skipped in IE11 because (contrary to spec) IE11 uses // SameValue instead of SameValueZero equality for sets. expect(set).to.include(0); } expect(set).to.include(NaN); } if (typeof WeakSet === 'function') { var ws = new WeakSet(); var val = [{a: 1}]; ws.add(val); expect(ws).to.include(val); expect(ws).to.not.include([{a: 1}]); expect(ws).to.not.include({}); } if (typeof Symbol === 'function') { var sym1 = Symbol() , sym2 = Symbol() , sym3 = Symbol(); expect([sym1, sym2]).to.include(sym1); expect([sym1, sym2]).to.not.include(sym3); } err(function(){ expect(['foo']).to.include('bar', 'blah'); }, "blah: expected [ 'foo' ] to include 'bar'"); err(function(){ expect(['foo'], 'blah').to.include('bar'); }, "blah: expected [ 'foo' ] to include 'bar'"); err(function(){ expect(['bar', 'foo']).to.not.include('foo', 'blah'); }, "blah: expected [ 'bar', 'foo' ] to not include 'foo'"); err(function(){ expect({a: 1}).to.include({b: 2}, 'blah'); }, "blah: expected { a: 1 } to have property 'b'"); err(function(){ expect({a: 1}, 'blah').to.include({b: 2}); }, "blah: expected { a: 1 } to have property 'b'"); err(function(){ expect({a:1,b:2}).to.not.include({b:2}); }, "expected { a: 1, b: 2 } to not have property 'b' of 2"); err(function () { expect([{a: 1}, {b: 2}]).to.include({a: 1}); }, "expected [ { a: 1 }, { b: 2 } ] to include { a: 1 }"); err(function () { var obj1 = {a: 1} , obj2 = {b: 2}; expect([obj1, obj2]).to.not.include(obj1); }, "expected [ { a: 1 }, { b: 2 } ] to not include { a: 1 }"); err(function () { expect({foo: {a: 1}, bar: {b: 2}}).to.include({foo: {a: 1}}); }, "expected { foo: { a: 1 }, bar: { b: 2 } } to have property 'foo' of { a: 1 }, but got { a: 1 }"); err(function () { var obj1 = {a: 1} , obj2 = {b: 2}; expect({foo: obj1, bar: obj2}).to.not.include({foo: obj1, bar: obj2}); }, "expected { foo: { a: 1 }, bar: { b: 2 } } to not have property 'foo' of { a: 1 }"); err(function(){ expect(true).to.include(true, 'blah'); }, "blah: the given combination of arguments (boolean and boolean) is invalid for this assertion. " + "You can use an array, a map, an object, a set, a string, or a weakset instead of a boolean" ); err(function(){ expect(true, 'blah').to.include(true); }, "blah: the given combination of arguments (boolean and boolean) is invalid for this assertion. " + "You can use an array, a map, an object, a set, a string, or a weakset instead of a boolean" ); err(function(){ expect(42.0).to.include(42); }, "the given combination of arguments (number and number) is invalid for this assertion. " + "You can use an array, a map, an object, a set, a string, or a weakset instead of a number" ); err(function(){ expect(null).to.include(42); }, "the given combination of arguments (null and number) is invalid for this assertion. " + "You can use an array, a map, an object, a set, a string, or a weakset instead of a number" ); err(function(){ expect(undefined).to.include(42); }, "the given combination of arguments (undefined and number) is invalid for this assertion. " + "You can use an array, a map, an object, a set, a string, or a weakset instead of a number" ); err(function(){ expect(true).to.not.include(true); }, "the given combination of arguments (boolean and boolean) is invalid for this assertion. " + "You can use an array, a map, an object, a set, a string, or a weakset instead of a boolean" ); err(function(){ expect(42.0).to.not.include(42); }, "the given combination of arguments (number and number) is invalid for this assertion. " + "You can use an array, a map, an object, a set, a string, or a weakset instead of a number" ); err(function(){ expect(null).to.not.include(42); }, "the given combination of arguments (null and number) is invalid for this assertion. " + "You can use an array, a map, an object, a set, a string, or a weakset instead of a number" ); err(function(){ expect(undefined).to.not.include(42); }, "the given combination of arguments (undefined and number) is invalid for this assertion. " + "You can use an array, a map, an object, a set, a string, or a weakset instead of a number" ); }); it('deep.include()', function () { var obj1 = {a: 1} , obj2 = {b: 2}; expect([obj1, obj2]).to.deep.include({a: 1}); expect([obj1, obj2]).to.not.deep.include({a: 9}); expect([obj1, obj2]).to.not.deep.include({z: 1}); expect({foo: obj1, bar: obj2}).to.deep.include({foo: {a: 1}}); expect({foo: obj1, bar: obj2}).to.deep.include({foo: {a: 1}, bar: {b: 2}}); expect({foo: obj1, bar: obj2}).to.not.deep.include({foo: {a: 9}}); expect({foo: obj1, bar: obj2}).to.not.deep.include({foo: {z: 1}}); expect({foo: obj1, bar: obj2}).to.not.deep.include({baz: {a: 1}}); expect({foo: obj1, bar: obj2}).to.not.deep.include({foo: {a: 1}, bar: {b: 9}}); if (typeof Map === 'function') { var map = new Map(); map.set(1, [{a: 1}]); expect(map).to.deep.include([{a: 1}]); } if (typeof Set === 'function') { var set = new Set(); set.add([{a: 1}]); expect(set).to.deep.include([{a: 1}]); } if (typeof WeakSet === 'function') { err(function() { expect(new WeakSet()).to.deep.include({}, 'foo'); }, 'foo: unable to use .deep.include with WeakSet'); } err(function () { expect([obj1, obj2]).to.deep.include({a: 9}, 'blah'); }, "blah: expected [ { a: 1 }, { b: 2 } ] to deep include { a: 9 }"); err(function () { expect([obj1, obj2], 'blah').to.deep.include({a: 9}); }, "blah: expected [ { a: 1 }, { b: 2 } ] to deep include { a: 9 }"); err(function () { expect([obj1, obj2], 'blah').to.not.deep.include({a: 1}); }, "blah: expected [ { a: 1 }, { b: 2 } ] to not deep include { a: 1 }"); err(function () { expect({foo: obj1, bar: obj2}).to.deep.include({foo: {a: 1}, bar: {b: 9}}); }, "expected { foo: { a: 1 }, bar: { b: 2 } } to have deep property 'bar' of { b: 9 }, but got { b: 2 }"); err(function () { expect({foo: obj1, bar: obj2}).to.not.deep.include({foo: {a: 1}, bar: {b: 2}}, 'blah'); }, "blah: expected { foo: { a: 1 }, bar: { b: 2 } } to not have deep property 'foo' of { a: 1 }"); }); it('nested.include()', function () { expect({a: {b: ['x', 'y']}}).to.nested.include({'a.b[1]': 'y'}); expect({a: {b: ['x', 'y']}}).to.not.nested.include({'a.b[1]': 'x'}); expect({a: {b: ['x', 'y']}}).to.not.nested.include({'a.c': 'y'}); expect({a: {b: [{x: 1}]}}).to.not.nested.include({'a.b[0]': {x: 1}}); expect({'.a': {'[b]': 'x'}}).to.nested.include({'\\.a.\\[b\\]': 'x'}); expect({'.a': {'[b]': 'x'}}).to.not.nested.include({'\\.a.\\[b\\]': 'y'}); err(function () { expect({a: {b: ['x', 'y']}}).to.nested.include({'a.b[1]': 'x'}, 'blah'); }, "blah: expected { a: { b: [ 'x', 'y' ] } } to have nested property 'a.b[1]' of 'x', but got 'y'"); err(function () { expect({a: {b: ['x', 'y']}}, 'blah').to.nested.include({'a.b[1]': 'x'}); }, "blah: expected { a: { b: [ 'x', 'y' ] } } to have nested property 'a.b[1]' of 'x', but got 'y'"); err(function () { expect({a: {b: ['x', 'y']}}).to.nested.include({'a.c': 'y'}); }, "expected { a: { b: [ 'x', 'y' ] } } to have nested property 'a.c'"); err(function () { expect({a: {b: ['x', 'y']}}).to.not.nested.include({'a.b[1]': 'y'}, 'blah'); }, "blah: expected { a: { b: [ 'x', 'y' ] } } to not have nested property 'a.b[1]' of 'y'"); err(function () { expect({a: {b: ['x', 'y']}}, 'blah').to.not.nested.include({'a.b[1]': 'y'}); }, "blah: expected { a: { b: [ 'x', 'y' ] } } to not have nested property 'a.b[1]' of 'y'"); }); it('deep.nested.include()', function () { expect({a: {b: [{x: 1}]}}).to.deep.nested.include({'a.b[0]': {x: 1}}); expect({a: {b: [{x: 1}]}}).to.not.deep.nested.include({'a.b[0]': {y: 2}}); expect({a: {b: [{x: 1}]}}).to.not.deep.nested.include({'a.c': {x: 1}}); expect({'.a': {'[b]': {x: 1}}}) .to.deep.nested.include({'\\.a.\\[b\\]': {x: 1}}); expect({'.a': {'[b]': {x: 1}}}) .to.not.deep.nested.include({'\\.a.\\[b\\]': {y: 2}}); err(function () { expect({a: {b: [{x: 1}]}}).to.deep.nested.include({'a.b[0]': {y: 2}}, 'blah'); }, "blah: expected { a: { b: [ { x: 1 } ] } } to have deep nested property 'a.b[0]' of { y: 2 }, but got { x: 1 }"); err(function () { expect({a: {b: [{x: 1}]}}, 'blah').to.deep.nested.include({'a.b[0]': {y: 2}}); }, "blah: expected { a: { b: [ { x: 1 } ] } } to have deep nested property 'a.b[0]' of { y: 2 }, but got { x: 1 }"); err(function () { expect({a: {b: [{x: 1}]}}).to.deep.nested.include({'a.c': {x: 1}}); }, "expected { a: { b: [ { x: 1 } ] } } to have deep nested property 'a.c'"); err(function () { expect({a: {b: [{x: 1}]}}).to.not.deep.nested.include({'a.b[0]': {x: 1}}, 'blah'); }, "blah: expected { a: { b: [ { x: 1 } ] } } to not have deep nested property 'a.b[0]' of { x: 1 }"); err(function () { expect({a: {b: [{x: 1}]}}, 'blah').to.not.deep.nested.include({'a.b[0]': {x: 1}}); }, "blah: expected { a: { b: [ { x: 1 } ] } } to not have deep nested property 'a.b[0]' of { x: 1 }"); }); it('own.include()', function () { expect({a: 1}).to.own.include({a: 1}); expect({a: 1}).to.not.own.include({a: 3}); expect({a: 1}).to.not.own.include({'toString': Object.prototype.toString}); expect({a: {b: 2}}).to.not.own.include({a: {b: 2}}); err(function () { expect({a: 1}).to.own.include({a: 3}, 'blah'); }, "blah: expected { a: 1 } to have own property 'a' of 3, but got 1"); err(function () { expect({a: 1}, 'blah').to.own.include({a: 3}); }, "blah: expected { a: 1 } to have own property 'a' of 3, but got 1"); err(function () { expect({a: 1}).to.own.include({'toString': Object.prototype.toString}); }, "expected { a: 1 } to have own property 'toString'"); err(function () { expect({a: 1}).to.not.own.include({a: 1}, 'blah'); }, "blah: expected { a: 1 } to not have own property 'a' of 1"); err(function () { expect({a: 1}, 'blah').to.not.own.include({a: 1}); }, "blah: expected { a: 1 } to not have own property 'a' of 1"); }); it('deep.own.include()', function () { expect({a: {b: 2}}).to.deep.own.include({a: {b: 2}}); expect({a: {b: 2}}).to.not.deep.own.include({a: {c: 3}}); expect({a: {b: 2}}) .to.not.deep.own.include({'toString': Object.prototype.toString}); err(function () { expect({a: {b: 2}}).to.deep.own.include({a: {c: 3}}, 'blah'); }, "blah: expected { a: { b: 2 } } to have deep own property 'a' of { c: 3 }, but got { b: 2 }"); err(function () { expect({a: {b: 2}}, 'blah').to.deep.own.include({a: {c: 3}}); }, "blah: expected { a: { b: 2 } } to have deep own property 'a' of { c: 3 }, but got { b: 2 }"); err(function () { expect({a: {b: 2}}).to.deep.own.include({'toString': Object.prototype.toString}); }, "expected { a: { b: 2 } } to have deep own property 'toString'"); err(function () { expect({a: {b: 2}}).to.not.deep.own.include({a: {b: 2}}, 'blah'); }, "blah: expected { a: { b: 2 } } to not have deep own property 'a' of { b: 2 }"); err(function () { expect({a: {b: 2}}, 'blah').to.not.deep.own.include({a: {b: 2}}); }, "blah: expected { a: { b: 2 } } to not have deep own property 'a' of { b: 2 }"); }); it('keys(array|Object|arguments)', function(){ expect({ foo: 1 }).to.have.keys(['foo']); expect({ foo: 1 }).have.keys({ 'foo': 6 }); expect({ foo: 1, bar: 2 }).to.have.keys(['foo', 'bar']); expect({ foo: 1, bar: 2 }).to.have.keys('foo', 'bar'); expect({ foo: 1, bar: 2 }).have.keys({ 'foo': 6, 'bar': 7 }); expect({ foo: 1, bar: 2, baz: 3 }).to.contain.keys('foo', 'bar'); expect({ foo: 1, bar: 2, baz: 3 }).to.contain.keys('bar', 'foo'); expect({ foo: 1, bar: 2, baz: 3 }).to.contain.keys('baz'); expect({ foo: 1, bar: 2 }).contain.keys({ 'foo': 6 }); expect({ foo: 1, bar: 2 }).contain.keys({ 'bar': 7 }); expect({ foo: 1, bar: 2 }).contain.keys({ 'foo': 6 }); expect({ foo: 1, bar: 2 }).to.contain.keys('foo'); expect({ foo: 1, bar: 2 }).to.contain.keys('bar', 'foo'); expect({ foo: 1, bar: 2 }).to.contain.keys(['foo']); expect({ foo: 1, bar: 2 }).to.contain.keys(['bar']); expect({ foo: 1, bar: 2 }).to.contain.keys(['bar', 'foo']); expect({ foo: 1, bar: 2, baz: 3 }).to.contain.all.keys(['bar', 'foo']); expect({ foo: 1, bar: 2 }).to.not.have.keys('baz'); expect({ foo: 1, bar: 2 }).to.not.have.keys('foo'); expect({ foo: 1, bar: 2 }).to.not.have.keys('foo', 'baz'); expect({ foo: 1, bar: 2 }).to.not.contain.keys('baz'); expect({ foo: 1, bar: 2 }).to.not.contain.keys('foo', 'baz'); expect({ foo: 1, bar: 2 }).to.not.contain.keys('baz', 'foo'); expect({ foo: 1, bar: 2 }).to.have.any.keys('foo', 'baz'); expect({ foo: 1, bar: 2 }).to.have.any.keys('foo'); expect({ foo: 1, bar: 2 }).to.contain.any.keys('bar', 'baz'); expect({ foo: 1, bar: 2 }).to.contain.any.keys(['foo']); expect({ foo: 1, bar: 2 }).to.have.all.keys(['bar', 'foo']); expect({ foo: 1, bar: 2 }).to.contain.all.keys(['bar', 'foo']); expect({ foo: 1, bar: 2 }).contain.any.keys({ 'foo': 6 }); expect({ foo: 1, bar: 2 }).have.all.keys({ 'foo': 6, 'bar': 7 }); expect({ foo: 1, bar: 2 }).contain.all.keys({ 'bar': 7, 'foo': 6 }); expect({ foo: 1, bar: 2 }).to.not.have.any.keys('baz', 'abc', 'def'); expect({ foo: 1, bar: 2 }).to.not.have.any.keys('baz'); expect({ foo: 1, bar: 2 }).to.not.contain.any.keys('baz'); expect({ foo: 1, bar: 2 }).to.not.have.all.keys(['baz', 'foo']); expect({ foo: 1, bar: 2 }).to.not.contain.all.keys(['baz', 'foo']); expect({ foo: 1, bar: 2 }).not.have.all.keys({ 'baz': 8, 'foo': 7 }); expect({ foo: 1, bar: 2 }).not.contain.all.keys({ 'baz': 8, 'foo': 7 }); var enumProp1 = 'enumProp1' , enumProp2 = 'enumProp2' , nonEnumProp = 'nonEnumProp' , obj = {}; obj[enumProp1] = 'enumProp1'; obj[enumProp2] = 'enumProp2'; Object.defineProperty(obj, nonEnumProp, { enumerable: false, value: 'nonEnumProp' }); expect(obj).to.have.all.keys([enumProp1, enumProp2]); expect(obj).to.not.have.all.keys([enumProp1, enumProp2, nonEnumProp]); if (typeof Symbol === 'function') { var sym1 = Symbol('sym1') , sym2 = Symbol('sym2') , sym3 = Symbol('sym3') , str = 'str' , obj = {}; obj[sym1] = 'sym1'; obj[sym2] = 'sym2'; obj[str] = 'str'; Object.defineProperty(obj, sym3, { enumerable: false, value: 'sym3' }); expect(obj).to.have.all.keys([sym1, sym2, str]); expect(obj).to.not.have.all.keys([sym1, sym2, sym3, str]); } if (typeof Map !== 'undefined') { // Not using Map constructor args because not supported in IE 11. var aKey = {thisIs: 'anExampleObject'} , anotherKey = {doingThisBecauseOf: 'referential equality'} , testMap = new Map(); testMap.set(aKey, 'aValue'); testMap.set(anotherKey, 'anotherValue'); expect(testMap).to.have.any.keys(aKey); expect(testMap).to.have.any.keys('thisDoesNotExist', 'thisToo', aKey); expect(testMap).to.have.all.keys(aKey, anotherKey); expect(testMap).to.contain.all.keys(aKey); expect(testMap).to.not.contain.all.keys(aKey, 'thisDoesNotExist'); expect(testMap).to.not.have.any.keys({iDoNot: 'exist'}); expect(testMap).to.not.have.any.keys('thisIsNotAkey', {iDoNot: 'exist'}, {33: 20}); expect(testMap).to.not.have.all.keys('thisDoesNotExist', 'thisToo', anotherKey); expect(testMap).to.have.any.keys([aKey]); expect(testMap).to.have.any.keys([20, 1, aKey]); expect(testMap).to.have.all.keys([aKey, anotherKey]); expect(testMap).to.not.have.any.keys([{13: 37}, 'thisDoesNotExist', 'thisToo']); expect(testMap).to.not.have.any.keys([20, 1, {13: 37}]); expect(testMap).to.not.have.all.keys([aKey, {'iDoNot': 'exist'}]); // Ensure the assertions above use strict equality err(function() { expect(testMap).to.have.any.keys({thisIs: 'anExampleObject'}); }); err(function() { expect(testMap).to.have.all.keys({thisIs: 'anExampleObject'}, {doingThisBecauseOf: 'referential equality'}); }); err(function() { expect(testMap).to.contain.all.keys({thisIs: 'anExampleObject'}); }); err(function() { expect(testMap).to.have.any.keys([{thisIs: 'anExampleObject'}]); }); err(function() { expect(testMap).to.have.all.keys([{thisIs: 'anExampleObject'}, {doingThisBecauseOf: 'referential equality'}]); }); // Using the same assertions as above but with `.deep` flag instead of using referential equality expect(testMap).to.have.any.deep.keys({thisIs: 'anExampleObject'}); expect(testMap).to.have.any.deep.keys('thisDoesNotExist', 'thisToo', {thisIs: 'anExampleObject'}); expect(testMap).to.contain.all.deep.keys({thisIs: 'anExampleObject'}); expect(testMap).to.not.contain.all.deep.keys({thisIs: 'anExampleObject'}, 'thisDoesNotExist'); expect(testMap).to.not.have.any.deep.keys({iDoNot: 'exist'}); expect(testMap).to.not.have.any.deep.keys('thisIsNotAkey', {iDoNot: 'exist'}, {33: 20}); expect(testMap).to.not.have.all.deep.keys('thisDoesNotExist', 'thisToo', {doingThisBecauseOf: 'referential equality'}); expect(testMap).to.have.any.deep.keys([{thisIs: 'anExampleObject'}]); expect(testMap).to.have.any.deep.keys([20, 1, {thisIs: 'anExampleObject'}]); expect(testMap).to.have.all.deep.keys({thisIs: 'anExampleObject'}, {doingThisBecauseOf: 'referential equality'}); expect(testMap).to.not.have.any.deep.keys([{13: 37}, 'thisDoesNotExist', 'thisToo']); expect(testMap).to.not.have.any.deep.keys([20, 1, {13: 37}]); expect(testMap).to.not.have.all.deep.keys([{thisIs: 'anExampleObject'}, {'iDoNot': 'exist'}]); var weirdMapKey1 = Object.create(null) , weirdMapKey2 = {toString: NaN} , weirdMapKey3 = [] , weirdMap = new Map(); weirdMap.set(weirdMapKey1, 'val1'); weirdMap.set(weirdMapKey2, 'val2'); expect(weirdMap).to.have.all.keys([weirdMapKey1, weirdMapKey2]); expect(weirdMap).to.not.have.all.keys([weirdMapKey1, weirdMapKey3]); if (typeof Symbol === 'function') { var symMapKey1 = Symbol() , symMapKey2 = Symbol() , symMapKey3 = Symbol() , symMap = new Map(); symMap.set(symMapKey1, 'val1'); symMap.set(symMapKey2, 'val2'); expect(symMap).to.have.all.keys(symMapKey1, symMapKey2); expect(symMap).to.have.any.keys(symMapKey1, symMapKey3); expect(symMap).to.contain.all.keys(symMapKey2, symMapKey1); expect(symMap).to.contain.any.keys(symMapKey3, symMapKey1); expect(symMap).to.not.have.all.keys(symMapKey1, symMapKey3); expect(symMap).to.not.have.any.keys(symMapKey3); expect(symMap).to.not.contain.all.keys(symMapKey3, symMapKey1); expect(symMap).to.not.contain.any.keys(symMapKey3); } var errMap = new Map(); errMap.set({ foo: 1 }); err(function(){ expect(errMap, 'blah').to.have.keys(); }, "blah: keys required"); err(function(){ expect(errMap).to.have.keys([]); }, "keys required"); err(function(){ expect(errMap).to.contain.keys(); }, "keys required"); err(function(){ expect(errMap).to.contain.keys([]); }, "keys required"); // Uncomment this after solving https://github.com/chaijs/chai/issues/662 // This should fail because of referential equality (this is a strict comparison) // err(function(){ // expect(new Map([[{foo: 1}, 'bar']])).to.contain.keys({ foo: 1 }); // }, 'expected [ [ { foo: 1 }, 'bar' ] ] to contain key { foo: 1 }'); // err(function(){ // expect(new Map([[{foo: 1}, 'bar']])).to.contain.deep.keys({ iDoNotExist: 0 }) // }, 'expected [ { foo: 1 } ] to deeply contain key { iDoNotExist: 0 }'); } if (typeof Set !== 'undefined') { // Not using Set constructor args because not supported in IE 11. var aKey = {thisIs: 'anExampleObject'} , anotherKey = {doingThisBecauseOf: 'referential equality'} , testSet = new Set(); testSet.add(aKey); testSet.add(anotherKey); expect(testSet).to.have.any.keys(aKey); expect(testSet).to.have.any.keys('thisDoesNotExist', 'thisToo', aKey); expect(testSet).to.have.all.keys(aKey, anotherKey); expect(testSet).to.contain.all.keys(aKey); expect(testSet).to.not.contain.all.keys(aKey, 'thisDoesNotExist'); expect(testSet).to.not.have.any.keys({iDoNot: 'exist'}); expect(testSet).to.not.have.any.keys('thisIsNotAkey', {iDoNot: 'exist'}, {33: 20}); expect(testSet).to.not.have.all.keys('thisDoesNotExist', 'thisToo', anotherKey); expect(testSet).to.have.any.keys([aKey]); expect(testSet).to.have.any.keys([20, 1, aKey]); expect(testSet).to.have.all.keys([aKey, anotherKey]); expect(testSet).to.not.have.any.keys([{13: 37}, 'thisDoesNotExist', 'thisToo']); expect(testSet).to.not.have.any.keys([20, 1, {13: 37}]); expect(testSet).to.not.have.all.keys([aKey, {'iDoNot': 'exist'}]); // Ensure the assertions above use strict equality err(function() { expect(testSet).to.have.any.keys({thisIs: 'anExampleObject'}); }); err(function() { expect(testSet).to.have.all.keys({thisIs: 'anExampleObject'}, {doingThisBecauseOf: 'referential equality'}); }); err(function() { expect(testSet).to.contain.all.keys({thisIs: 'anExampleObject'}); }); err(function() { expect(testSet).to.have.any.keys([{thisIs: 'anExampleObject'}]); }); err(function() { expect(testSet).to.have.all.keys([{thisIs: 'anExampleObject'}, {doingThisBecauseOf: 'referential equality'}]); }); // Using the same assertions as above but with `.deep` flag instead of using referential equality expect(testSet).to.have.any.deep.keys({thisIs: 'anExampleObject'}); expect(testSet).to.have.any.deep.keys('thisDoesNotExist', 'thisToo', {thisIs: 'anExampleObject'}); expect(testSet).to.contain.all.deep.keys({thisIs: 'anExampleObject'}); expect(testSet).to.not.contain.all.deep.keys({thisIs: 'anExampleObject'}, 'thisDoesNotExist'); expect(testSet).to.not.have.any.deep.keys({iDoNot: 'exist'}); expect(testSet).to.not.have.any.deep.keys('thisIsNotAkey', {iDoNot: 'exist'}, {33: 20}); expect(testSet).to.not.have.all.deep.keys('thisDoesNotExist', 'thisToo', {doingThisBecauseOf: 'referential equality'}); expect(testSet).to.have.any.deep.keys([{thisIs: 'anExampleObject'}]); expect(testSet).to.have.any.deep.keys([20, 1, {thisIs: 'anExampleObject'}]); expect(testSet).to.have.all.deep.keys([{thisIs: 'anExampleObject'}, {doingThisBecauseOf: 'referential equality'}]); expect(testSet).to.not.have.any.deep.keys([{13: 37}, 'thisDoesNotExist', 'thisToo']); expect(testSet).to.not.have.any.deep.keys([20, 1, {13: 37}]); expect(testSet).to.not.have.all.deep.keys([{thisIs: 'anExampleObject'}, {'iDoNot': 'exist'}]); var weirdSetKey1 = Object.create(null) , weirdSetKey2 = {toString: NaN} , weirdSetKey3 = [] , weirdSet = new Set(); weirdSet.add(weirdSetKey1); weirdSet.add(weirdSetKey2); expect(weirdSet).to.have.all.keys([weirdSetKey1, weirdSetKey2]); expect(weirdSet).to.not.have.all.keys([weirdSetKey1, weirdSetKey3]); if (typeof Symbol === 'function') { var symSetKey1 = Symbol() , symSetKey2 = Symbol() , symSetKey3 = Symbol() , symSet = new Set(); symSet.add(symSetKey1); symSet.add(symSetKey2); expect(symSet).to.have.all.keys(symSetKey1, symSetKey2); expect(symSet).to.have.any.keys(symSetKey1, symSetKey3); expect(symSet).to.contain.all.keys(symSetKey2, symSetKey1); expect(symSet).to.contain.any.keys(symSetKey3, symSetKey1); expect(symSet).to.not.have.all.keys(symSetKey1, symSetKey3); expect(symSet).to.not.have.any.keys(symSetKey3); expect(symSet).to.not.contain.all.keys(symSetKey3, symSetKey1); expect(symSet).to.not.contain.any.keys(symSetKey3); } var errSet = new Set(); errSet.add({ foo: 1}); err(function(){ expect(errSet, 'blah').to.have.keys(); }, "blah: keys required"); err(function(){ expect(errSet).to.have.keys([]); }, "keys required"); err(function(){ expect(errSet).to.contain.keys(); }, "keys required"); err(function(){ expect(errSet).to.contain.keys([]); }, "keys required"); // Uncomment this after solving https://github.com/chaijs/chai/issues/662 // This should fail because of referential equality (this is a strict comparison) // err(function(){ // expect(new Set([{foo: 1}])).to.contain.keys({ foo: 1 }); // }, 'expected [ { foo: 1 } ] to deeply contain key { foo: 1 }'); // err(function(){ // expect(new Set([{foo: 1}])).to.contain.deep.keys({ iDoNotExist: 0 }); // }, 'expected [ { foo: 1 } ] to deeply contain key { iDoNotExist: 0 }'); } err(function(){ expect({ foo: 1 }, 'blah').to.have.keys(); }, "blah: keys required"); err(function(){ expect({ foo: 1 }).to.have.keys([]); }, "keys required"); err(function(){ expect({ foo: 1 }).to.not.have.keys([]); }, "keys required"); err(function(){ expect({ foo: 1 }).to.contain.keys([]); }, "keys required"); var mixedArgsMsg = 'blah: when testing keys against an object or an array you must give a single Array|Object|String argument or multiple String arguments' err(function(){ expect({}, 'blah').contain.keys(['a'], "b"); }, mixedArgsMsg); err(function(){ expect({}, 'blah').contain.keys({ 'a': 1 }, "b"); }, mixedArgsMsg); err(function(){ expect({ foo: 1 }, 'blah').to.have.keys(['bar']); }, "blah: expected { foo: 1 } to have key 'bar'"); err(function(){ expect({ foo: 1 }).to.have.keys(['bar', 'baz']); }, "expected { foo: 1 } to have keys 'bar', and 'baz'"); err(function(){ expect({ foo: 1 }).to.have.keys(['foo', 'bar', 'baz']); }, "expected { foo: 1 } to have keys 'foo', 'bar', and 'baz'"); err(function(){ expect({ foo: 1 }).to.not.have.keys(['foo']); }, "expected { foo: 1 } to not have key 'foo'"); err(function(){ expect({ foo: 1 }).to.not.have.keys(['foo']); }, "expected { foo: 1 } to not have key 'foo'"); err(function(){ expect({ foo: 1, bar: 2 }).to.not.have.keys(['foo', 'bar']); }, "expected { foo: 1, bar: 2 } to not have keys 'foo', and 'bar'"); err(function(){ expect({ foo: 1, bar: 2 }).to.have.all.keys('foo'); }, "expected { foo: 1, bar: 2 } to have key 'foo'"); err(function(){ expect({ foo: 1 }).to.not.contain.keys(['foo']); }, "expected { foo: 1 } to not contain key 'foo'"); err(function(){ expect({ foo: 1 }).to.contain.keys('foo', 'bar'); }, "expected { foo: 1 } to contain keys 'foo', and 'bar'"); err(function() { expect({ foo: 1 }).to.have.any.keys('baz'); }, "expected { foo: 1 } to have key 'baz'"); err(function(){ expect({ foo: 1, bar: 2 }).to.not.have.all.keys(['foo', 'bar']); }, "expected { foo: 1, bar: 2 } to not have keys 'foo', and 'bar'"); err(function(){ expect({ foo: 1, bar: 2 }).to.not.have.any.keys(['foo', 'baz']); }, "expected { foo: 1, bar: 2 } to not have keys 'foo', or 'baz'"); // repeat previous tests with Object as arg. err(function(){ expect({ foo: 1 }, 'blah').have.keys({ 'bar': 1 }); }, "blah: expected { foo: 1 } to have key 'bar'"); err(function(){ expect({ foo: 1 }).have.keys({ 'bar': 1, 'baz': 1}); }, "expected { foo: 1 } to have keys 'bar', and 'baz'"); err(function(){ expect({ foo: 1 }).have.keys({ 'foo': 1, 'bar': 1, 'baz': 1}); }, "expected { foo: 1 } to have keys 'foo', 'bar', and 'baz'"); err(function(){ expect({ foo: 1 }).not.have.keys({ 'foo': 1 }); }, "expected { foo: 1 } to not have key 'foo'"); err(function(){ expect({ foo: 1 }).not.have.keys({ 'foo': 1 }); }, "expected { foo: 1 } to not have key 'foo'"); err(function(){ expect({ foo: 1, bar: 2 }).not.have.keys({ 'foo': 1, 'bar': 1}); }, "expected { foo: 1, bar: 2 } to not have keys 'foo', and 'bar'"); err(function(){ expect({ foo: 1 }).not.contain.keys({ 'foo': 1 }); }, "expected { foo: 1 } to not contain key 'foo'"); err(function(){ expect({ foo: 1 }).contain.keys('foo', 'bar'); }, "expected { foo: 1 } to contain keys 'foo', and 'bar'"); err(function() { expect({ foo: 1 }).have.any.keys('baz'); }, "expected { foo: 1 } to have key 'baz'"); err(function(){ expect({ foo: 1, bar: 2 }).not.have.all.keys({ 'foo': 1, 'bar': 1}); }, "expected { foo: 1, bar: 2 } to not have keys 'foo', and 'bar'"); err(function(){ expect({ foo: 1, bar: 2 }).not.have.any.keys({ 'foo': 1, 'baz': 1}); }, "expected { foo: 1, bar: 2 } to not have keys 'foo', or 'baz'"); }); it('keys(array) will not mutate array (#359)', function () { var expected = [ 'b', 'a' ]; var original_order = [ 'b', 'a' ]; var obj = { "b": 1, "a": 1 }; expect(expected).deep.equal(original_order); expect(obj).keys(original_order); expect(expected).deep.equal(original_order); }); it('chaining', function(){ var tea = { name: 'chai', extras: ['milk', 'sugar', 'smile'] }; expect(tea).to.have.property('extras').with.lengthOf(3); expect(tea).to.have.property('extras').which.contains('smile'); err(function(){ expect(tea).to.have.property('extras').with.lengthOf(4); }, "expected [ 'milk', 'sugar', 'smile' ] to have a length of 4 but got 3"); expect(tea).to.be.a('object').and.have.property('name', 'chai'); var badFn = function () { throw new Error('testing'); }; expect(badFn).to.throw(Error).with.property('message', 'testing'); }); it('throw', function () { // See GH-45: some poorly-constructed custom errors don't have useful names // on either their constructor or their constructor prototype, but instead // only set the name inside the constructor itself. var PoorlyConstructedError = function () { this.name = 'PoorlyConstructedError'; }; PoorlyConstructedError.prototype = Object.create(Error.prototype); function CustomError(message) { this.name = 'CustomError'; this.message = message; } CustomError.prototype = Error.prototype; var specificError = new RangeError('boo'); var goodFn = function () { 1==1; } , badFn = function () { throw new Error('testing'); } , refErrFn = function () { throw new ReferenceError('hello'); } , ickyErrFn = function () { throw new PoorlyConstructedError(); } , specificErrFn = function () { throw specificError; } , customErrFn = function() { throw new CustomError('foo'); } , emptyErrFn = function () { throw new Error(); } , emptyStringErrFn = function () { throw new Error(''); }; expect(goodFn).to.not.throw(); expect(goodFn).to.not.throw(Error); expect(goodFn).to.not.throw(specificError); expect(badFn).to.throw(); expect(badFn).to.throw(Error); expect(badFn).to.not.throw(ReferenceError); expect(badFn).to.not.throw(specificError); expect(refErrFn).to.throw(); expect(refErrFn).to.throw(ReferenceError); expect(refErrFn).to.throw(Error); expect(refErrFn).to.not.throw(TypeError); expect(refErrFn).to.not.throw(specificError); expect(ickyErrFn).to.throw(); expect(ickyErrFn).to.throw(PoorlyConstructedError); expect(ickyErrFn).to.throw(Error); expect(ickyErrFn).to.not.throw(specificError); expect(specificErrFn).to.throw(specificError); expect(goodFn).to.not.throw('testing'); expect(goodFn).to.not.throw(/testing/); expect(badFn).to.throw(/testing/); expect(badFn).to.not.throw(/hello/); expect(badFn).to.throw('testing'); expect(badFn).to.not.throw('hello'); expect(emptyStringErrFn).to.throw(''); expect(emptyStringErrFn).to.not.throw('testing'); expect(badFn).to.throw(''); expect(badFn).to.throw(Error, /testing/); expect(badFn).to.throw(Error, 'testing'); expect(emptyErrFn).to.not.throw(Error, 'testing'); expect(badFn).to.not.throw(Error, 'I am the wrong error message'); expect(badFn).to.not.throw(TypeError, 'testing'); err(function(){ expect(goodFn, 'blah').to.throw(); }, /^blah: expected \[Function( goodFn)*\] to throw an error$/); err(function(){ expect(goodFn, 'blah').to.throw(ReferenceError); }, /^blah: expected \[Function( goodFn)*\] to throw ReferenceError$/); err(function(){ expect(goodFn, 'blah').to.throw(specificError); }, /^blah: expected \[Function( goodFn)*\] to throw 'RangeError: boo'$/); err(function(){ expect(badFn, 'blah').to.not.throw(); }, /^blah: expected \[Function( badFn)*\] to not throw an error but 'Error: testing' was thrown$/); err(function(){ expect(badFn, 'blah').to.throw(ReferenceError); }, /^blah: expected \[Function( badFn)*\] to throw 'ReferenceError' but 'Error: testing' was thrown$/); err(function(){ expect(badFn, 'blah').to.throw(specificError); }, /^blah: expected \[Function( badFn)*\] to throw 'RangeError: boo' but 'Error: testing' was thrown$/); err(function(){ expect(badFn, 'blah').to.not.throw(Error); }, /^blah: expected \[Function( badFn)*\] to not throw 'Error' but 'Error: testing' was thrown$/); err(function(){ expect(refErrFn, 'blah').to.not.throw(ReferenceError); }, /^blah: expected \[Function( refErrFn)*\] to not throw 'ReferenceError' but 'ReferenceError: hello' was thrown$/); err(function(){ expect(badFn, 'blah').to.throw(PoorlyConstructedError); }, /^blah: expected \[Function( badFn)*\] to throw 'PoorlyConstructedError' but 'Error: testing' was thrown$/); err(function(){ expect(ickyErrFn, 'blah').to.not.throw(PoorlyConstructedError); }, /^blah: (expected \[Function( ickyErrFn)*\] to not throw 'PoorlyConstructedError' but)(.*)(PoorlyConstructedError|\{ Object \()(.*)(was thrown)$/); err(function(){ expect(ickyErrFn, 'blah').to.throw(ReferenceError); }, /^blah: (expected \[Function( ickyErrFn)*\] to throw 'ReferenceError' but)(.*)(PoorlyConstructedError|\{ Object \()(.*)(was thrown)$/); err(function(){ expect(specificErrFn, 'blah').to.throw(new ReferenceError('eek')); }, /^blah: expected \[Function( specificErrFn)*\] to throw 'ReferenceError: eek' but 'RangeError: boo' was thrown$/); err(function(){ expect(specificErrFn, 'blah').to.not.throw(specificError); }, /^blah: expected \[Function( specificErrFn)*\] to not throw 'RangeError: boo'$/); err(function (){ expect(badFn, 'blah').to.not.throw(/testing/); }, /^blah: expected \[Function( badFn)*\] to throw error not matching \/testing\/$/); err(function () { expect(badFn, 'blah').to.throw(/hello/); }, /^blah: expected \[Function( badFn)*\] to throw error matching \/hello\/ but got 'testing'$/); err(function () { expect(badFn).to.throw(Error, /hello/, 'blah'); }, /^blah: expected \[Function( badFn)*\] to throw error matching \/hello\/ but got 'testing'$/); err(function () { expect(badFn, 'blah').to.throw(Error, /hello/); }, /^blah: expected \[Function( badFn)*\] to throw error matching \/hello\/ but got 'testing'$/); err(function () { expect(badFn).to.throw(Error, 'hello', 'blah'); }, /^blah: expected \[Function( badFn)*\] to throw error including 'hello' but got 'testing'$/); err(function () { expect(badFn, 'blah').to.throw(Error, 'hello'); }, /^blah: expected \[Function( badFn)*\] to throw error including 'hello' but got 'testing'$/); err(function () { expect(customErrFn, 'blah').to.not.throw(); }, /^blah: expected \[Function( customErrFn)*\] to not throw an error but 'CustomError: foo' was thrown$/); err(function(){ expect(badFn).to.not.throw(Error, 'testing', 'blah'); }, /^blah: expected \[Function( badFn)*\] to not throw 'Error' but 'Error: testing' was thrown$/); err(function(){ expect(badFn, 'blah').to.not.throw(Error, 'testing'); }, /^blah: expected \[Function( badFn)*\] to not throw 'Error' but 'Error: testing' was thrown$/); err(function(){ expect(emptyStringErrFn).to.not.throw(Error, '', 'blah'); }, /^blah: expected \[Function( emptyStringErrFn)*\] to not throw 'Error' but 'Error' was thrown$/); err(function(){ expect(emptyStringErrFn, 'blah').to.not.throw(Error, ''); }, /^blah: expected \[Function( emptyStringErrFn)*\] to not throw 'Error' but 'Error' was thrown$/); err(function(){ expect(emptyStringErrFn, 'blah').to.not.throw(''); }, /^blah: expected \[Function( emptyStringErrFn)*\] to throw error not including ''$/); err(function () { expect({}, 'blah').to.throw(); }, "blah: expected {} to be a function"); err(function () { expect({}).to.throw(Error, 'testing', 'blah'); }, "blah: expected {} to be a function"); }); it('respondTo', function(){ function Foo(){}; Foo.prototype.bar = function(){}; Foo.func = function() {}; var bar = {}; bar.foo = function(){}; expect(Foo).to.respondTo('bar'); expect(Foo).to.not.respondTo('foo'); expect(Foo).itself.to.respondTo('func'); expect(Foo).itself.not.to.respondTo('bar'); expect(bar).to.respondTo('foo'); err(function(){ expect(Foo).to.respondTo('baz', 'constructor'); }, /^(constructor: expected)(.*)(\[Function Foo\])(.*)(to respond to \'baz\')$/); err(function(){ expect(Foo, 'constructor').to.respondTo('baz'); }, /^(constructor: expected)(.*)(\[Function Foo\])(.*)(to respond to \'baz\')$/); err(function(){ expect(bar).to.respondTo('baz', 'object'); }, /^(object: expected)(.*)(\{ foo: \[Function\] \}|\{ Object \()(.*)(to respond to \'baz\')$/); err(function(){ expect(bar, 'object').to.respondTo('baz'); }, /^(object: expected)(.*)(\{ foo: \[Function\] \}|\{ Object \()(.*)(to respond to \'baz\')$/); }); it('satisfy', function(){ var matcher = function (num) { return num === 1; }; expect(1).to.satisfy(matcher); err(function(){ expect(2).to.satisfy(matcher, 'blah'); }, /^blah: expected 2 to satisfy \[Function( matcher)*\]$/); err(function(){ expect(2, 'blah').to.satisfy(matcher); }, /^blah: expected 2 to satisfy \[Function( matcher)*\]$/); }); it('closeTo', function(){ expect(1.5).to.be.closeTo(1.0, 0.5); expect(10).to.be.closeTo(20, 20); expect(-10).to.be.closeTo(20, 30); err(function(){ expect(2).to.be.closeTo(1.0, 0.5, 'blah'); }, "blah: expected 2 to be close to 1 +/- 0.5"); err(function(){ expect(2, 'blah').to.be.closeTo(1.0, 0.5); }, "blah: expected 2 to be close to 1 +/- 0.5"); err(function(){ expect(-10).to.be.closeTo(20, 29, 'blah'); }, "blah: expected -10 to be close to 20 +/- 29"); err(function() { expect([1.5]).to.be.closeTo(1.0, 0.5, 'blah'); }, "blah: expected [ 1.5 ] to be a number"); err(function() { expect([1.5], 'blah').to.be.closeTo(1.0, 0.5); }, "blah: expected [ 1.5 ] to be a number"); err(function() { expect(1.5).to.be.closeTo("1.0", 0.5, 'blah'); }, "blah: the arguments to closeTo or approximately must be numbers"); err(function() { expect(1.5, 'blah').to.be.closeTo("1.0", 0.5); }, "blah: the arguments to closeTo or approximately must be numbers"); err(function() { expect(1.5).to.be.closeTo(1.0, true, 'blah'); }, "blah: the arguments to closeTo or approximately must be numbers"); err(function() { expect(1.5, 'blah').to.be.closeTo(1.0, true); }, "blah: the arguments to closeTo or approximately must be numbers"); err(function() { expect(1.5, 'blah').to.be.closeTo(1.0); }, "blah: the arguments to closeTo or approximately must be numbers, and a delta is required"); }); it('approximately', function(){ expect(1.5).to.be.approximately(1.0, 0.5); expect(10).to.be.approximately(20, 20); expect(-10).to.be.approximately(20, 30); err(function(){ expect(2).to.be.approximately(1.0, 0.5, 'blah'); }, "blah: expected 2 to be close to 1 +/- 0.5"); err(function(){ expect(-10).to.be.approximately(20, 29, 'blah'); }, "blah: expected -10 to be close to 20 +/- 29"); err(function() { expect([1.5]).to.be.approximately(1.0, 0.5); }, "expected [ 1.5 ] to be a number"); err(function() { expect(1.5).to.be.approximately("1.0", 0.5); }, "the arguments to closeTo or approximately must be numbers"); err(function() { expect(1.5).to.be.approximately(1.0, true); }, "the arguments to closeTo or approximately must be numbers"); err(function() { expect(1.5).to.be.approximately(1.0); }, "the arguments to closeTo or approximately must be numbers, and a delta is required"); }); it('oneOf', function() { expect(1).to.be.oneOf([1, 2, 3]); expect('1').to.not.be.oneOf([1, 2, 3]); expect([3, [4]]).to.not.be.oneOf([1, 2, [3, 4]]); var threeFour = [3, [4]]; expect(threeFour).to.be.oneOf([1, 2, threeFour]); expect([]).to.be.deep.oneOf([[], '']); expect([1, 2]).to.contain.oneOf([4,2,5]); expect([3, 4]).to.not.contain.oneOf([2,1,5]); expect('The quick brown fox jumps over the lazy dog').to.contain.oneOf(['cat', 'dog', 'bird']); expect('The quick brown fox jumps over the lazy dog').to.not.contain.oneOf(['elephant', 'pigeon', 'lynx']); err(function () { expect(1).to.be.oneOf([2, 3], 'blah'); }, "blah: expected 1 to be one of [ 2, 3 ]"); err(function () { expect(1, 'blah').to.be.oneOf([2, 3]); }, "blah: expected 1 to be one of [ 2, 3 ]"); err(function () { expect(1).to.not.be.oneOf([1, 2, 3], 'blah'); }, "blah: expected 1 to not be one of [ 1, 2, 3 ]"); err(function () { expect(1, 'blah').to.not.be.oneOf([1, 2, 3]); }, "blah: expected 1 to not be one of [ 1, 2, 3 ]"); err(function () { expect(1).to.be.oneOf({}, 'blah'); }, "blah: expected {} to be an array"); err(function () { expect(1, 'blah').to.be.oneOf({}); }, "blah: expected {} to be an array"); }); it('include.members', function() { expect([1, 2, 3]).to.include.members([]); expect([1, 2, 3]).to.include.members([3, 2]); expect([1, 2, 3]).to.include.members([3, 2, 2]); expect([1, 2, 3]).to.not.include.members([8, 4]); expect([1, 2, 3]).to.not.include.members([1, 2, 3, 4]); expect([{a: 1}]).to.not.include.members([{a: 1}]); err(function() { expect([1, 2, 3]).to.include.members([2, 5], 'blah'); }, 'blah: expected [ 1, 2, 3 ] to be a superset of [ 2, 5 ]'); err(function() { expect([1, 2, 3], 'blah').to.include.members([2, 5]); }, 'blah: expected [ 1, 2, 3 ] to be a superset of [ 2, 5 ]'); err(function() { expect([1, 2, 3]).to.not.include.members([2, 1]); }, 'expected [ 1, 2, 3 ] to not be a superset of [ 2, 1 ]'); }); it('same.members', function() { expect([5, 4]).to.have.same.members([4, 5]); expect([5, 4]).to.have.same.members([5, 4]); expect([5, 4, 4]).to.have.same.members([5, 4, 4]); expect([5, 4]).to.not.have.same.members([]); expect([5, 4]).to.not.have.same.members([6, 3]); expect([5, 4]).to.not.have.same.members([5, 4, 2]); expect([5, 4]).to.not.have.same.members([5, 4, 4]); expect([5, 4, 4]).to.not.have.same.members([5, 4]); expect([5, 4, 4]).to.not.have.same.members([5, 4, 3]); expect([5, 4, 3]).to.not.have.same.members([5, 4, 4]); }); it('members', function() { expect([5, 4]).members([4, 5]); expect([5, 4]).members([5, 4]); expect([5, 4, 4]).members([5, 4, 4]); expect([5, 4]).not.members([]); expect([5, 4]).not.members([6, 3]); expect([5, 4]).not.members([5, 4, 2]); expect([5, 4]).not.members([5, 4, 4]); expect([5, 4, 4]).not.members([5, 4]); expect([5, 4, 4]).not.members([5, 4, 3]); expect([5, 4, 3]).not.members([5, 4, 4]); expect([{ id: 1 }]).not.members([{ id: 1 }]); err(function() { expect([1, 2, 3]).members([2, 1, 5], 'blah'); }, 'blah: expected [ 1, 2, 3 ] to have the same members as [ 2, 1, 5 ]'); err(function() { expect([1, 2, 3], 'blah').members([2, 1, 5]); }, 'blah: expected [ 1, 2, 3 ] to have the same members as [ 2, 1, 5 ]'); err(function() { expect([1, 2, 3]).not.members([2, 1, 3]); }, 'expected [ 1, 2, 3 ] to not have the same members as [ 2, 1, 3 ]'); err(function () { expect({}).members([], 'blah'); }, 'blah: expected {} to be an array'); err(function () { expect({}, 'blah').members([]); }, 'blah: expected {} to be an array'); err(function () { expect([]).members({}, 'blah'); }, 'blah: expected {} to be an array'); err(function () { expect([], 'blah').members({}); }, 'blah: expected {} to be an array'); }); it('deep.members', function() { expect([{ id: 1 }]).deep.members([{ id: 1 }]); expect([{a: 1}, {b: 2}, {b: 2}]).deep.members([{a: 1}, {b: 2}, {b: 2}]); expect([{ id: 2 }]).not.deep.members([{ id: 1 }]); expect([{a: 1}, {b: 2}]).not.deep.members([{a: 1}, {b: 2}, {b: 2}]); expect([{a: 1}, {b: 2}, {b: 2}]).not.deep.members([{a: 1}, {b: 2}]); expect([{a: 1}, {b: 2}, {b: 2}]).not.deep.members([{a: 1}, {b: 2}, {c: 3}]); expect([{a: 1}, {b: 2}, {c: 3}]).not.deep.members([{a: 1}, {b: 2}, {b: 2}]); err(function(){ expect([{ id: 1 }]).deep.members([{ id: 2 }], 'blah') }, 'blah: expected [ { id: 1 } ] to have the same members as [ { id: 2 } ]'); err(function(){ expect([{ id: 1 }], 'blah').deep.members([{ id: 2 }]) }, 'blah: expected [ { id: 1 } ] to have the same members as [ { id: 2 } ]'); }); it('include.deep.members', function() { expect([{a: 1}, {b: 2}, {c: 3}]).include.deep.members([{b: 2}, {a: 1}]); expect([{a: 1}, {b: 2}, {c: 3}]).include.deep.members([{b: 2}, {a: 1}, {a: 1}]); expect([{a: 1}, {b: 2}, {c: 3}]).not.include.deep.members([{b: 2}, {a: 1}, {f: 5}]); err(function() { expect([{a: 1}, {b: 2}, {c: 3}]).include.deep.members([{b: 2}, {a: 1}, {f: 5}], 'blah'); }, 'blah: expected [ { a: 1 }, { b: 2 }, { c: 3 } ] to be a superset of [ { b: 2 }, { a: 1 }, { f: 5 } ]'); err(function() { expect([{a: 1}, {b: 2}, {c: 3}], 'blah').include.deep.members([{b: 2}, {a: 1}, {f: 5}]); }, 'blah: expected [ { a: 1 }, { b: 2 }, { c: 3 } ] to be a superset of [ { b: 2 }, { a: 1 }, { f: 5 } ]'); }); it('ordered.members', function() { expect([1, 2, 3]).ordered.members([1, 2, 3]); expect([1, 2, 2]).ordered.members([1, 2, 2]); expect([1, 2, 3]).not.ordered.members([2, 1, 3]); expect([1, 2, 3]).not.ordered.members([1, 2]); expect([1, 2]).not.ordered.members([1, 2, 2]); expect([1, 2, 2]).not.ordered.members([1, 2]); expect([1, 2, 2]).not.ordered.members([1, 2, 3]); expect([1, 2, 3]).not.ordered.members([1, 2, 2]); err(function() { expect([1, 2, 3]).ordered.members([2, 1, 3], 'blah'); }, 'blah: expected [ 1, 2, 3 ] to have the same ordered members as [ 2, 1, 3 ]'); err(function() { expect([1, 2, 3], 'blah').ordered.members([2, 1, 3]); }, 'blah: expected [ 1, 2, 3 ] to have the same ordered members as [ 2, 1, 3 ]'); err(function() { expect([1, 2, 3]).not.ordered.members([1, 2, 3]); }, 'expected [ 1, 2, 3 ] to not have the same ordered members as [ 1, 2, 3 ]'); }); it('include.ordered.members', function() { expect([1, 2, 3]).include.ordered.members([1, 2]); expect([1, 2, 3]).not.include.ordered.members([2, 1]); expect([1, 2, 3]).not.include.ordered.members([2, 3]); expect([1, 2, 3]).not.include.ordered.members([1, 2, 2]); err(function() { expect([1, 2, 3]).include.ordered.members([2, 1], 'blah'); }, 'blah: expected [ 1, 2, 3 ] to be an ordered superset of [ 2, 1 ]'); err(function() { expect([1, 2, 3], 'blah').include.ordered.members([2, 1]); }, 'blah: expected [ 1, 2, 3 ] to be an ordered superset of [ 2, 1 ]'); err(function() { expect([1, 2, 3]).not.include.ordered.members([1, 2]); }, 'expected [ 1, 2, 3 ] to not be an ordered superset of [ 1, 2 ]'); }); it('deep.ordered.members', function() { expect([{a: 1}, {b: 2}, {c: 3}]).deep.ordered.members([{a: 1}, {b: 2}, {c: 3}]); expect([{a: 1}, {b: 2}, {b: 2}]).deep.ordered.members([{a: 1}, {b: 2}, {b: 2}]); expect([{a: 1}, {b: 2}, {c: 3}]).not.deep.ordered.members([{b: 2}, {a: 1}, {c: 3}]); expect([{a: 1}, {b: 2}]).not.deep.ordered.members([{a: 1}, {b: 2}, {b: 2}]); expect([{a: 1}, {b: 2}, {b: 2}]).not.deep.ordered.members([{a: 1}, {b: 2}]); expect([{a: 1}, {b: 2}, {b: 2}]).not.deep.ordered.members([{a: 1}, {b: 2}, {c: 3}]); expect([{a: 1}, {b: 2}, {c: 3}]).not.deep.ordered.members([{a: 1}, {b: 2}, {b: 2}]); err(function() { expect([{a: 1}, {b: 2}, {c: 3}]).deep.ordered.members([{b: 2}, {a: 1}, {c: 3}], 'blah'); }, 'blah: expected [ { a: 1 }, { b: 2 }, { c: 3 } ] to have the same ordered members as [ { b: 2 }, { a: 1 }, { c: 3 } ]'); err(function() { expect([{a: 1}, {b: 2}, {c: 3}], 'blah').deep.ordered.members([{b: 2}, {a: 1}, {c: 3}]); }, 'blah: expected [ { a: 1 }, { b: 2 }, { c: 3 } ] to have the same ordered members as [ { b: 2 }, { a: 1 }, { c: 3 } ]'); err(function() { expect([{a: 1}, {b: 2}, {c: 3}]).not.deep.ordered.members([{a: 1}, {b: 2}, {c: 3}]); }, 'expected [ { a: 1 }, { b: 2 }, { c: 3 } ] to not have the same ordered members as [ { a: 1 }, { b: 2 }, { c: 3 } ]'); }); it('include.deep.ordered.members', function() { expect([{a: 1}, {b: 2}, {c: 3}]).include.deep.ordered.members([{a: 1}, {b: 2}]); expect([{a: 1}, {b: 2}, {c: 3}]).not.include.deep.ordered.members([{b: 2}, {a: 1}]); expect([{a: 1}, {b: 2}, {c: 3}]).not.include.deep.ordered.members([{b: 2}, {c: 3}]); expect([{a: 1}, {b: 2}, {c: 3}]).not.include.deep.ordered.members([{a: 1}, {b: 2}, {b: 2}]); err(function() { expect([{a: 1}, {b: 2}, {c: 3}]).include.deep.ordered.members([{b: 2}, {a: 1}], 'blah'); }, 'blah: expected [ { a: 1 }, { b: 2 }, { c: 3 } ] to be an ordered superset of [ { b: 2 }, { a: 1 } ]'); err(function() { expect([{a: 1}, {b: 2}, {c: 3}], 'blah').include.deep.ordered.members([{b: 2}, {a: 1}]); }, 'blah: expected [ { a: 1 }, { b: 2 }, { c: 3 } ] to be an ordered superset of [ { b: 2 }, { a: 1 } ]'); err(function() { expect([{a: 1}, {b: 2}, {c: 3}]).not.include.deep.ordered.members([{a: 1}, {b: 2}]); }, 'expected [ { a: 1 }, { b: 2 }, { c: 3 } ] to not be an ordered superset of [ { a: 1 }, { b: 2 } ]'); }); it('change', function() { var obj = { value: 10, str: 'foo' }, heroes = ['spiderman', 'superman'], fn = function() { obj.value += 5 }, decFn = function() { obj.value -= 20 }, sameFn = function() { 'foo' + 'bar' }, bangFn = function() { obj.str += '!' }, batFn = function() { heroes.push('batman') }, lenFn = function() { return heroes.length }; expect(fn).to.change(obj, 'value'); expect(fn).to.change(obj, 'value').by(5); expect(fn).to.change(obj, 'value').by(-5); expect(decFn).to.change(obj, 'value').by(20); expect(decFn).to.change(obj, 'value').but.not.by(21); expect(sameFn).to.not.change(obj, 'value'); expect(sameFn).to.not.change(obj, 'str'); expect(bangFn).to.change(obj, 'str'); expect(batFn).to.change(lenFn).by(1); expect(batFn).to.change(lenFn).but.not.by(2); err(function () { expect(sameFn).to.change(obj, 'value', 'blah'); }, "blah: expected .value to change"); err(function () { expect(sameFn, 'blah').to.change(obj, 'value'); }, "blah: expected .value to change"); err(function () { expect(fn).to.not.change(obj, 'value', 'blah'); }, "blah: expected .value to not change"); err(function () { expect({}).to.change(obj, 'value', 'blah'); }, "blah: expected {} to be a function"); err(function () { expect({}, 'blah').to.change(obj, 'value'); }, "blah: expected {} to be a function"); err(function () { expect(fn).to.change({}, 'badprop', 'blah'); }, "blah: expected {} to have property 'badprop'"); err(function () { expect(fn, 'blah').to.change({}, 'badprop'); }, "blah: expected {} to have property 'badprop'"); err(function () { expect(fn, 'blah').to.change({}); }, "blah: expected {} to be a function"); err(function () { expect(fn).to.change(obj, 'value').by(10, 'blah'); }, "blah: expected .value to change by 10"); err(function () { expect(fn, 'blah').to.change(obj, 'value').by(10); }, "blah: expected .value to change by 10"); err(function () { expect(fn).to.change(obj, 'value').but.not.by(5, 'blah'); }, "blah: expected .value to not change by 5"); }); it('increase, decrease', function() { var obj = { value: 10, noop: null }, arr = ['one', 'two'], pFn = function() { arr.push('three') }, popFn = function() { arr.pop() }, nFn = function() { return null }, lenFn = function() { return arr.length }, incFn = function() { obj.value += 2 }, decFn = function() { obj.value -= 3 }, smFn = function() { obj.value += 0 }; expect(smFn).to.not.increase(obj, 'value'); expect(decFn).to.not.increase(obj, 'value'); expect(incFn).to.increase(obj, 'value'); expect(incFn).to.increase(obj, 'value').by(2); expect(incFn).to.increase(obj, 'value').but.not.by(1); expect(smFn).to.not.decrease(obj, 'value'); expect(incFn).to.not.decrease(obj, 'value'); expect(decFn).to.decrease(obj, 'value'); expect(decFn).to.decrease(obj, 'value').by(3); expect(decFn).to.decrease(obj, 'value').but.not.by(2); expect(popFn).to.not.increase(lenFn); expect(nFn).to.not.increase(lenFn); expect(pFn).to.increase(lenFn); expect(pFn).to.increase(lenFn).by(1); expect(pFn).to.increase(lenFn).but.not.by(2); expect(popFn).to.decrease(lenFn); expect(popFn).to.decrease(lenFn).by(1); expect(popFn).to.decrease(lenFn).but.not.by(2); expect(nFn).to.not.decrease(lenFn); expect(pFn).to.not.decrease(lenFn); err(function () { expect(smFn).to.increase(obj, 'value', 'blah'); }, "blah: expected .value to increase"); err(function () { expect(smFn, 'blah').to.increase(obj, 'value'); }, "blah: expected .value to increase"); err(function () { expect(incFn).to.not.increase(obj, 'value', 'blah'); }, "blah: expected .value to not increase"); err(function () { expect({}).to.increase(obj, 'value', 'blah'); }, "blah: expected {} to be a function"); err(function () { expect({}, 'blah').to.increase(obj, 'value'); }, "blah: expected {} to be a function"); err(function () { expect(incFn).to.increase({}, 'badprop', 'blah'); }, "blah: expected {} to have property 'badprop'"); err(function () { expect(incFn, 'blah').to.increase({}, 'badprop'); }, "blah: expected {} to have property 'badprop'"); err(function () { expect(incFn, 'blah').to.increase({}); }, "blah: expected {} to be a function"); err(function() { expect(incFn).to.increase(obj, 'noop', 'blah'); }, 'blah: expected null to be a number'); err(function() { expect(incFn, 'blah').to.increase(obj, 'noop'); }, 'blah: expected null to be a number'); err(function () { expect(incFn).to.increase(obj, 'value').by(10, 'blah'); }, "blah: expected .value to increase by 10"); err(function () { expect(incFn, 'blah').to.increase(obj, 'value').by(10); }, "blah: expected .value to increase by 10"); err(function () { expect(incFn).to.increase(obj, 'value').but.not.by(2, 'blah'); }, "blah: expected .value to not increase by 2"); err(function () { expect(smFn).to.decrease(obj, 'value', 'blah'); }, "blah: expected .value to decrease"); err(function () { expect(smFn, 'blah').to.decrease(obj, 'value'); }, "blah: expected .value to decrease"); err(function () { expect(decFn).to.not.decrease(obj, 'value', 'blah'); }, "blah: expected .value to not decrease"); err(function () { expect({}).to.decrease(obj, 'value', 'blah'); }, "blah: expected {} to be a function"); err(function () { expect({}, 'blah').to.decrease(obj, 'value'); }, "blah: expected {} to be a function"); err(function () { expect(decFn).to.decrease({}, 'badprop', 'blah'); }, "blah: expected {} to have property 'badprop'"); err(function () { expect(decFn, 'blah').to.decrease({}, 'badprop'); }, "blah: expected {} to have property 'badprop'"); err(function () { expect(decFn, 'blah').to.decrease({}); }, "blah: expected {} to be a function"); err(function() { expect(decFn).to.decrease(obj, 'noop', 'blah'); }, 'blah: expected null to be a number'); err(function() { expect(decFn, 'blah').to.decrease(obj, 'noop'); }, 'blah: expected null to be a number'); err(function () { expect(decFn).to.decrease(obj, 'value').by(10, 'blah'); }, "blah: expected .value to decrease by 10"); err(function () { expect(decFn, 'blah').to.decrease(obj, 'value').by(10); }, "blah: expected .value to decrease by 10"); err(function () { expect(decFn).to.decrease(obj, 'value').but.not.by(3, 'blah'); }, "blah: expected .value to not decrease by 3"); }); it('extensible', function() { var nonExtensibleObject = Object.preventExtensions({}); expect({}).to.be.extensible; expect(nonExtensibleObject).to.not.be.extensible; err(function() { expect(nonExtensibleObject, 'blah').to.be.extensible; }, 'blah: expected {} to be extensible'); err(function() { expect({}).to.not.be.extensible; }, 'expected {} to not be extensible'); // Making sure ES6-like Object.isExtensible response is respected for all primitive types expect(42).to.not.be.extensible; expect(null).to.not.be.extensible; expect('foo').to.not.be.extensible; expect(false).to.not.be.extensible; expect(undefined).to.not.be.extensible; if (typeof Symbol === 'function') { expect(Symbol()).to.not.be.extensible; } err(function() { expect(42).to.be.extensible; }, 'expected 42 to be extensible'); err(function() { expect(null).to.be.extensible; }, 'expected null to be extensible'); err(function() { expect('foo').to.be.extensible; }, 'expected \'foo\' to be extensible'); err(function() { expect(false).to.be.extensible; }, 'expected false to be extensible'); err(function() { expect(undefined).to.be.extensible; }, 'expected undefined to be extensible'); if (typeof Proxy === 'function') { var proxy = new Proxy({}, { isExtensible: function() { throw new TypeError(); } }); err(function() { // .extensible should not suppress errors, thrown in proxy traps expect(proxy).to.be.extensible; }, { name: 'TypeError' }, true); } }); it('sealed', function() { var sealedObject = Object.seal({}); expect(sealedObject).to.be.sealed; expect({}).to.not.be.sealed; err(function() { expect({}, 'blah').to.be.sealed; }, 'blah: expected {} to be sealed'); err(function() { expect(sealedObject).to.not.be.sealed; }, 'expected {} to not be sealed'); // Making sure ES6-like Object.isSealed response is respected for all primitive types expect(42).to.be.sealed; expect(null).to.be.sealed; expect('foo').to.be.sealed; expect(false).to.be.sealed; expect(undefined).to.be.sealed; if (typeof Symbol === 'function') { expect(Symbol()).to.be.sealed; } err(function() { expect(42).to.not.be.sealed; }, 'expected 42 to not be sealed'); err(function() { expect(null).to.not.be.sealed; }, 'expected null to not be sealed'); err(function() { expect('foo').to.not.be.sealed; }, 'expected \'foo\' to not be sealed'); err(function() { expect(false).to.not.be.sealed; }, 'expected false to not be sealed'); err(function() { expect(undefined).to.not.be.sealed; }, 'expected undefined to not be sealed'); if (typeof Proxy === 'function') { var proxy = new Proxy({}, { ownKeys: function() { throw new TypeError(); } }); // Object.isSealed will call ownKeys trap only if object is not extensible Object.preventExtensions(proxy); err(function() { // .sealed should not suppress errors, thrown in proxy traps expect(proxy).to.be.sealed; }, { name: 'TypeError' }, true); } }); it('frozen', function() { var frozenObject = Object.freeze({}); expect(frozenObject).to.be.frozen; expect({}).to.not.be.frozen; err(function() { expect({}, 'blah').to.be.frozen; }, 'blah: expected {} to be frozen'); err(function() { expect(frozenObject).to.not.be.frozen; }, 'expected {} to not be frozen'); // Making sure ES6-like Object.isFrozen response is respected for all primitive types expect(42).to.be.frozen; expect(null).to.be.frozen; expect('foo').to.be.frozen; expect(false).to.be.frozen; expect(undefined).to.be.frozen; if (typeof Symbol === 'function') { expect(Symbol()).to.be.frozen; } err(function() { expect(42).to.not.be.frozen; }, 'expected 42 to not be frozen'); err(function() { expect(null).to.not.be.frozen; }, 'expected null to not be frozen'); err(function() { expect('foo').to.not.be.frozen; }, 'expected \'foo\' to not be frozen'); err(function() { expect(false).to.not.be.frozen; }, 'expected false to not be frozen'); err(function() { expect(undefined).to.not.be.frozen; }, 'expected undefined to not be frozen'); if (typeof Proxy === 'function') { var proxy = new Proxy({}, { ownKeys: function() { throw new TypeError(); } }); // Object.isFrozen will call ownKeys trap only if object is not extensible Object.preventExtensions(proxy); err(function() { // .frozen should not suppress errors, thrown in proxy traps expect(proxy).to.be.frozen; }, { name: 'TypeError' }, true); } });});