Zaista / cypress-mongodb

Cypress MongoDB plugin
MIT License
17 stars 8 forks source link

Allow createCollection and dropCollection to return rather than throw error #12

Closed ajhoddinott closed 2 years ago

ajhoddinott commented 2 years ago

Please create feature requests for things you'd like to see.

Feature request/suggestion in the form of a proof-of-concept PR.

Motivation: I want to be able to clear collection(s) before testing, to ensure a consistent state.

  cy.dropCollection('collection_a') // but continue silently on NamespaceNotFound
  cy.dropCollection('collection_b') // ... ditto ...

The errors might be caught and discarded with cy.on('fail', but it means adding boilerplate to distinguish between expected and unexpected failures in each test, or maybe wrapping the commands in support/index.js.

This PR allows for:

  cy.dropCollection('collection', { noThrow: true })
    .then(resultOrError => {})
  cy.createCollection('collection', { noThrow: true })
    .then(resultOrError => {})

The methodology might be extended to the validate() errors and other commands, but my immediate need is for dropCollection when the collection doesn’t exist.

Zaista commented 2 years ago

Hey, thanks a lot for the PR. Idea is awesome, I can also extend it later on for other commands. For this one I would just like to change 'noThrow' name to something that points better at what it does, like 'failSilently', 'continueOnError', or smth like that. Please also rename the added test titles to 'Should not fail creating/dropping...'

ajhoddinott commented 2 years ago

Thanks for the quick response and feedback. I’ll go with 'failSilently'.

I thought I’d add this to README.md at the bottom of the Usage section, or would you rather I left that to you?

createCollection and dropCollection have the option to failSilently.

cy.createCollection('existing_collection', { failSilently: true}).then(res => {
    cy.log(res);  // Error object if collection already exists
});

cy.dropCollection('nonexistent_collection', { failSilently: true}).then(res => {
    cy.log(res);  // Error object if collection doesn’t exist
});
Zaista commented 2 years ago

Feel free to add that to the readme file as well, thanks

Zaista commented 2 years ago

Merged and released to npm. Thanks a lot for your contribution :) Happy testing!