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

Test database not reset between tests #136

Open
nilnullzip opened this issue Mar 12, 2015 · 15 comments
Open

Test database not reset between tests #136

nilnullzip opened this issue Mar 12, 2015 · 15 comments

Comments

@nilnullzip
Copy link

Is this the expected behavior for Mocha/Velocity?

@rissem
Copy link
Collaborator

rissem commented Mar 13, 2015

That is expected right now. We should probably do a better job communicating that. It sounds like you would like to have the database cleared between tests? Or between test runs?

@nilnullzip
Copy link
Author

Yes, the database needs to be cleared to ensure that the starting conditions are the same every time. Otherwise you can get unrepeatable results. Jasmine appears to clear the database as my app runs through its initialization and sets up the database from the empty state.

At the very least it needs to be cleared between test runs. I'm not sure I understand the exact distinction between tests and test runs. So I'm not sure how to answer the question if it has to be cleared between tests too.

@rissem
Copy link
Collaborator

rissem commented Mar 13, 2015

I agree that some kind of reset should happen. The question to me is who should be responsible for resetting (framework, Velocity, or user), and how often it should happen. Resetting the db after each test seems like it'd be too slow, but at the start of the test suite seems like it'd offer a good balance of speed and a clear starting point.

@nilnullzip
Copy link
Author

I would think the logical place is in Velocity when the mirror is set up. Does that happen only once at the beginning of a suite of tests? Or does it happen before every test?

Actually I think clearing the database should be very fast in Mongo as it is accomplished by deleting all of the DB files.

@aramk
Copy link

aramk commented Mar 21, 2015

Yeah I agree, following discussion from https://github.com/meteor-velocity/velocity/issues/39 it seems like this should already be implemented in the mirror? In the meantime, I'm using a utility method I wrote here.

beforeEach ->
      Collections.removeAllDocs(Orders)
      Collections.removeAllDocs(OrderSchedules)

@aramk
Copy link

aramk commented Mar 21, 2015

I've found a slightly better temporary solution. If you have a beforeEach outside of any describe in Mocha, it will run for each test case in all suites, so I created a test called _startupTest.coffee - I'm assuming it will load tests in alphabetical order.

MochaWeb?.testOnly ->

  beforeEach ->
    console.log('Clearing test collections')
    _.each [Orders, OrderSchedules, Runs, RunSchedules, Customers, Products], (collection) ->
      Collections.removeAllDocs(collection)

@charleshan
Copy link

Is there a meteor method do get this done from Velocity?

@carlosbaraza
Copy link

👍 Any news about this? How do you guys usually clear the DB after tests without modifying the actual mocha package?

@charleshan
Copy link

@carlosbaraza Although in my opinion this is not the best solution, what I ended up doing is remove every item in each collection manually using beforeEach(). I did this because my current permissions do not allow me to drop databases on the client side.

if (!(typeof MochaWeb === 'undefined')){
  MochaWeb.testOnly(function(){
    beforeEach(function() {
      Collection1.find().forEach(function(item) {
        Collection1.remove(item._id);
      });
      Collection2.find().forEach(function(item) {
        Collection2.remove(item._id);
      });
    });
  });
}

@carlosbaraza
Copy link

Hi @OminStyle,

But what about cleaning the full database, not only the documents available in the client? I want to have a clean way to clear the database without compromising security, letting docs to be deleted from the client.

Maybe I am missing still some point from Velocity. Is there some way to set up a server method only callable from tests where I could clear the database?

@ryneeverett
Copy link

@carlosbaraza Agree that this isn't ideal, but couldn't you simply add the methods in a debugOnly package?

@carlosbaraza
Copy link

Hi @ryneeverett, thank you, that's an interesting point. I am still kind of new on the Meteor world, so I am figuring out the best way to do things. That might be my solution for the moment.

Do you have some example repository where this is being done?

@carlosbaraza
Copy link

For the moment I could not find someone doing such a debug package to clear the database via methods. However, I found a cool well known package example of the usage of debugOnly https://github.com/msavin/Mongol/blob/master/package.js

@nilnullzip
Copy link
Author

@ryneeverett
Copy link

Here's my implementation (and a mocha example):

https://github.com/ryneeverett/meteor-dbreset

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

No branches or pull requests

6 participants