Mocking neo4j driver in nodejs unit tests

What is the recommended way to mock a database session in a node unit test? It needs to be very light weight to work with CI.

I'm currently writing a Graph App with Angular and the JS Driver.
I ended up writing this piece of code to mock the driver, session and transaction function:

const stubReactiveSession = <T>(results: Observable<T>): [jasmine.SpyObj<Driver>, jasmine.SpyObj<RxSession>] => {
  const driverSpy = jasmine.createSpyObj('driver', ['rxSession']);
  const rxSessionSpy = jasmine.createSpyObj('rxSession', ['readTransaction', 'close']);
  return [driverSpy, rxSessionSpy];

I use it as such:

import {Observable, of} from 'rxjs';
  let driverSpy: jasmine.SpyObj<Driver>;
  let rxSessionSpy: jasmine.SpyObj<RxSession>;
  [driverSpy, rxSessionSpy] = stubReactiveSession(of(new MyObject(...)));

That's more concise than I expected and works well in our case.

1 Like

Not bad :slight_smile: Thanks @florent_biville!

I suppose I could do a similar thing in ava using sinon...

I have just released a package on npm that makes mocking neo4j-driver really easy in node: neo-forgery.

You can (in your code with console statements or in the data browser) capture query results and then set up a mock Session object that you can use in testing.

@William_Lyon / @william.lyon and @michael_hunger please check it out!


I've published a tutorial for mocking a query.

1 Like