Comments (6)
Gentle nudge
If you're low on capacity but the feature is acceptable, I can try to implement it.
from testdouble.js.
@searls would you have an opinion on this?
from testdouble.js.
I don't really understand a "global mock" in this context, as I've never made a mock that survived a single test.
If for some reason you want that, I think I'd no longer call that a "test double" and instead a fake / alternate implementation for the sake of building a test harness. And if that's the case, I'd suggest dropping down to quibble to accomplish this.
of course, as soon as I think of this, quibble doesn't support this either because it just has a single process-wide reset()
function. But if one could set a context for quibbles of other modules, and took that context as an optional arguemnt in reset()
, then maybe something like this could be accomplished.
from testdouble.js.
A global mock would be something that is very common in an app but that has an adverse impact on testing. For example, I have an app that interacts with IndexedDB (a browser API); aside from the spec of that util, I never want that util to be real in the rest of my test suite, so I do something like:
$ node --import ./test/setup.mjs --test ./app/**/*.test.*
// test/setup
await td.replaceEsm('…/app/storage.mjs', {
createTransaction: mock_createTransaction,
storage: mock_storage,
});
Then in foo.test.mjs
, let's say it replaces bar.mjs
. At the end of foo.test.mjs
, bar.mjs
should be restored, but storage.mjs
should not.
I think the most flexible solution would be for td.reset() to optionally accept arguments for which modules to reset to the originals.
from testdouble.js.
Yeah, I hear you. I personally would rather folks used quibble directly for that, because the semantics of td.js are so organized around per-test setup and teardown (including its reset() implementation) that I think it'd be better if this lived in quibble.
That said, whether it's via named contexts like I mentioned or a list of modules to not reset (because accidentally letting a test-scoped mock survive across tests because it was missed would be bad), i'd be 👍 for a well-tested quibble PR that did this
from testdouble.js.
Hmmm, I think the ergonomics of the inverse would be rather tedious (both unexpected and not particularly what users want, as well as a bit more difficult to implement).
because accidentally letting a test-scoped mock survive across tests because it was missed would be bad
Yes 🙂 That's why I think having the default (no arguments provided) is right: unless the user chooses to do something quite specific, your concern is avoided.
And this way it's not a breaking change (td.reset()
current blows everything away).
I don't know this lib well enough to know the difference between putting this in quibble vs testdouble. Quibble exists as a dependency by proxy of testdouble, so it's not extra burden on users aside from being an extra thing to know (and I think most will not expect and thus won't know where to look).
from testdouble.js.
Related Issues (20)
- ES module replacement changed in Node 18.6 HOT 5
- Using testdouble esm loader with ts-node/esm loader on mocha leads to errors (Node 18.6.0) HOT 24
- function identity is different for modules that are not replaced with testdouble when testdouble is replacing other ESM modules HOT 10
- Importing a mocked ESM module dynamically will throw an error in node 16.17.0 HOT 3
- td.replace support for constructors? HOT 2
- td.replaceEsm() but mocking a global in that module only HOT 2
- Consider relaxing `thenReject` to accept types other than `Error` HOT 2
- How to call-through original function? HOT 1
- Incompatibility with node v20.0.0 HOT 2
- Add docs for using `testdouble` when we have to use another loader (ie `ts-node/esm`) HOT 8
- Where is changelog for 3.19.0 HOT 2
- Get list of mocked modules HOT 7
- replaceEsm behaviour HOT 1
- replaceEsm breaks in node20 when other loaders are used HOT 3
- `td.imitate` produces `undefined` for object keys of type `Accessor`
- `td.replaceEsm` does not work when specifier is resolved via loader HOT 1
- Feature request: `td.func().reset()` (reset spy/stub state)
- Suite test:esm fails under Node 21 HOT 2
- Module not found: Error: Can't resolve 'module' in 'node_modules/testdouble/lib'
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 testdouble.js.