Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

137
Vistas
Jest - Externalise extended expect matchers

I have. node.js-TypeScript applciation and Jest for testing. Using this reference https://jestjs.io/docs/expect#expectextendmatchers I have some extended expect matchers in my test classes. Exactly like the example below. I have a lot of common extends in several different test classes. Is there a way to externalise/group these extended matchers and use in the test classes by importing them?

Example:

expect.extend({
  async toBeDivisibleByExternalValue(received) {
    const externalValue = await getExternalValueFromRemoteSource();
    const pass = received % externalValue == 0;
    if (pass) {
      return {
        message: () =>
          `expected ${received} not to be divisible by ${externalValue}`,
        pass: true,
      };
    } else {
      return {
        message: () =>
          `expected ${received} to be divisible by ${externalValue}`,
        pass: false,
      };
    }
  },
});

test('is divisible by external value', async () => {
  await expect(100).toBeDivisibleByExternalValue();
  await expect(101).not.toBeDivisibleByExternalValue();
});

My jest.d.ts:

export {};
declare global {
  namespace jest {
    interface Matchers<R> {
      hasTestData(): R;
    }
}
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

For common extended expects I use the following logic;

ExtendedExpects.ts:

declare global {
    namespace jest {
        interface Matchers<R> {
            toBeDivisibleByExternalValue(): R;
        }
    }
}
export function toBeDivisibleByExternalValue(received:any): jest.CustomMatcherResult {
    const externalValue = await getExternalValueFromRemoteSource();
    const pass = received % externalValue == 0;
    if (pass) {
      return {
        message: () =>
          `expected ${received} not to be divisible by ${externalValue}`,
        pass: true,
      };
    } else {
      return {
        message: () =>
          `expected ${received} to be divisible by ${externalValue}`,
        pass: false,
      };
    }
}

You defined the common method, now how to consume it;

Your test class will look like,

import { toBeDivisibleByExternalValue } from "../ExtendedExpects";

expect.extend({
   toBeDivisibleByExternalValue
});

test('is divisible by external value', async () => {
  await expect(100).toBeDivisibleByExternalValue();
  await expect(101).not.toBeDivisibleByExternalValue();
});

You do not need jest.d.ts anymore.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda