<p>I'd like to execute JavaScript code from within a C# assembly and have the results of the JavaScript code returned to the calling C# code.</p>

<p>It's easier to define things that I'm not trying to do:</p>

<ul>
<li><p>I'm not trying to call a JavaScript function on a web page from my code behind.</p></li>
<li><p>I'm not trying to load a WebBrowser control.</p></li>
<li><p>I don't want to have the JavaScript perform an AJAX call to a server.</p></li>
</ul>

<p>What I want to do is write unit tests in JavaScript and have then unit tests output JSON, even plain text would be fine. Then I want to have a generic C# class/executible that can load the file containing the JS, run the JS unit tests, scrap/load the results, and return a pass/fail with details during a post-build task.</p>

<p>I think it's possible using the old ActiveX ScriptControl, but it seems like there ought to be a .NET way to do this without using SilverLight, the DLR, or anything else that hasn't shipped yet. Anyone have any ideas?</p>

<p>update: <a href="http://blogs.msdn.com/brada/articles/239857.aspx" rel="nofollow noreferrer">From Brad Abrams blog</a></p>

<pre><code>namespace Microsoft.JScript.Vsa
{
    [Obsolete("There is no replacement for this feature. " +
              "Please see the ICodeCompiler documentation for additional help. " +
              "http://go.microsoft.com/fwlink/?linkid=14202")]
</code></pre>

<p>Clarification:
We have unit tests for our JavaScript functions that are written in JavaScript using the JSUnit framework. Right now during our build process, we have to manually load a web page and click a button to ensure that all of the JavaScript unit tests pass. I'd like to be able to execute the tests during the post-build process when our automated C# unit tests are run and report the success/failure alongside of out C# unit tests and use them as an indicator as to whether or not the build is broken.</p>

[{'answer_id': 67755, 'author': 'Gulzar Nazim', 'author_id': 4337, 'author_profile': 'https://Stackoverflow.com/users/4337', 'pm_score': 2, 'selected': False, 'text': '<p>You can use the Microsoft Javascript engine for <a href="http://www.west-wind.com/WebLog/posts/10688.aspx" rel="nofollow noreferrer">evaluating JavaScript code from C#</a></p>\n\n<p>Update: This is obsolete as of VS 2008</p>\n'}, {'answer_id': 67768, 'author': 'Sam Wessel', 'author_id': 4734, 'author_profile': 'https://Stackoverflow.com/users/4734', 'pm_score': 0, 'selected': False, 'text': '<p>Could it be simpler to use <a href="https://github.com/pivotal/jsunit" rel="nofollow noreferrer">JSUnit</a> to write your tests, and then use a <a href="http://watin.sourceforge.net/" rel="nofollow noreferrer">WatiN</a>\n test wrapper to run them through C#, passing or failing based on the JSUnit results?</p>\n\n<p>It is indeed an extra step though.</p>\n\n<p>I believe I read somewhere that an upcoming version of either MBUnit or WatiN will have the functionality built in to process JSUnit test fixtures. If only I could remember where I read that...</p>\n'}, {'answer_id': 67772, 'author': 'tomasr', 'author_id': 10292, 'author_profile': 'https://Stackoverflow.com/users/10292', 'pm_score': 0, 'selected': False, 'text': "<p>I don't know of any .NET specific way of doing this right now... Well, there's still JScript.NET, but that probably won't be compatible with whatever JS you need to execute :)</p>\n\n<p>Obviously the future would be the .NET JScript implementation for the DLR which is coming... someday (hopefully).</p>\n\n<p>So that probably leaves running the old ActiveX JScript engine, which is certainly possible to do so from .NET (I've done it in the past, though it's a bit on the ugly side!).</p>\n"}, {'answer_id': 67786, 'author': 'scubabbl', 'author_id': 9450, 'author_profile': 'https://Stackoverflow.com/users/9450', 'pm_score': 3, 'selected': False, 'text': '<p>The code should be pretty self explanitory, so I\'ll just post that.</p>\n\n<pre><code>&lt;add assembly="Microsoft.Vsa, Version=8.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/&gt;&lt;/assemblies&gt;\n</code></pre>\n\n<hr>\n\n<pre><code>using Microsoft.JScript;\n\npublic class MyClass {\n\n    public static Microsoft.JScript.Vsa.VsaEngine Engine = Microsoft.JScript.Vsa.VsaEngine.CreateEngine();\n\n    public static object EvaluateScript(string script)\n    {\n        object Result = null;\n        try\n        {\n            Result = Microsoft.JScript.Eval.JScriptEvaluate(JScript, Engine);\n        }\n        catch (Exception ex)\n        {\n            return ex.Message;\n        }\n\n        return Result;\n    }\n\n    public void MyMethod() {\n        string myscript = ...;\n        object myresult = EvaluateScript(myscript);\n    }\n}\n</code></pre>\n'}, {'answer_id': 67906, 'author': 'Jon Galloway', 'author_id': 5, 'author_profile': 'https://Stackoverflow.com/users/5', 'pm_score': 0, 'selected': False, 'text': "<p>If you're not executing the code in the context of a browser, why do the tests need to be written in Javascript? It's hard to understand the bigger picture of what you're trying to accomplish here.</p>\n"}, {'answer_id': 69224, 'author': 'user10917', 'author_id': 10917, 'author_profile': 'https://Stackoverflow.com/users/10917', 'pm_score': 2, 'selected': True, 'text': "<p>You can run your JSUnit from inside Nant using the JSUnit server, it's written in java and there is not a Nant task but you can run it from the command prompt, the results are logged as XML and you can them integrate them with your build report process.\nThis won't be part of your Nunit result but an extra report.\nWe fail the build if any of those test fails.\nWe are doing exactly that using CC.Net.</p>\n"}]
['https://Stackoverflow.com/questions/67734', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1538/']