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.

Mocking getters from babell-ed libraries

See original GitHub issue

I needed to mock storiesOf member of @storybook/react. It looked like a simple function member and all went to this code

import * as StorybookReact from '@storybook/react';
const baseStoriesOf = ImportMock.mockFunction(StorybookReact, 'storiesOf');

But it did not work. After long hours of digging, I’ve found out what was that. Storybook-guys use babel to compile all code. So when we do import ... from '@storybook/react' we actually import this ugly thing:

"use strict";

require("core-js/modules/es6.object.define-property");

Object.defineProperty(exports, "__esModule", {
  value: true
});
Object.defineProperty(exports, "storiesOf", {
  enumerable: true,
  get: function get() {
    return _preview.storiesOf;
  }
});
// and so on

And nothing can help ImportMock.mockFunction to replace [Getter].

Simplified example to reproduce the problem in gist.

Can we do something with that?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Vittlycommented, May 29, 2019

Unfortunately no 😦

In my case I’ve made it possible to replace external dependecy. Like

// my ts-module
import { CoolThing } from 'cool-external-thing';

export function f(arg1, arg2, replacerForThing = CoolThing) {
   // ...
}

And in tests I do simply pass sinon.stub() into replacerForThing-argument. So I had to change my own code to fix the issue

0reactions
mpetruniccommented, May 29, 2019

@Vittly Have you found out some alternative to mock babel-ed libraries like that?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mocking Static Methods - Spock Framework
A feature method must have at least one explicit (i.e. labelled) block ... When expecting a call to a getter method, Groovy property...
Read more >
equals · GitHub Topics · GitHub
Java testing framework for testing pojo methods. It tests equals, hashCode, toString, getters, setters, constructors and whatever you report in issues ;).
Read more >
Documentation - Ethers.js
The ethers.js library aims to be a complete and compact library for interacting with the Ethereum ... This is useful for calling getters...
Read more >
Metaprogramming
The propertyMissing(String) method is only called when no getter method for the given property can be found by the Groovy runtime. For setter ......
Read more >
(PDF) Mock objects for testing java systems - ResearchGate
expected. Listing 1shows an example usage of Mockito, one of the most popular mocking. libraries in Java (Mostafa and Wang 2014).
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