Skip to content

Custom matcher is not recognized (TS2339) on the running with ---no-cache #2303

Answered by mugifly
mugifly asked this question in Q&A
Discussion options

You must be logged in to vote

Here is the minimum code that worked:

src/index.spec.ts:

import "./matcher";

test("foo", async () => {
  expect("foo").toBeFoo();
});

src/global.d.ts:

declare namespace jest {
  interface Matchers<R> {
    toBeFoo(): R;
  }
}

src/matcher.ts:

import { expect } from "@jest/globals";

expect.extend({
  toBeFoo(received: string) {
    if (received == "foo") {
      return {
        message: () => "OK",
        pass: true,
      };
    } else {
      return {
        message: () => "NG",
        pass: false,
      };
    }
  },
});

package.json:

{
  "name": "tmp",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "test": "jest"
  },
  "jest": {
    "preset": "ts-jest"
  },
  "depe…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@mugifly
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by mugifly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants