mattphillips/jest-expect-message

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

AdamOakman opened this issue ยท 4 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 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>;
    }
}

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

gorn commented

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

Going to close as this can be solved with #26 (comment) or by following the Typescript setup outlined in the readme