Possibility to pass custom-message via a callback function
mirismaili opened this issue · 3 comments
Feature Request
Description:
Thank useful tool. There should be a possibility to pass custom message as the output of a callback function, not just a plain (resolved) string
. Like expect.extend()
API of the main library:
message: () => `${received} not to be within range ${floor} - ${ceiling}`
This will be useful when our message needs to be produced in a result of some processes. The callback function will only be resolved in a fail state and thus those processes are done only in this special state (not a lot of times per each expect()
invocation).
Possible solution:
For example, this is my custom toMatch()
matcher that supports custom msg (in typescript):
expect.extend({
toMatch1(text: string, regExp: RegExp, msg: string | (() => string)) {
const match1 = regExp.exec(text)
const passed = match1 !== null
return passed ?
{
message: () => `Expected the text to not match ${regExp}\nFirst match:\n[${match1}]\n${typeof msg === 'string' ? msg : msg()}`,
pass: true,
} :
{
message: () => `Expected the text to match ${regExp}\n${typeof msg === 'string' ? msg : msg()}`,
pass: false,
}
},
})
As you can see the msg
parameter has been declared as string | (() => string)
and it has been used only in the bodies of message
callbacks, this way:
typeof msg === 'string' ? msg : msg()
So it supports both plain string (resolved string) and will-be-resolved string.
Oh! very larger problem!
When I just add setupFilesAfterEnv: ['jest-expect-message']
to jest's configurations (without any other change) I can see one of my tests (that has expect()
in a loop) runs ~45 times slower!
See it yourself:
1. Before adding setupFilesAfterEnv: ['jest-expect-message']
to jest/unit/config.js
:
> jest --verbose --colors --config jest/unit/config.js
PASS jest/unit/text-wrap.ts
Case-specific tests:
○ skipped Check input's hash
General tests [0]:
√ Try to find an illegal short line (45ms)
○ skipped Check num of markers
○ skipped Reproduce output using markers
○ skipped Try to find an illegal long line
Case-specific tests [0]:
○ skipped Check output's hash
General tests [1]:
√ Try to find an illegal short line (89ms)
○ skipped Check num of markers
○ skipped Reproduce output using markers
○ skipped Try to find an illegal long line
Case-specific tests [1]:
○ skipped Check output's hash
General tests [2]:
√ Try to find an illegal short line (89ms)
○ skipped Check num of markers
○ skipped Reproduce output using markers
○ skipped Try to find an illegal long line
Case-specific tests [2]:
○ skipped Check output's hash
General tests [3]:
√ Try to find an illegal short line (84ms)
○ skipped Check num of markers
○ skipped Reproduce output using markers
○ skipped Try to find an illegal long line
Case-specific tests [3]:
○ skipped Check output's hash
General tests [4]:
√ Try to find an illegal short line (94ms)
○ skipped Check num of markers
○ skipped Reproduce output using markers
○ skipped Try to find an illegal long line
Case-specific tests [4]:
○ skipped Check output's hash
General tests [5]:
√ Try to find an illegal short line (69ms)
○ skipped Check num of markers
○ skipped Reproduce output using markers
○ skipped Try to find an illegal long line
Case-specific tests [5]:
○ skipped Check output's hash
General tests [6]:
√ Try to find an illegal short line (60ms)
○ skipped Check num of markers
○ skipped Reproduce output using markers
○ skipped Try to find an illegal long line
Case-specific tests [6]:
○ skipped Check output's hash
General tests [7]:
√ Try to find an illegal short line
○ skipped Check num of markers
○ skipped Reproduce output using markers
○ skipped Try to find an illegal long line
Case-specific tests [7]:
○ skipped Check output's hash
Test Suites: 1 passed, 1 total
Tests: 33 skipped, 8 passed, 41 total
Snapshots: 0 total
Time: 3.508s, estimated 4s
Ran all test suites.
1. After adding setupFilesAfterEnv: ['jest-expect-message']
to jest/unit/config.js
:
> jest --verbose --colors --config jest/unit/config.js
PASS jest/unit/text-wrap.ts (24.599s)
Case-specific tests:
○ skipped Check input's hash
General tests [0]:
√ Try to find an illegal short line (1358ms)
○ skipped Check num of markers
○ skipped Reproduce output using markers
○ skipped Try to find an illegal long line
Case-specific tests [0]:
○ skipped Check output's hash
General tests [1]:
√ Try to find an illegal short line (3719ms)
○ skipped Check num of markers
○ skipped Reproduce output using markers
○ skipped Try to find an illegal long line
Case-specific tests [1]:
○ skipped Check output's hash
General tests [2]:
√ Try to find an illegal short line (3692ms)
○ skipped Check num of markers
○ skipped Reproduce output using markers
○ skipped Try to find an illegal long line
Case-specific tests [2]:
○ skipped Check output's hash
General tests [3]:
√ Try to find an illegal short line (3554ms)
○ skipped Check num of markers
○ skipped Reproduce output using markers
○ skipped Try to find an illegal long line
Case-specific tests [3]:
○ skipped Check output's hash
General tests [4]:
√ Try to find an illegal short line (3703ms)
○ skipped Check num of markers
○ skipped Reproduce output using markers
○ skipped Try to find an illegal long line
Case-specific tests [4]:
○ skipped Check output's hash
General tests [5]:
√ Try to find an illegal short line (3094ms)
○ skipped Check num of markers
○ skipped Reproduce output using markers
○ skipped Try to find an illegal long line
Case-specific tests [5]:
○ skipped Check output's hash
General tests [6]:
√ Try to find an illegal short line (2699ms)
○ skipped Check num of markers
○ skipped Reproduce output using markers
○ skipped Try to find an illegal long line
Case-specific tests [6]:
○ skipped Check output's hash
General tests [7]:
√ Try to find an illegal short line (9ms)
○ skipped Check num of markers
○ skipped Reproduce output using markers
○ skipped Try to find an illegal long line
Case-specific tests [7]:
○ skipped Check output's hash
Test Suites: 1 passed, 1 total
Tests: 33 skipped, 8 passed, 41 total
Snapshots: 0 total
Time: 24.759s
Ran all test suites.
I see the same problem to @mirismaili
with the property-based test, it's much worse.
I'm not sure I see the full value of supporting a custom message callback. Given clients of this library will be invoking expect from within their test, they will have the ability to customise the message (even if it is a resolved value).
Perhaps I'm missing something? I'm going to go ahead and close this issue for now but if you have a different example that will help me understand I'm happy to look at it 👍
Oh and on the performance issue, that should have been fixed in: https://www.npmjs.com/package/jest-expect-message/v/1.0.4