Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support setup via jest.config.js #22

Open
logi opened this issue Nov 27, 2020 · 3 comments
Open

Support setup via jest.config.js #22

logi opened this issue Nov 27, 2020 · 3 comments

Comments

@logi
Copy link

logi commented Nov 27, 2020

To avoid having to add this boilerplate to each test file that uses the library:

import {toBeDeepCloseTo,toMatchCloseTo} from 'jest-matcher-deep-close-to';
expect.extend({toBeDeepCloseTo, toMatchCloseTo});

the expect.extend line could instead be added at the bottom of index.js in which case the expectations can be made globally available in a user's jest.config.js with something like:

module.exports = {
    setupFilesAfterEnv: [
        'jest-extended',  // https://github.com/jest-community/jest-extended
        'jest-matcher-deep-close-to',  // https://github.com/maasencioh/jest-matcher-deep-close-to
        './jest-extensions',  // Our own extensions
    ],
}

I'd make a PR for this but I'm not set up for hacking on typescript code and this itch isn't annoying enough.

@jakeleventhal
Copy link

bump on this

@smith558
Copy link

smith558 commented Aug 4, 2022

Bump

@Antyos
Copy link

Antyos commented Sep 5, 2022

I got something working based on https://github.com/jest-community/jest-extended/blob/2bd088758d08a5ada82f28d26757f1d7f4ccaca4/src/all/index.js. Getting it to play nicely with my other VS Code extensions, XO, etc. was a bit of a pain.

Here's where I'm using it in a project: https://github.com/Antyos/image-layout/tree/adf3b4acde4482390d2e006db802a2c9eb7e965c.

// ./jest.config.ts
module.exports = {
    setupFilesAfterEnv: [
        'jest.config.ts', 
        // ...
    ],
}
// ./jest.setup.ts
import { toMatchCloseTo, toBeDeepCloseTo } from 'jest-matcher-deep-close-to';

// Patch `toMatchCloseTo` and `toBeDeepCloseTo` into jest's expect()
interface MaybeHasJestExpect {
    expect?: jest.Expect;
}

// Based on: https://github.com/jest-community/jest-extended/blob/2bd088758d08a5ada82f28d26757f1d7f4ccaca4/src/all/index.js
const jestExpect = (global as unknown as MaybeHasJestExpect).expect;

if (jestExpect === undefined) {
    throw new Error(
        "Unable to find Jest's global expect. " +
            'Please check you have added jest-extended correctly to your jest configuration. ' +
            'See https://github.com/jest-community/jest-extended#setup for help.',
    );
}

jestExpect.extend({ toMatchCloseTo, toBeDeepCloseTo });
// ./src/global.d.ts
export * from 'jest-matcher-deep-close-to';

In my tsconfig.json, I had to comment out the "exclude" key to avoid getting: Property 'toBeDeepCloseTo' does not exist on type 'JestMatchers<Position[]>'. ts(2339) in jest.setup.ts.

{
  // ...

  // Comment out the following line
  "exclude": ["src/**/*.test.*"],
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants