Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

122
Views
Jest instantiating empty objects instead of class instances

I'm using ts-jest to test a JS/TS SDK/module. I'm running into a strange issue where a Jest test will run (no compile/import failures) but fail to correctly instantiate an object from the appropriate class.

test("Should build unit", () => {
  const builder = new UnitBuilder("TEST_UNIT");
  console.log(builder); // prints "{}"
  const unit = builder
    .addResource(...)
    .build();
  expect(unit.name).toBe("TEST_UNIT");
});

The test fails with: TypeError: builder.addResource is not a function since the instantiated object is empty. Here's the class in question:

export class UnitBuilder {
  constructor(templateId: string) {
    this.payload = {
      templateId,
      parameters: [],
    };
  }

  public addResource = (resource: CreateResourcePayload) => {
      // do stuff
  };

  public build = () => {
      // do stuff
  };

  public payload: CreateUnitPayload;
}

I'm assuming this has something to do with the jest or ts-jest transpilation, e.g. babel, but perhaps it's something to do with the jest configuration as well?

jest.config.ts

import type { Config } from "@jest/types";

const config: Config.InitialOptions = {
  preset: "ts-jest",
  testEnvironment: "node",
  moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json"],
  verbose: true,
  automock: true,
  testMatch: ["**/__tests/*.test.ts"],
  roots: ["<rootDir>/src"],
  transform: {
    "^.+\\.(ts|tsx)$": "ts-jest",
  },
};

export default config;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Removing automock: true fixed the problem.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!