0

In the implementation of repositories, you inject the PersistenceManager interface using @InjectPersistenceManager(). For testing, the docs mention to use RunWithDrivine, and to import Drivine (through the AppModule) into the createTestingModule call. This allows the PersistenceManager to be injected in the repository to be used.

I create a temporary Docker container with a new Neo4j database for the tests to use (using a package called testcontainers). This database needs some data to be used. In the test setup, it is possible to get the PersistenceManager by retrieving it from the testing module using a string which normally the decorator provides: app.get("PersistenceManager:default") as PersistenceManager and while this works, it does not seem like the correct way to do it.
How would I get the PersistenceManager to set up the data (and do other things) properly?

Nils
  • 520
  • 7
  • 15

1 Answers1

1

app.get("PersistenceManager:default") as PersistenceManager

This is actually probably what you're looking to do, but need to add { strict: false } as a second parameter to the get method. Seeing as @InjectPersistenceManager() is just @Inject('PersistenceManager:database') and database is default by default, you have the correct token, so you probably just need to tell Nest to dig deeper than the top module, hence the{ strict: false } option

Jay McDoniel
  • 57,339
  • 7
  • 135
  • 147
  • I probably should have specified :) This is working, as in it injects correctly and I can run queries. However what happens now if I want to have different databases? Or Drivine changes its implementation? I would need to change every single test file. There is a reason that Drivine uses the custom injection decorator. – Nils Jan 25 '23 at 20:50
  • Well, _generally_ when a library uses a custom injection decorator they hide the string behind a getter function so that clients can just use that getter. Like nest has `getRepositoryToken()`, even if the full string changes, the method won't and it will handle it for you. That should be taken up with the library author though and isn't something Nest could make a constraint of in the first place – Jay McDoniel Jan 25 '23 at 21:05
  • I suppose I will go on the hunt for a getter from Drivine itself, or make one. Thank you – Nils Jan 25 '23 at 21:16
  • 1
    They don't have one. A PR could be helpful for them – Jay McDoniel Jan 25 '23 at 21:17