jest-expect-message icon indicating copy to clipboard operation
jest-expect-message copied to clipboard

Using jest-expect-message outside unit tests (Cucumber tests) in TypeScript project

Open AdamOakman opened this issue 5 years ago • 3 comments

Hello, how can I configure this library to use expect assertions outside unit tests and describe and it blocks?

I have a typescript project where I use jest expect assertions inside cucumber tests just by importing it like below:

import expect from 'expect';

but it seems to be not working after simply adding your library to the project and produces an error:

features/step_definitions/Feature1.steps.ts:33:9 - error TS2554: Expected 1 arguments, but got 2.

Does it need any additional configuration apart from described in the project README or do I need to import it in the specific way?

AdamOakman avatar Jun 10 '20 15:06 AdamOakman

@AdamOakman Try putting this type declaration somewhere in your src or test folder:

// test/test.d.ts or something similar

declare namespace jest {
    interface Expect {
        /**
         * The `expect` function is used every time you want to test a value.
         * You will rarely call `expect` by itself.
         *
         * @param actual    The value to apply matchers against.
         * @param message   An optional custom error message to provide if the test fails.
         */
        <T = any>(actual: T, message?: string): JestMatchers<T>;
    }
}

DonIsaac avatar Jul 11 '20 21:07 DonIsaac

I can confirm that @DonIsaac's solution works. If there was a way to ship this by default, that would be even better.

otto-dev avatar Jan 04 '21 03:01 otto-dev

can be resolved by npm install --save-dev @types/jest-expect-message now

gorn avatar Mar 21 '21 21:03 gorn

Going to close as this can be solved with https://github.com/mattphillips/jest-expect-message/issues/26#issuecomment-803660589 or by following the Typescript setup outlined in the readme

mattphillips avatar Sep 08 '22 17:09 mattphillips