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

136
Views
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 answers
Answer question

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 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!