question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do I stub "chrome-launcher" with sinon?

See original GitHub issue

Hello, 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:open
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
westy92commented, May 5, 2021

@simonespa I was able to use proxyquire to mock chrome-launcher: https://github.com/westy92/html-pdf-chrome/pull/297

0reactions
simonespacommented, May 5, 2021

Hi @westy92 @paulirish is this issue fixed then? Can we say it is now possible to mock the chrome-launcher module with sinon?

Thanks both 😄

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found