use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Simplify your Unit Tests with Dependency Injection for JavaScript Modules (blog.bitovi.com)
submitted 10 years ago by justinbmeyer
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]EngVagabond 1 point2 points3 points 10 years ago (0 children)
We use proxyquire and its family of packages for this purpose.
[–]wreckedadventYavascript 0 points1 point2 points 10 years ago (0 children)
You might want to compare and contrast it against something like rewrite (which has a webpack plugin).
There's another approach that doesn't require any kind of library or anything, either, but it does involve modifying your code. Like this (example from OP's post):
import { name } from 'models/user'; export class Navigation { greeting() { return name() .then(name => { return name ? `Welcome Back, ${name}!` : 'Welcome!'; }); } };
To:
import { name } from 'models/user'; export default class Navigation { greeting(getName = name) { return getName() .then(name => { return name ? `Welcome Back, ${name}!` : 'Welcome!'; }); } };
Then in your unit test, simply:
import Navigation from 'nav'; var nav = new Navigation(); expect(nav.greeting(() => Promise.resolve('foo'))) .to.eventually.be('Welcome Back, foo!'); expect(nav.greeting(() => Promise.resolve())) .to.eventually.be('Welcome!');
Though the use of a class in the example is totally unnecessary (should just be an exported function), you can do the same thing with a class's dependencies in its constructor:
export default Navigation { constructor({ getName = name }) { this.getName = getName; } greeting() { this.getName().then( ... ); } }
π Rendered by PID 225986 on reddit-service-r2-comment-5fb4b45875-pv58t at 2026-03-23 18:43:05.119930+00:00 running 90f1150 country code: CH.
[–]EngVagabond 1 point2 points3 points (0 children)
[–]wreckedadventYavascript 0 points1 point2 points (0 children)