Skip to content

Neo4j

Install

1
npm install @testcontainers/neo4j --save-dev

Examples

These examples use the following libraries:

Choose an image from the container registry and substitute IMAGE.

Create a node

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
await using container = await new Neo4jContainer(IMAGE).start();

const driver = neo4j.driver(
  container.getBoltUri(),
  neo4j.auth.basic(container.getUsername(), container.getPassword())
);
const session = driver.session();

const personName = "Chris";
const result = await session.run("CREATE (a:Person {name: $name}) RETURN a", { name: personName });
const singleRecord = result.records[0];
const node = singleRecord.get(0);
expect(node.properties.name).toBe(personName);

await session.close();
await driver.close();

With credentials

1
2
3
4
5
6
await using container = await new Neo4jContainer(IMAGE).withPassword("xyz1234@!").start();

const driver = neo4j.driver(
  container.getBoltUri(),
  neo4j.auth.basic(container.getUsername(), container.getPassword())
);

With APOC

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
await using container = await new Neo4jContainer(IMAGE).withApoc().withStartupTimeout(120_000).start();

const driver = neo4j.driver(
  container.getBoltUri(),
  neo4j.auth.basic(container.getUsername(), container.getPassword())
);
const session = driver.session();

const result = await session.run("CALL apoc.help('text')");
const singleRecord = result.records[0];
expect(singleRecord.length).toBeGreaterThan(0);

With plugins

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
await using container = await new Neo4jContainer(IMAGE)
  .withPlugins([Neo4jPlugin.APOC_EXTENDED, Neo4jPlugin.GRAPH_DATA_SCIENCE])
  .withStartupTimeout(120_000)
  .start();

const driver = neo4j.driver(
  container.getBoltUri(),
  neo4j.auth.basic(container.getUsername(), container.getPassword())
);

const session = driver.session();

// Monitor methods are only available in extended Apoc.
const result = await session.run("CALL apoc.monitor.ids()");
expect(result.records[0].get("nodeIds")).toEqual({ high: 0, low: 0 });

// Insert one node.
await session.run("CREATE (a:Person {name: $name}) RETURN a", { name: "Some dude" });

// Monitor result should reflect increase in data.
const result2 = await session.run("CALL apoc.monitor.ids()");
expect(result2.records[0].get("nodeIds")).toEqual({ high: 0, low: 1 });

await session.close();
await driver.close();