Unit testing segment integration

hi there guys, 


Really liking the tool and the integration. We were wondering if it's possible to launch analytics.js in a debug/testing mode and assert if calls to the api make it to the queue (but don't actually get fired) 


Consider this as an example 


// This should be part of your setup
ReactGA.initialize('foo', { testMode: true });
// This would be in the component/js you are testing
ReactGA.ga('send', 'pageview', '/mypage');
// This would be how you check that the calls are made correctly
  ['create', 'foo', 'auto'],
  ['send', 'pageview', '/mypage']
5replies Oldest first
  • Oldest first
  • Newest first
  • Active threads
  • Popular
  • Hi Jan,

    I'm not sure we have something exactly like the example you shared from the react-ga library but we do offer a ".debug()" method that includes helpful messages in the console for you to understand what is happening in the browser with our library.

    We don't have a way for you to assert if calls to our API make it to the queue if they don't get fired. We need to see those calls hit our API in order to validate if they reached the platform and were correctly processed in our ingestion pipeline. 

    You can always create an additional source for testing purposes if you don't want to corrupt the production/live data and replicate the exact same settings as your production source to send those tests and see how everything would work before implementing things in production.

    Hope this is helpful!



    Reply Upvote
  • Hi Emilio, 

    Thanks for your reply – yeah we're already using two sources (one for production, one for staging) to avoid the pollution. 

    But the point is to assert on mock/decoy API calls being made, not to actually make the calls ;-) 

    The .debug() maybe can work but will send API calls all the same right? How can I make asserts againt console statements? That's not what I want – and that's probably not what customers doing unit testing of this thing want. This is supposed to be a quick test "clicked a BLAH button -> will segment attempt to make an API call?"

    I'd like to request this feature, the closer to ReactGA (which seemed to have done it right) the better. 

    - Jan

    Reply Upvote
  • Totally understand your use case Jan.

    Unfortunately, we don't have an endpoint where you can mock those API calls. You'll need to send the call to us to actually validate if we received it and even in debug mode, you'd still send the call to us.

    If you're interested in this feature, can you please create a request here: https://segment.com/requests/integrations ? Our product team prioritizes what we build next based on the feedback we receive from our customers via this form.

    Hope this helps!



    Reply Upvote
  • Emilio Gomez many thanks – will file a request

    Reply Upvote
  • Emilio Gomez Any updates on this feature? I'm also looking to do the same -- create spies on the `analytics.track` method to see if it gets called (not if it actually hits Segment's API).


    Having difficulty loading the segment library in my testing framework scope (Jest).



    Reply Upvote