Introduction
In this article, we will explore how to run the last touched test file in Visual Studio Code using TypeScript and Vitest. Vitest is a lightweight and fast test framework for TypeScript and JavaScript. This guide will help you understand the key concepts of running tests in a site-focused development environment.
Prerequisites
To follow along with this article, make sure you have the following:
- Visual Studio Code installed
- Node.js installed (minimum version 12.0.0)
- TypeScript installed
- Vitest installed
Setting up the project
Create a new directory for the project and initialize it with npm:
mkdir my-vitest-project
cd my-vitest-project
npm init -y
Next, install the necessary dependencies:
npm install vitest --save-dev
npx vitest init
npm install typescript --save-dev
Now, let's create a sample test file:
touch myTestFile.test.ts
Writing a test
Open Visual Studio Code and create the test file with the following content:
Running the tests
To run the test, open a terminal in Visual Studio Code, and type:
npx vitest
You will see test result output similar to the following:
Running the last touched test file
Assuming you have multiple test files and you only want to run the last touched test file, you can use the following launch configuration in your launch.json file (which can be found in the .vscode directory):
Creating the launch configuration
Open the launch.json file and add the following:
This configuration tells Vitest to run the current test file you have open.
To run the last touched test file with Visual Studio Code, TypeScript, and Vitest:
- Create or open a test file in Visual Studio Code
- Use the integrated terminal to run the test file with:
npx vitest - Create a launch configuration for Vitest to run the current file:
${file}