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']);
  rxSessionSpy.readTransaction.and.returnValue(results);
  rxSessionSpy.close.and.stub();
  driverSpy.rxSession.and.returnValue(rxSessionSpy);
  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.biville1!

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