Showing results for 
Search instead for 
Did you mean: 

Mocking neo4j driver in nodejs unit tests

Node Clone

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.

Not bad Thanks @florent.biville1!

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

Node Clone

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!

Nodes 2022
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.