Comments (2)
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.
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)
- Inaccuracies in tutorial HOT 3
- Inconsistency between github repo learnstorybook-code and Create addons chapter of storybook tutorial HOT 6
- Add assets error HOT 3
- Angular: Use service instead of ngxs to share state HOT 14
- document which Node.js and Yarn versions should be used HOT 1
- add prettier to devDependencies
- Add assets error in React tutorial HOT 3
- Angular tests fail HOT 1
- [Security] Potential Secret Leak HOT 2
- Missing components for React Native tutorial HOT 3
- How to add unit testing to this vuejs project? HOT 2
- Problems used in typescript HOT 3
- icon problem HOT 1
- app.component.spec fails from Angular 'Screens' chapter HOT 1
- Missing diff on pure-task-list.component HOT 1
- Interactions in Vue example not working correctly HOT 1
- Asterisk (*) issue in index.css
- which is the react native branch? HOT 1
- Docs InboxScreen shows only Error stories HOT 2
- yarn test, yarn start Not Work HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from learnstorybook-code.