Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a compare function to be used in expected object template #66

Merged
merged 2 commits into from
Sep 5, 2017
Merged

Adding a compare function to be used in expected object template #66

merged 2 commits into from
Sep 5, 2017

Conversation

sergejsha
Copy link
Contributor

@sergejsha sergejsha commented Aug 27, 2017

The idea of this PR is to add a possibility of using a compare function inside the expected object. This allows not only checking for static values, but also adding more sophisticated checks like check for being not null, check for been defined etc.

Here is an example. I store an object and then additionally check that id, createdAt and updatedAt properties are present in the object. The check would not be possible without the compare function because returned values are expected to be different between different test runs.

        it('should store object', async function () {
            const publisherData = {
                name: 'publisher',
                descr: 'description',
                urls: { homepage: 'http://homepage' }
            }
            const publisher = await repository.findOrCreatePublisher(publisherData)
            expect(publisher).containSubset({
                ...publisherData,
                id: id => id,
                createdAt: createdAt => createdAt,
                updatedAt: updatedAt => updatedAt
            })
        })

This might also solve #64.

@sergejsha sergejsha changed the title Adding compare function to be used in expected object template Adding a compare function to be used in expected object template Aug 27, 2017
@eagleeye
Copy link
Contributor

@beworker not bad =) Please add some examples to the README

@sergejsha
Copy link
Contributor Author

Here we are. Some examples were added.

@eagleeye eagleeye merged commit 54e486e into debitoor:master Sep 5, 2017
@eagleeye
Copy link
Contributor

eagleeye commented Sep 5, 2017

@beworker thanks a lot!

@sergejsha
Copy link
Contributor Author

Thanks you for releasing! Now I can remove locally modified chai-subset copy :)

@elmigranto
Copy link

This breaks comparing functions (see #68) or throws on class instances.

const int = (left, right) => left - right
class Expected {}

expect({comparator: int, ctor: Expected}).to.containSubset({
  comparator: int, // fails, even though function is the same
  ctor: Expected,  // throws TypeError
})

// TypeError: Class constructor Expected cannot be invoked without 'new'
//      at <redacted>/node_modules/chai-subset/lib/chai-subset.js:75:13
//      at Array.every (<anonymous>)
//      at compare (node_modules/chai-subset/lib/chai-subset.js:68:33)
//      at Proxy.<anonymous> (node_modules/chai-subset/lib/chai-subset.js:21:5)
//      <redacted>

@beworker @eagleeye

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants