33564

How to spy on a default exported function

Question:

sinon.spy takes 2 parameters, object and function name.

I have a module as listed below:

module.exports = function xyz() { }

How do I define a spy for xyz? I don't have object name to use.

Thoughts?

Answer1:

The above actually doesn't work if you're using the ES6 modules import functionality, If you are I've discovered you can actually spy on the defaults like so.

// your file export default function () {console.log('something here');} // your test import * as someFunction from './someFunction'; spyOn(someFunction, 'default')

As stated in <a href="http://2ality.com/2014/09/es6-modules-final.html#the-default-export-is-just-another-named-export" rel="nofollow">http://2ality.com/2014/09/es6-modules-final.html</a>

<blockquote>

The default export is actually just a named export with the special name default

</blockquote>

So the import * as someFunction gives you access to the whole module.exports object allowing you to spy on the default.

Recommend

  • F# multi-condition if/else versus matching
  • What is a shaded jar? And what is the difference/similarities between uber jar and shaded jar? [dupl
  • How to find the index of the minimum value between two specific columns of a matrix with Rcpp?
  • Since TranslateMessage() returns nonzero unconditionally, how can I tell, either before or after the
  • How does a JPA provider access (private) field values when no getter/setter method are defined?
  • I get a pg error when trying to destroy a controller
  • MySQL and average of multiple columns + indivudual condition for each of the columns
  • Rails 4: Using PostgreSQL function in order causes error in query due to the includes table not bein
  • inserting data into multiple tables using php via a web form
  • How to get value from Fields in redux-form?
  • Implicit resolution with covariance
  • Angular 4 + Universal : Has no exported member 'StaticProvider'
  • How to make html video autoplay on phones and tablets?
  • 30 tables with few rows - TRUNCATE the fastest way to empty them and reset attached sequences?
  • how to animate two continuous popup windows with jquery mobile?
  • Is there a modern ( e.g. CLR ) replacement for bison / yacc?
  • Encoding/decoding PDP-11 assembly language to binary and hex?
  • Select multiple fields with single group by in django
  • Android Studio generate signed apk not working on build 0.3.6 - Giving Error
  • Scrapy + Splash: scraping element inside inner html
  • Using Facebook Graph API with ASP.NET
  • Azure NodeJS Error: ENOENT, open 'D:\\home\\site\\wwwroot\\bin\\views\\'
  • Passing HTTP Post from AWS API GW to Lambda
  • Remove stopwords and tolower function slow on a Corpus in R
  • didSelectItemAtIndexPath of UICollectionView inside of a UIScrollView is not getting called
  • How to implement Google Drive and Google Plus sdk in ios project
  • MayAVI install on Python 3.6 [duplicate]
  • Query regarding com.jcraft.jsch.JSchException: UnknownHostKey: x.y.com. DSA key fingerprint is “ac:e
  • Optimization of optim() in R ( L-BFGS-B needs finite values of 'fn')
  • Query takes almost two seconds but matches only two rows - why isn't the index helping?
  • Magento-Change Attribute of All Products
  • How to create subsets of a single set of elements with XSLT?
  • how to run a different select statement based on condition in Hive SQL
  • Creating 2d platforms using JavaScript
  • How to write seo friendly url's using htaccess?
  • Is there a better way for handling SpatialPolygons that cross the antimeridian (date line)?
  • Conflicting declaration using constexpr and auto in C++11
  • Using Service Component Runtime
  • How to mutate multiple variables without repeating codes?
  • How to check if object is null in Java?