Comments (4)
Thx, I'll continue with standalone package!
from esmock.
Another possible option - to add preprocessing magic, due to esmock loader has access to all files, including test files themself. It's much more tricky and not so clear, but theoretically can be used to injecting parent module url inside the calls, making something similar to Jest's 'mock' method, etc. Personally I don't like that way, because previously I have a lot of questions about Jest internals - 'my tests doesn't work as expected, plz help me, Master'.
from esmock.
an un-tested vanilla js function-sketch
import test, { mock } from 'node:test'
import assert from 'node:assert'
import esmock from 'esmock'
const esmockFactory = ((url = import.meta.url) => (moduleId, factdefs, factgdefs) => (
(defs, gdefs) => esmock(moduleId, url, {
...factdefs,
...Object.keys(defs).reduce((p, k) => ({
...k,
...(Object.isExtensible(factdefs[k]) && Object.isExtensible(defs[k]))
? ({ ...factdefs[k], ...defs[k] })
: defs[k]
}), factdefs || {})
}, {
...factgdefs,
...Object.keys(defs).reduce((p, k) => ({
...k,
...(Object.isExtensible(factgdefs[k]) && Object.isExtensible(defs[k]))
? ({ ...factgdefs[k], ...gdefs[k] })
: gdefs[k]
}), factgdefs || {})
})
))()
const esmockGetCountryJWT = esmockFactory('#src/get-country-from-jwt', {
'my-package-A': {
createJwt: () => ({
countryCode: 'US'
}),
},
'my-package-B': {
isCountry: () => true
}
})
await test("variant 1", async () => {
const isCountry = mock.fn(() => true)
const { getCountryFromJwt } = await esmockGetCountryJWT({
'my-package-B': {
isCountry: mock.fn(() => true)
}
})
isCountry.mock.mockImplementationOnce(() => true)
assert.deepEqual(getCountryFromJwt('token'), 'us')
})
await test("variant 2", async () => {
const { getCountryFromJwt } = await esmockGetCountryJWT({
'my-package-B': {
isCountry: () => false
}
})
assert.throws(() => getCountryFromJwt('token'), {
message: 'Invalid country us'
})
})
reusable factories could be returned from external package like eg "esmockfactory" below,
import test, { mock } from 'node:test'
import assert from 'node:assert'
import esmock from 'esmock'
import esmockfactory from 'esmockfactory'
const esmockfact = esmockfactory(esmock, import.meta.url)
const esmockGetCountryJWT = esmockfact('#src/get-country-from-jwt', {
'my-package-A': {
createJwt: () => ({
countryCode: 'US'
}),
},
'my-package-B': {
isCountry: () => true
}
})
await test("variant 1", async () => {
const isCountry = mock.fn(() => true)
const { getCountryFromJwt } = await esmockGetCountryJWT({
'my-package-B': {
isCountry
}
})
isCountry.mock.mockImplementationOnce(() => true)
assert.deepEqual(getCountryFromJwt('token'), 'us')
})
Imo factory-functionality should be created through an external package esmock wrapper. The reason is, factory-functionality does not require integration with esmock's loader behaviour and esmock is better (imo) if it provides only essential parts needed for mocking.
Also, the simplified area around these lines in the demo "esmockFactory" example would need to be more complicated ({ ...factdefs[k], ...defs[k] })
as this formula doesn't work in various scenarios mostly related to class instances and class properties
If an esmock-factory-creating package existed, that package could be mentioned in the README and wiki.
from esmock.
Decided to don't include such functionality into esmock package
from esmock.
Related Issues (20)
- Error: invalid moduleId when trying to stub a module dependency ES6 HOT 4
- What about Yarn PnP support? HOT 13
- Minified sources in esmock package HOT 4
- Invalid check in esmockIsLoader.js HOT 8
- Unable to mock the native node:zlib module with TypeScript and Mocha/Chai/Sinon. HOT 2
- Partial mocking not working as expected HOT 18
- Issues with pnpm HOT 8
- strictest silently allows to pass {} or undefined as defs -> real dependencies are used HOT 1
- Is it possible to mock `process.cwd()`? HOT 6
- Overriding `globalThis` per mock HOT 6
- Node 20.x: import.meta.resolve is not a promise anymore HOT 1
- Unexpected token 'delete' HOT 9
- Mock a package globally HOT 1
- `invalid moduleId` error when using tsx HOT 16
- Mock dependency HOT 8
- Is it possible to mock a binary's imports? HOT 6
- Mocking a file with a hashbang using the `import` key fails HOT 7
- Error when using `import` key with modules that have CJS imports HOT 9
- 2.3.4 - 'RangeError [ERR_UNKNOWN_MODULE_FORMAT]: Unknown module format: undefined' HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from esmock.