jest-extended icon indicating copy to clipboard operation
jest-extended copied to clipboard

Feature Request: extend the expect library

Open desm opened this issue 5 years ago • 6 comments

Feature Request

Description:

Would it be (have been?) possible to extend the 'expect' library instead? My use case is that I am using Jest from the command line (the "normal" way), but am also trying to use the 'expect' library directly from within my client-side code in order to make assertions. That way I only have 1 assertion library to learn.

desm avatar Aug 14 '18 18:08 desm

I'm not sure I fully understand the question of extend the expect library instead.

Jest allows it's expect to be extended via the expect.extend API, that is what jest-extended does inside of the entry point of the lib. When setting up jest-extended on the CLI you need to tell Jest you are using jest-extended, Jest allows you to do that in the setupTestFrameworkScript field of the config.

If you wanted to also use expect with jest-extended in a browser you would need to import jest-extended and call expect.extend yourself inside of the code that is to be bundled up for the browser.

Something like the following might be what you want (not sure exactly)

const matchers = require('jest-extended/dist/matchers');
const expect = require('expect');

expect.extend(matchers);

module.exports = expect;

mattphillips avatar Aug 14 '18 18:08 mattphillips

OMG I think that's exactly what I wanted! I'll give it a try and let you know. Thanks Matt!

desm avatar Aug 14 '18 20:08 desm

Hi @mattphillips , your suggestion works great! Thanks so much!

Can I do something similar with jest-chain?

desm avatar Aug 15 '18 02:08 desm

This worked for me:

import chain from 'jest-chain/dist/chain'
import matchers from 'jest-extended/dist/matchers'
import expect_ from 'expect'

expect_.extend(matchers)
const expect = chain(expect_)

So now I can do both chaining thanks to jest-chain, and use the more expressive tests from jest-extended. Great!

desm avatar Aug 15 '18 07:08 desm

A question would be, is it worth bringing chaining directly into jest-extended, to enable that functionality out the box when you bring in jest-extended?

I presume (my outlook) is that jest-extended gives jest a tonne of steroid injections, this would just be another direct one of those things.

benjaminkay93 avatar Sep 07 '18 13:09 benjaminkay93

Hi, sorry for the late reply. You can close this issue if you'd like.

desm avatar Feb 06 '24 18:02 desm