Run Javascript In Visual Studio Code
To define our code as a JavaScript project, create jsconfig.json at the root of your JavaScript code as shown below. A JavaScript project is the source files of the project and should not include the derived or packaged files (such as a dist directory). In more complex projects, you may have more than one jsconfig.json file defined inside a workspace. You will want to do this so that the source code in one. You can write and run unit tests in Visual Studio using some of the more popular JavaScript frameworks without the need to switch to a command prompt.
- Typescript Vscode
- Codecademy
- Install Typescript In Vs Code
- Run React Js In Visual Studio Code
- How To Run Javascript In Visual Studio Code
You can write and run unit tests in Visual Studio using some of the more popularJavaScript frameworks without the need to switch to a command prompt. Both Node.js and ASP.NET Core projects are supported.
The supported frameworks are:
- Mocha (mochajs.org)
- Jasmine (Jasmine.github.io)
- Tape (github.com/substack/tape)
- Jest (jestjs.io)
- Export Runner (this framework is specific to Node.js Tools for Visual Studio)
For ASP.NET Core and JavaScript or TypeScript, see Write unit tests for ASP.NET Core.
Typescript Vscode
If your favorite framework is not supported, see Add support for a unit test framework for information on adding support.
Write unit tests in a Node.js project
Before adding unit tests to your project, make sure the framework you plan to use is installed locally in your project. This is easy to do using the npm package installation window.
The preferred way to add unit tests to your project is by creating a tests folder inyour project, and setting that as the test root in project properties. You also needto select the test framework you want to use.
You can add simple blank tests to your project, using the Add New Item dialog box. Both JavaScript and TypeScript are supported in the same project.
For a Mocha unit test, use the following code:
If you haven't set the unit test options in the project properties, you must ensure the Test Frameworkproperty in the Properties window is set to the correct test framework for your unit test files. This isdone automatically by the unit test file templates.
Note
The unit test options will take preference over the settings for individual files.
After opening Test Explorer (choose Test > Windows > Test Explorer), Visual Studio discovers and displays tests. If tests are not showing initially, then rebuild the project to refresh the list.
Note
For TypeScript, do not use the outdir
or outfile
option in tsconfig.json, because Test Explorer won't be able to find your unit tests.
Run tests (Node.js)
You can run tests in Visual Studio or from the command line.
Run tests in Visual Studio
You can run the tests by clicking the Run All link in Test Explorer. Or, you can run tests by selecting one or more tests or groups, right-clicking, and selecting Run from the shortcut menu. Tests run in the background, and Test Explorer automatically updates and shows the results. Furthermore, you can also debug selected tests by right-clicking and selecting Debug.
You can run the tests by clicking the Run All link in Test Explorer. Or, you can run tests by selecting one or more tests or groups, right-clicking, and selecting Run Selected Tests from the shortcut menu. Tests run in the background, and Test Explorer automatically updates and shows the results. Furthermore, you can also debug selected tests by selecting Debug Selected Tests.
For TypeScript, unit tests are run against the generated JavaScript code.
Note
In most TypeScript scenarios, you can debug a unit test by setting a breakpoint in TypeScript code, right-clicking a test in Test Explorer, and choosing Debug. In more complex scenarios, such as some scenarios that use source maps, you may have difficulty hitting breakpoints in TypeScript code. As a workaround, try using the debugger
keyword.
Note
We don't currently support profiling tests, or code coverage.
Run tests from the command line
You can run the tests from Developer Command Prompt for Visual Studio using the following command:
This command shows output similar to the following:
Note
If you get an error indicating that vstest.console.exe cannot be found, make sure you've opened the Developer Command Prompt and not a regular command prompt.
Write unit tests for ASP.NET Core
Create an ASP.NET Core project and add TypeScript support.
For an example project, see Create an ASP.NET Core app with TypeScript. For unit testing support, we recommend you start with a standard ASP.NET Core project template.
Use the NuGet package to add TypeScript support instead of the npm TypeScript package.
Install the NuGet package Microsoft.JavaScript.UnitTest
In Solution Explorer, right-click the project node and choose Unload Project.
The .csproj file should open in Visual Studio.
Add the following elements to the .csproj file in the
PropertyGroup
element.This example specifies Mocha as the test framework. You could specify Jest, Tape, or Jasmine instead.
The
JavaScriptTestRoot
element specifies that your unit tests will be in the tests folder of the project root.In Solution Explorer, right-click the project node and choose Reload Project.
Add npm support as described in the npm package management article under ASP.NET Core projects.
This requires installing the Node.js runtime for npm support and adding package.json in the project root.
In package.json, add the npm package you want under dependencies.
For example, for mocha, you might use the following:
Some unit testing frameworks, such as Jest, require additional npm packages. For Jest, use the following JSON:
Note
In some scenarios, Solution Explorer may not show the npm node due to a known issue described here. If you need to see the npm node, you can unload the project (right-click the project and choose Unload Project) and then reload the project to make the npm node re-appear.
Add code to test.
If you are using the example described in Create an ASP.NET Core app with TypeScript, add the following code at the end of the library.ts file, which is in the scripts folder.
For TypeScript, unit tests are run against the generated JavaScript code.
Add your unit tests to the tests folder in the project root.
For example, you might use the following code by selecting the correct documentation tab that matches your test framework, in this example either Mocha or Jest. This code tests a function called
getData
.Open Test Explorer (choose Test > Windows > Test Explorer) and Visual Studio discovers and displays tests. If tests are not showing initially, then rebuild the project to refresh the list.
Note
For TypeScript, do not use the
outfile
option in tsconfig.json, because Test Explorer won't be able to find your unit tests. You can use theoutdir
option, but make sure that configuration files such aspackage.json
andtsconfig.json
are in the project root.
Run tests (ASP.NET Core)
You can run the tests by clicking the Run All link in Test Explorer. Or, you can run tests by selecting one or more tests or groups, right-clicking, and selecting Run from the shortcut menu. Tests run in the background, and Test Explorer automatically updates and shows the results. Furthermore, you can also debug selected tests by right-clicking and selecting Debug.
You can run the tests by clicking the Run All link in Test Explorer. Or, you can run tests by selecting one or more tests or groups, right-clicking, and selecting Run Selected Tests from the shortcut menu. Tests run in the background, and Test Explorer automatically updates and shows the results. Furthermore, you can also debug selected tests by selecting Debug Selected Tests.
For TypeScript, unit tests are run against the generated JavaScript code.
Note
In most TypeScript scenarios, you can debug a unit test by setting a breakpoint in TypeScript code, right-clicking a test in Test Explorer, and choosing Debug. In more complex scenarios, such as some scenarios that use source maps, you may have difficulty hitting breakpoints in TypeScript code. As a workaround, try using the debugger
keyword.
Codecademy
Note
Install Typescript In Vs Code
We don't currently support profiling tests, or code coverage.
Add support for a unit test framework
You can add support for additional test frameworks by implementing the discovery and execution logic using JavaScript. You do this by adding a folder with the name of the test framework under:
<VisualStudioFolder>Common7IDEExtensionsMicrosoftNodeJsToolsTestAdapterTestFrameworks
This folder has to contain a JavaScript file with the same name which exports the following two functions:
find_tests
run_tests
For good a example of the find_tests
and the run_tests
implementations, see the implementation for the Mochaunit testing framework in:
<VisualStudioFolder>Common7IDEExtensionsMicrosoftNodeJsToolsTestAdapterTestFrameworksmochamocha.js
Discovery of available test frameworks occurs at Visual Studio start. If a framework is added whileVisual Studio is running, restart Visual Studio to detect the framework. However you don't need to restartwhen making changes to the implementation.
Unit tests in .NET Framework
You are not limited to writing unit tests in just your Node.js and ASP.NET Core projects. When you add the TestFramework andTestRoot properties to any C# or Visual Basic project, those tests will be enumerated and you can run them usingthe Test Explorer window.
Run React Js In Visual Studio Code
To enable this, right-click the project node in the Solution Explorer, choose Unload Project, and then choose Edit Project. Then in the project file, add the following two elements to a property group.
Important
Make sure that the property group you're adding the elements to doesn't have a condition specified.This can cause unexpected behavior.
Next, add your tests to the test root folder you specified, and they will be available to run in theTest Explorer window. If they don't initially appear, you may need to rebuild the project.
Unit test .NET Core and .NET Standard
In addition to the preceding properties, you also need to install the NuGet package Microsoft.JavaScript.UnitTest and set the property:
How To Run Javascript In Visual Studio Code
Some test frameworks may require additional npm packages for test detection. For example, jest requires the jest-editor-support npm package. If necessary, check the documentation for the specific framework.