Code Monkey home page Code Monkey logo

Comments (2)

tmeasday avatar tmeasday commented on September 4, 2024

Hey @dmngu9.

One option is to mock out the library you are using a require-time mock. This is sort of brittle though.

A better option is to have your B component take its async library from the context, e.g. this.context.axios. You can make it default to requiring axios if the context is unset if you want it to be easily consumable. Then you can use a mocked axois provider when testing.

Does anyone else have any other ways to do it?

from learnstorybook-code.

kennethchoe avatar kennethchoe commented on September 4, 2024

I had similar situation recently on my mocha test. I used sinon with its sandbox.

Basic idea: the tester (or story in your case) will import the component, and also its referenced component (like axios in your example) with import * as targetsReference from 'target-s-reference', and then stub it.
https://railsware.com/blog/mocking-es6-module-import-without-dependency-injection/

Without sandbox, this injection stays and influences other tests. So make sure you also use stub with sandbox.
https://sinonjs.org/releases/v1.17.7/sandbox/
I used sandbox on before() and after(), but since you are doing this for storybook, you could do the same on lifecycle hooks.

I happened to deal with ES6 style (export) and ES5 style (module.exports) mixed, and that also made things tricky. This link was most helpful.
https://kentcdodds.com/blog/misunderstanding-es6-modules-upgrading-babel-tears-and-a-solution

I hope it helps.

from learnstorybook-code.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.