Closed phyllisstein closed 6 years ago
N.B. also that I flagged this with the Chai maintainers and they suggested that I take my whinge to you folks. Q.v. https://github.com/chaijs/chai/issues/1165.
I haven't checked, but I'd guess this is due to #2549
Oh wow, thank you so much for weighing in @SimenB! In the course of getting up to speed on the details of #2549, I wound up checking to see what would happen if I moved the environment bootstrapping code from a setupTestFrameworkScriptFile
script to a custom environment:
// spec/_should-environment.js
const browserEnv = require('browser-env')
const chai = require('chai')
const chaiDOM = require('chai-dom')
const JSDOMEnvironment = require('jest-environment-jsdom')
class ShouldEnvironment extends JSDOMEnvironment {
async setup() {
await super.setup()
browserEnv()
chai.use(chaiDOM)
this.global.should = chai.should()
}
}
module.exports = ShouldEnvironment
After pointing Jest's testEnvironment
option to this file and removing the setupTestFrameworkScriptFile
, all my should
assertions worked beautifully. (For whatever reason, it was still necessary to import chai/register-should
in setupFiles
.) Here's the complete changeset: https://github.com/phyllisstein/expect-should-repro-repo/commit/8571f66e7f82624eba6fcabf258fbddd52f5b0e1.
This resolves a greasy headache of long standing and frees me once and for all from Jasmine's clunky assertion style. Thank you for pointing me in the right direction.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.
🐛 Bug Report
Hey Jesters! I was recently attempting to integrate Chai's
should
assertion syntax into a Jest test suite, and discovered something a little odd. For some reason, tests that work fine in Mocha and in Jest withjest-mocha-runner
crash in Jest, with errors indicating that theshould
methods are no longer defined at various points in the language chain.I've seen the issue with built-in objects like
URL
......as well as custom constructors...
...and objects from libraries like JSDOM...
...but it doesn't affect all
should
assertions---some work just fine, and I haven't figured out the pattern yet.should
works by manipulatingObject.prototype
, so it seems likely to me that Jest's environment is making that difficult or unpredictable. However, I don't know Jest's internals well enough to guess what the conflict might be, and I was hoping you might have some insight.Thanks in advance for any thoughts you have! Let me know if I can provide any more color.
To Reproduce
Steps to reproduce the behavior:
should
helpers on the global object by importingchai/register-should
or callingchai.should()
.Expected behavior
I would expect that the
Object.prototype.should
global would remain defined through the entire test suite and allow chaining of its methods, as it does in the Mocha andjest-mocha-runner
environments.Link to repl or repo (highly encouraged)
https://github.com/phyllisstein/expect-should-repro-repo
Run
npx envinfo --preset jest