[unit test] How to properly mock element.appendChild in my unit test?

This page summarizes the projects mentioned and recommended in the original post on /r/learnjavascript

Our great sponsors
  • SurveyJS - Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • WorkOS - The modern identity platform for B2B SaaS
  • Sinon.JS

    Test spies, stubs and mocks for JavaScript.

  • Hi all. I have a method which creates an HTML element and calls the appendChild method. I've now written a unit test for this method, and want to use the Sinon package to mock the call to appendChlid and count how many times it was called. But when I do that, Sinon reports it was called zero times, instead of one. How do I use Sinon to properly mock (spy) appendChild in this case? My code is below.

  • SurveyJS

    Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App. With SurveyJS form UI libraries, you can build and style forms in a fully-integrated drag & drop form builder, render them in your JS app, and store form submission data in any backend, inc. PHP, ASP.NET Core, and Node.js.

    SurveyJS logo
NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts