You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/javascript/unit-testing-javascript-with-visual-studio.md
+30-15Lines changed: 30 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: "Unit testing JavaScript and TypeScript"
3
3
description: Explore unit testing support in Visual Studio for JavaScript and TypeScript code by using the Node.js Tools for Visual Studio.
4
-
ms.date: "12/06/2024"
4
+
ms.date: "7/31/2025"
5
5
ms.topic: "how-to"
6
6
ms.devlang: javascript
7
7
author: "mikejo5000"
@@ -33,13 +33,13 @@ The supported frameworks are:
33
33
::: moniker-end
34
34
35
35
::: moniker range="<=vs-2019"
36
-
If your favorite framework is not supported, see [Add support for a unit test framework](#addingFramework) for information on adding support.
36
+
If your favorite framework isn't supported, see [Add support for a unit test framework](#addingFramework) for information on adding support.
37
37
::: moniker-end
38
38
39
39
::: moniker range=">=vs-2022"
40
40
## Write unit tests for a CLI-based project (.esproj)
41
41
42
-
The [CLI-based projects](../javascript/javascript-in-vs-2022.md#project-templates) supported in Visual Studio 2022 work with Test Explorer. Vitest is the built-in test framework for React and Vue projects (previously Jest), and Karma and Jasmine is used for Angular projects. By default, you will be able to run the default tests provided by each framework, as well as any additional tests you write. Just hit the **Run** button in Test Explorer. If you don’t already have Test Explorer open, you can find it by selecting **Test** > **Test Explorer** in the menu bar.
42
+
The [CLI-based projects](../javascript/javascript-in-vs-2022.md#project-templates) supported in Visual Studio 2022 work with Test Explorer. Vitest is the built-in test framework for React and Vue projects (previously Jest), and Karma and Jasmine is used for Angular projects. By default, you'll be able to run the default tests provided by each framework, as well as any additional tests you write. Just hit the **Run** button in Test Explorer. If you don’t already have Test Explorer open, you can find it by selecting **Test** > **Test Explorer** in the menu bar.
43
43
44
44
To run unit tests from the command-line, right-click the project in Solution Explorer, choose **Open in Terminal**, and run the command specific to the test type.
45
45
@@ -77,7 +77,7 @@ The following example is based on the TypeScript React project template provided
77
77
78
78
- vitest
79
79
80
-
This package are added to the *package.json* file under dependencies.
80
+
This package is added to the *package.json* file under dependencies.
81
81
82
82
> [!NOTE]
83
83
> If you're using jest, the jest-editor-support npm package is required as well as the jest package.
@@ -107,12 +107,15 @@ The following example is based on the TypeScript React project template provided
107
107
});
108
108
```
109
109
110
-
1. Open Test Explorer (choose **Test** > **Test Explorer**) and Visual Studio discovers and displays tests. If tests are not showing initially, then rebuild the project to refresh the list.
110
+
1. Open Test Explorer (choose **Test** > **Test Explorer**) and Visual Studio discovers and displays tests. If tests aren't showing initially, then rebuild the project to refresh the list.
111
111
112
112

113
113
114
114
> [!NOTE]
115
-
> 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 the `outdir` option, but make sure that configuration files such as `package.json` and `tsconfig.json` are in the project root.
115
+
> For TypeScript, don't use the `outfile` option in *tsconfig.json*, because Test Explorer won't be able to find your unit tests. You can use the `outdir` option, but make sure that configuration files such as `package.json` and `tsconfig.json` are in the project root.
116
+
117
+
> [!IMPORTANT]
118
+
> If the output from **Tests** in the Output window shows a `ReadOnlySpan` error during test discovery, use this workaround for a known [MSBuild issue](https://github.com/dotnet/msbuild/issues/7873). Open the folder, *Program Files\Microsoft Visual Studio\2022\\<version\>\Common7\IDE\Extensions\TestPlatform*, and rename *System.Memory.dll* to a different name. This fix enables test discovery.
116
119
117
120
### Run tests (.esproj)
118
121
@@ -128,13 +131,13 @@ For some unit test frameworks, unit tests are typically run against the generate
128
131
> 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.
129
132
130
133
> [!NOTE]
131
-
> Profiling tests and code coverage are not currently supported.
134
+
> Profiling tests and code coverage aren't currently supported.
132
135
133
136
::: moniker-end
134
137
135
138
## Write unit tests for ASP.NET Core
136
139
137
-
To add support for unit testing of JavaScript and TypeScript in an ASP.NET Core project, you need to add TypeScript, Npm, and unit testing support to the project by including required NuGet packages.
140
+
To add support for unit testing of JavaScript and TypeScript in an ASP.NET Core project, you need to add TypeScript, npm, and unit testing support to the project by including required NuGet packages.
138
141
139
142
### Add a unit test (ASP.NET Core)
140
143
@@ -148,9 +151,16 @@ The following example is based on the ASP.NET Core Model-View-Controller project
148
151
149
152
1. In the **Browse** tab, search for the following packages and install each one:
Use the NuGet package to add TypeScript support instead of the npm TypeScript package.
156
166
@@ -177,7 +187,7 @@ The following example is based on the ASP.NET Core Model-View-Controller project
177
187
178
188
# [Mocha](#tab/mocha)
179
189
180
-
The `JavaScriptTestRoot` element specifies the location for your unit tests. To simplify the configuration for Mocha, in this example we point the test root to the output folder, *wwwroot\js\tests\*.
190
+
The `JavaScriptTestRoot` element specifies the location for your unit tests. To simplify the configuration for Mocha, in this example we point the test root to the output folder, *wwwroot\js\tests\\*.
181
191
182
192
```xml
183
193
<PropertyGroup>
@@ -337,12 +347,17 @@ The following example is based on the ASP.NET Core Model-View-Controller project
337
347
})
338
348
```
339
349
340
-
1. 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. The following illustration shows the Jest example, with two different unit test files.
350
+
1. Open Test Explorer (choose **Test** > **Windows** > **Test Explorer**) and Visual Studio discovers and displays tests. If tests aren't showing initially, then rebuild the project to refresh the list. The following illustration shows the Jest example, with two different unit test files.
341
351
342
352

343
353
344
354
> [!NOTE]
345
-
> 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 the `outdir` option, but make sure that configuration files such as `package.json` and `tsconfig.json` are in the project root.
355
+
> For TypeScript, don't use the `outfile` option in *tsconfig.json*, because Test Explorer won't be able to find your unit tests. You can use the `outdir` option, but make sure that configuration files such as `package.json` and `tsconfig.json` are in the project root.
356
+
357
+
::: moniker range="vs-2022"
358
+
> [!IMPORTANT]
359
+
> If the output from **Tests** in the Output window shows a `ReadOnlySpan` error during test discovery, use this workaround for a known [MSBuild issue](https://github.com/dotnet/msbuild/issues/7873). Open the folder, *Program Files\Microsoft Visual Studio\2022\\<version\>\Common7\IDE\Extensions\TestPlatform*, and rename *System.Memory.dll* to a different name. This fix enables test discovery.
360
+
::: moniker-end
346
361
347
362
### Run tests (ASP.NET Core)
348
363
@@ -358,7 +373,7 @@ For some unit test frameworks, unit tests are typically run against the generate
358
373
> 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.
359
374
360
375
> [!NOTE]
361
-
> Profiling tests and code coverage are not currently supported.
376
+
> Profiling tests and code coverage aren't currently supported.
362
377
363
378
::: moniker range="<=vs-2019"
364
379
## <aname="addingFramework"></a>Add support for a unit test framework
@@ -387,7 +402,7 @@ Discovery of available test frameworks occurs at Visual Studio start. If a frame
387
402
388
403
## Unit tests in .NET Framework
389
404
390
-
You are not limited to writing unit tests in just your Node.js and ASP.NET Core projects. When you add the TestFramework and TestRoot properties to any C# or Visual Basic project, those tests will be enumerated and you can run them using the Test Explorer window.
405
+
You aren't limited to writing unit tests in just your Node.js and ASP.NET Core projects. When you add the TestFramework and TestRoot properties to any C# or Visual Basic project, those tests will be enumerated and you can run them using the Test Explorer window.
391
406
392
407
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.
393
408
@@ -455,12 +470,12 @@ property in the **Properties** window is set to the correct test framework for y
455
470
> [!NOTE]
456
471
> The unit test options will take preference over the settings for individual files.
457
472
458
-
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.
473
+
After opening Test Explorer (choose **Test** > **Windows** > **Test Explorer**), Visual Studio discovers and displays tests. If tests aren't showing initially, then rebuild the project to refresh the list.
459
474
460
475

461
476
462
477
> [!NOTE]
463
-
> 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.
478
+
> For TypeScript, don't use the `outdir` or `outfile` option in *tsconfig.json*, because Test Explorer won't be able to find your unit tests.
0 commit comments