How do I stub "chrome-launcher" with sinon?
See original GitHub issueHello, thanks for the hard work on this library, it is really great.
I’ve got a module that exports the following function (among many others)
import puppeteer from 'puppeteer-core';
import * as ChromeLauncher from 'chrome-launcher';
import fetch from 'node-fetch';
export const launchBrowser = async (options = {}) =>
// Launches Chrome
const chrome = await ChromeLauncher.launch(options);
// Gets the port Chrome is running on
options.port = chrome.port;
// Connect puppeteer to the running Chrome instance
const response = await fetch(`http://localhost:${options.port}/json/version`);
const { webSocketDebuggerUrl } = await response.json();
return puppeteer.connect({ browserWSEndpoint: webSocketDebuggerUrl });
};
I’m using mocha
in conjunction with chai
and sinon
to test it. I’ve got the following test where I’m calling import * as ChromeLauncher from 'chrome-launcher'
before the module to test so that I can apply the stub correctly.
import sinon from 'sinon';
import * as ChromeLauncher from 'chrome-launcher';
import { launchBrowser } from '...';
describe('something', () => {
const sandbox = sinon.createSandbox();
afterEach(() => sandbox.restore());
it('should do something', async () => {
const chrome = {
port: '3434'
};
sandbox.stub(chromeLauncher, 'launch').resolves(chrome);
... ... ...
await launchBrowser(options);
sandbox.assert.calledOnceWithExactly(ChromeLauncher.launch, options);
});
});
As you can see, I’m using sandbox.stub(chromeLauncher, 'launch').resolves(chrome)
to decorate the launch
function and resolve with a fake chrome
object.
Unfortunately it doesn’t work, and it calls the real chrome-launcher
.
My question is, how can I stub the launch
function properly? Do you provide mocks ready to use?
Thanks, hope you’ve got all the details you need.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Sinon.stub works in karma, mocha with chrome running but ...
I am working on an angularjs project where if a user scrolls on an element, the element calls scrollTop() to determine if another...
Read more >Intermittent errors using sinon with Karma, RequireJS and multiple ...
I'm using RequireJS, and writing unit tests with Mocha, Chai and Sinon. Then using Karma to run the tests. ... sandbox.stub is not...
Read more >AngularJS Unit Tests with Sinon.JS - Object Computing, Inc.
It provides spies, stubs, and mocks for use in JavaScript unit tests. It works with any unit testing framework and has no external...
Read more >apps/unpacker - Git at Google
This is the ZIP Unpacker extension used in Chrome OS to support reading and unpacking of zip ... Chai for assertions, and Sinon...
Read more >How to stub a dependency of a module - Sinon.JS
Sinon is a stubbing library, not a module interception library. Stubbing dependencies is highly dependant on your environment and the implementation.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@simonespa I was able to use
proxyquire
to mockchrome-launcher
: https://github.com/westy92/html-pdf-chrome/pull/297Hi @westy92 @paulirish is this issue fixed then? Can we say it is now possible to mock the
chrome-launcher
module withsinon
?Thanks both 😄