GithubHelp home page GithubHelp logo

Expect object to have property about code HOT 13 CLOSED

hapijs avatar hapijs commented on June 17, 2024
Expect object to have property

from code.

Comments (13)

AdriVanHoudt avatar AdriVanHoudt commented on June 17, 2024

Can you do expect(obj.a).to.exist().and.to.equal('foo')?

from code.

Marsup avatar Marsup commented on June 17, 2024

Less elegant but yes that would work. I'll wait for @cjihrig to advise on whether we add this.

from code.

cjihrig avatar cjihrig commented on June 17, 2024

I personally prefer @AdriVanHoudt's version, but I would take a PR for this.

from code.

hueniverse avatar hueniverse commented on June 17, 2024

I purposely didn't include it because expect(obj).to.contain({ a: 'foo' }) does the trick just fine.

from code.

AdriVanHoudt avatar AdriVanHoudt commented on June 17, 2024

I prefer Eran's solution but it doesn't allow you to do expect(obj.a).to.exist().and.to.contains('foo')

from code.

hueniverse avatar hueniverse commented on June 17, 2024

What's the diff? if obj.a doesn't exist, my assertion will fail.

from code.

AdriVanHoudt avatar AdriVanHoudt commented on June 17, 2024

In your case you check if obj has the property a with value foo in my case you check if obj.a contains the value foo so it can also be foo bar or am I doing it wrong?

from code.

hueniverse avatar hueniverse commented on June 17, 2024

Ah, you are using the string version. I generally avoid using partial comparisons...

from code.

AdriVanHoudt avatar AdriVanHoudt commented on June 17, 2024

Ah yes if you take the original request your version would be better

from code.

Marsup avatar Marsup commented on June 17, 2024

Does it work for deep equal as well ? Like expect(obj).to.contain({ a: { foo: 'bar' }}).
Is it partial ? (ie. a may not only contain foo).
I'm trying to also find alternatives to expect(obj).to.have.deep.property('a.foo').that.equal('bar').

from code.

hueniverse avatar hueniverse commented on June 17, 2024

It should. Look at the tests in both hoek and code.

from code.

Marsup avatar Marsup commented on June 17, 2024

Well nope :

> Code.expect({ foo: { bar: 'baz', foo: 'bar' }}).to.deep.include({ foo: { bar: 'baz' } })
Error: Expected { foo: { bar: 'baz', foo: 'bar' } } to include { foo: { bar: 'baz' } }

from code.

lock avatar lock commented on June 17, 2024

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

from code.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.