title: Calling Javascript functions from C# Unit Tests
body: <p>I am working on a small project that will primarily be a C# library. I also want to create a JavaScript counterpart that is functionally equivalent. I would like to be able to test both from the same unit tests.</p>
  <p>To be clear I am NOT going to be using this in a production environment where C# calls JS, or vice versa. I only want to blend C# and JavaScript in the context of the unit tests so I can verify and maintain cross-platform compatibility. Most examples I find show that this is possible within Blazor using JSRuntime, but I don't want to do this within a web application.</p>
  <p>Below is an example of what I am trying to accomplish.</p>
  <p>C# Method:</p>
  <pre><code>public class CsharpLibrary
  {
      public static bool CheckLength(string str) {
          if (str.Length &gt; 10)
              return false;
  
          return true;
      }
  }
  </code></pre>
  <p>JavaScript Function:</p>
  <pre><code>function CheckLength(str) {
      if (str.length &gt; 10)
          return false;
  
      return true;
  }
  </code></pre>
  <p>Unit Test</p>
  <pre><code>using Microsoft.VisualStudio.TestTools.UnitTesting;
  
  namespace CsharpJavascriptUnitTesting
  {
      [TestClass]
      public class UnitTest1
      {
          [TestMethod]
          public void CheckLength_TooLong()
          {
              var str = &quot;1234567890123456789&quot;;
  
              var isValid = CsharpLibrary.CheckLength(str);
              Assert.IsFalse(isValid);
  
              // TODO: is it possible to load the local Javascript file?
              // TODO: call JavaScript CheckLength(str);
              // TODO: assert that the value returned is also false
          }
      }
  }
  </code></pre>