Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

136
Visualizações
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 Respostas
Responde à pergunta

0

Removing automock: true fixed the problem.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda