DEV Community

Cover image for 🧪 vscode test current file task
Jhon William
Jhon William

Posted on

🧪 vscode test current file task

While setting up a new project at Atlassian, I decided to dive into some vscodeworkspace settings to see how I could improve the developer experience. I found the task system to be pretty interesting and extensible, so I created a custom task to run tests against the currently open file.

Custom task definitions are placed in .vscode/tasks.json. Most of the fields are straightforward, but the command property has some interesting bits. There are all sorts of variables you can use to get information about the current file and directory you're in. You can also do things like run a task when a workspace is opened with the runOn property. Here's what my task definition looked like.

{
"version": "2.0.0",
"tasks": [
{
"label": "test current file",
"type": "shell",
"command": "npm run test
${fileBasenameNoExtension}.test${fileExtname}",
"group": "test",
"presentation": {
"clear": true,
"close": false,
"panel": "dedicated"
}
}
]
}

To run this task, open up the command palette by pressingCmd+P. Then select Tasks: Run Tasks to view a list of custom tasks. Finally, choose the test current file task from the list. If you ask me, this is a much better workflow than executing npm run test and then entering in the exact file path you're looking for.

Top comments (2)

Collapse
 
master_345 profile image
master

thank you for your sharing. this is useful for me.

Collapse
 
john_0519 profile image
Jhon William

thanks for your support. I'll try to submit more useful article. thanks.