pouchdb-community / ember-pouch

PouchDB/CouchDB adapter for Ember Data
Apache License 2.0
281 stars 76 forks source link

Empty store after tests #217

Open 0000marcell opened 6 years ago

0000marcell commented 6 years ago

How can i empty the stores after performing a test, I'm getting some async errors like this

pushedData` on <post:F8489E9E-4FD4-FA5A-8BDB-10381B0400B5> while in state root.deleted.inFlight

I'm deleting the models like this

for(let post of posts){
   await run(async ()=> {
      await post.destroyRecord();
   });
}
Matt-Jensen commented 6 years ago

The stack traces on these errors are incredibly frustrating; in my case I believe the issue was an unhandled rejection in a promise chain saving a pouch record.

Almost all rejections related to ember-pouch, in my limited experience, are 409 conflicts.

Can you add a catch to your destroyRecord requests and see if the error is status 409?

0000marcell commented 6 years ago

Hi @Matt-Jensen, i've tried that but the catch never runs, i've tried everything that i could think of, even creating a new db for each test, the worse part is that sometimes the tests run fine and sometimes the error occurs 😩, i don't even know how to reproduce it reliably, the records are always deleted so the code works anyway

jlami commented 6 years ago

Can you try with the new ember-pouch version 5.1.0? This one has some new behavior for deletes.

Otherwise I think the best way is to use a memory adapter for tests. If you then make sure that each test will create a separate app you should be set.

To be able to use the memory adapter you'll have to load the file, as it is not added by default. If you are not using ember-auto-import you can use ember-cli-build.js to add the file to the vendorTree and import it.

...

module.exports = function(defaults) {
  var app = new EmberApp(defaults, {
    trees: {
      vendor: stew.find(path.join(path.dirname(require.resolve('pouchdb')), '..', 'dist'), {
          destDir: 'pouchdb',
          files: ['pouchdb.memory.js']
        })
    },
    ...
  });

  ...

  app.import('vendor/pouchdb/pouchdb.memory.js', { type: 'test' });
  return app.toTree([]);
};

Then setup your PouchDB instance in the adapter with an extra parameter: new Pouch('dbname', {adapter: 'memory'}). Best is to setup some localDBOptions in your environment and set that to that value for the test environment.

jacobq commented 6 years ago

@jlami I am trying to work around this problem too (using ember-pouch v6.0.0). I installed pouchdb-adapter-memory via npm and am using ember-auto-import so was able to do:

// app/adapters/application.ts
import PouchDB from 'pouchdb'; // Note: this is actually provided by ember-pouch
import pouchInMemoryPlugin from 'pouchdb-adapter-memory';
// ...
  if (config.emberPouch.options && config.emberPouch.options.adapter === "memory") {
      PouchDB.plugin(pouchInMemoryPlugin);
  }
  const db = new PouchDB(localDb, config.emberPouch.options);
// ...

Although switching to the in-memory DB seems to have made the first error disappear

Error: Attempted to handle event deleteRecord on <...> while in state root.loaded.updated.inFlight.

it has created a new one that is popping up all over the place now:

Error: Attempted to handle event becameError on <...> while in state root.loaded.saved.

How do I "make sure that each test will create a separate app"? (Perhaps that is why this is happening?) I am using ember-cli 3.5.0 and the qunit testing convention that it ships with by default.