Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jest.config.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export default {
transform: {},
silent: true
}
25 changes: 12 additions & 13 deletions lib/cds-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,22 +148,21 @@ class Test extends require('./axios') {
get chai() {
if (global.chai) return global.chai
const chai = require('chai')
chai.use (require('chai-subset'))
const chaip = require('chai-as-promised')
chai.use (chaip.default/*v8 on ESM*/ ?? chaip/*v7*/)
return chai
function require (mod) { try { return module.require(mod) } catch(e) {
if (e.code === 'MODULE_NOT_FOUND')
throw new Error (`Failed to load required package '${mod}'. Please add it thru:`
+ `\n npm add -D chai chai-as-promised chai-subset`, {cause: e})
else if (e.name === 'SyntaxError') // Jest stumbling over ESM
throw new Error (`Jest failed to load ESM package '${mod}'.`
+ `\nDowngrade '${mod}' to the major version before, or use a different test runner like 'node --test'.\n`, {cause: e})
else if (e.code === 'ERR_REQUIRE_ESM') // node --test on older Node versions
throw new Error (`Failed to load ESM package '${mod}'. This only is supported on Node.js >= 23.`
+ `\nUpgrade your Node.js installation or downgrade '${mod}' to the major version before.\n`, {cause: e})
else throw e
}}
// function require (mod) { try { return _require (mod) } catch(e) {
// if (e.code === 'MODULE_NOT_FOUND')
// throw new Error (`Failed to load required package '${mod}'. Please add it thru:`
// + `\n npm add -D chai chai-as-promised chai-subset`, {cause: e})
// // else if (e.name === 'SyntaxError') // Jest stumbling over ESM
// // throw new Error (`Jest failed to load ESM package '${mod}'.`
// // + `\nDowngrade '${mod}' to the major version before, or use a different test runner like 'node --test'.\n`, {cause: e})
// // else if (e.code === 'ERR_REQUIRE_ESM') // node --test on older Node versions
// // throw new Error (`Failed to load ESM package '${mod}'. This only is supported on Node.js >= 23.`
// // + `\nUpgrade your Node.js installation or downgrade '${mod}' to the major version before.\n`, {cause: e})
// else throw e
// }}
}
// set expect(x) { super.expect = x }
get expect() { return this.chai.expect }
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
},
"dependencies": {
"axios": "^1",
"chai": "^4.4.1",
"chai-as-promised": "^7.1.1",
"chai": "^6",
"chai-as-promised": "^8",
"chai-subset": "^1.6.0"
},
"peerDependencies": {
Expand Down
27 changes: 27 additions & 0 deletions test/chai.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// jest.unstable_shouldLoadAsEsm = ()=>false

// Requires jest to be run with NODE_OPTIONS="--experimental-vm-modules"
it.skip ('should load chai via dynamic import', async ()=>{
const chai = await import('chai')
console.log (chai)
chai.expect(chai).to.exist
expect(chai).toBeDefined()
})

// Supported by Node 24, but not by jest's monkey-patched require implementation
it.skip ('should load chai via createRequire', async ()=>{
const chai = require ('chai')
console.log (chai)
chai.expect(chai).to.exist
expect(chai).toBeDefined()
})

// Supported by Node 24, but not by jest's monkey-patched createRequire implementation
it.skip ('should load chai via createRequire', async ()=>{
const { createRequire } = require ('module')
require = createRequire (__filename)
const chai = require ('chai')
console.log (chai)
chai.expect(chai).to.exist
expect(chai).toBeDefined()
})
Loading