Skip to content

Instantly share code, notes, and snippets.

@olegsych
Last active February 4, 2022 00:59
Show Gist options
  • Save olegsych/53ce3fe1c0386409cb56 to your computer and use it in GitHub Desktop.
Save olegsych/53ce3fe1c0386409cb56 to your computer and use it in GitHub Desktop.
Node + TypeScript
.vscode/
build/
typings/

Pre-requisites

npm install typescript -g
npm install typings -g
npm install mocha -g

Create Node & TypeScript project

npm init --force
typings install dt~node --global --save
typings install dt~mocha --global --save
  • Modify package.json using template in this gist to define npm build and test scripts.
  • Create tsconfig.json using template in this gist to allow running tsc to build all TypeScript files in the directory.
  • Create .gitignore from template in this gist to avoid storing intermediate files produced by build in Git repository.
  • Create .vscode/tasks.json using template in this gist to define VSCode build and test tasks.
  • Create .vscode/launch.json using template in this gist
  • Create src\product.ts using template in this gist
  • Create test\product.ts using template in this gist

Build

  • VSCode: Ctrl+Shift+B
  • Shell: tsc or npm run build

How it works:

  • npm runs the build script defined in package.json to invoke tsc command from the typescript module.
  • tsc looks for tsconfig.json in the current directory and uses
    • outDir property to make tsc generate .js files in the build subfolder,
    • sourceMaps to generate .js.map along with them.

Test

  • VSCode: Ctrl+Shift+P then Tasks: Run Test Task
  • Shell: mocha build/test or npm test

Run

node build/index.js

How it works:

  • tsc compiles index.ts to index.js in the build directory based on the tsconfig.json settings
  • node runs the JavaScript in index.js

Debug application

VS Code

  • Ctrl+Shift+D to open Debugger view
  • select Launch debugger configuration
  • F5

How it works:

  • .vsconfig/launch.json defines Launch configuration
    • program property to indicate which .ts file contains the application entry point,
    • sourceMaps property to make debugger use .js.map files,
    • outDir property to specify where to find them.

Debug tests

  • Shell
    • mocha build/test --debug-brk
  • VS Code
    • Ctrl+Shift+D to open Debugger view
    • select Attach debugger configuration
    • F5

How it works:

  • mocha tells node to stop as soon as it starts executing the test runner
  • Node starts listening on port 5858
  • VS Code Attach configuration supplies debugger settings
    • address tells debugger to connect to localhost where node is running
    • port tells debugger to connect to 5858 where node is listening
{
"version": "0.1.0",
"configurations": [
{
"name": "Launch",
"type": "node",
"program": "index.ts",
"runtimeArgs": ["--nolazy"],
"env": {
"NODE_ENV": "development"
},
"sourceMaps": true,
"outDir": "build"
},
{
"name": "Attach",
"type": "node",
"address": "localhost",
"port": 5858,
"sourceMaps": true,
"outDir": "build"
}
]
}
{
"scripts": {
"build": "tsc",
"test": "tsc && mocha build/test"
}
}
import assert = require("assert");
import product = require("../src/product");
describe('foo', () => {
describe('bar', () => {
it('returns baz', () => {
var sut = new product.foo();
assert.equal("baz", sut.bar());
});
});
});
export class foo {
bar(): string {
return "baz";
}
}
{
"version": "0.1.0",
"command": "npm",
"isShellCommand": true,
"tasks": [
{
"taskName": "build",
"showOutput": "always",
"problemMatcher": "$tsc",
"suppressTaskName": true,
"isBuildCommand": true,
"args": ["run", "build"]
},
{
"taskName": "test",
"showOutput": "always",
"isTestCommand": true
}
]
}
{
"compilerOptions": {
"module": "commonjs",
"outDir": "build",
"sourceMap": true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment