Skip to content

Record type issue #632

Closed
Closed
@AdrienLemaire

Description

@AdrienLemaire

Getting this error when using Record in my unit tests

[tsserver 2351] [E] This expression is not constructable.
Type 'Record' has no construct signatures.

Driver version: JS driver 4.1.2

Steps to reproduce

driver.ts

import neo4j, { Driver } from "neo4j-driver";
import environment from "./environment";

const driver: Driver = neo4j.driver(
  environment.neo4j.uri,
  neo4j.auth.basic(environment.neo4j.user, environment.neo4j.password)
);
driver.verifyConnectivity().then((param) => console.dir(param));

export default driver;

simplified User.ts

const generateUserModel = (user: JwtUser): TUserModel => ({
  canUpdateRoleFor: async ({ id, role }) => {
    const session = driver.session();
    try {
      const result = await session.run(
        "MATCH (u:User {id: $id}) RETURN u.role",
        { id }
      );
      const currentRole: Role = result.records[0].get(0);
      return currentRole
    } finally {
      await session.close();
    }

User.spec.ts

import { types } from "neo4j-driver";
const { Record } = types; // Strange, but I can't import Record directly from neo4j-driver

jest.mock("../driver", () => {
  // Mock driver.session() used in canUpdateRoleFor
  return {
    __esModule: true,
    default: {
      session: jest.fn(() => ({
        run: jest.fn((_cypher, { id }) => {
          return {
            records: [new Record(["role"], [SortedRoles[id]])], // <= LINE RAISING THE TYPE ERROR
          };
        }),
        close: jest.fn(),
      })),
    },
  };
});

Expected behavior

No type error

Actual behavior

I verified that the implementation is correct and my tests pass as expected.

Also, I cannot find the reason for this error in the code. When jumping to the type definition, I can see a constructor definition.

neo4j-driver/types/record.d.ts

declare class Record {
  keys: string[]
  length: number

  constructor(
    keys: string[],
    fields: any[],
    fieldLookup?: { [index: string]: string }
  )

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions