Project Setup
Preface
This tutorial runs you through the steps required to set up your project.
Create Project
We will create a new project first. Fire up a command shell (bash on Linux or Git Bash on Windows) and then run the following commands.
In our examples we will be using bash on both Linux and MacOS and Git Bash on Windows.
If you are using a different shell, you might have to adjust the provided scripts so that they will work in your environment.
- Linux
- MacOS
- Windows
mkdir my-package
cd my-package
mkdir my-package
cd my-package
mkdir my-package
cd my-package
Initialize Project
Now, we will initialize our project. Just follow the instructions when running the below commands.
The command will create a package.json file based on the provided information.
- npm
- yarn
npm init
OUTPUT
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See `npm help init` for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (my-package)
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to [.../]my-package/package.json:
{
"name": "my-package",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Is this OK? (yes)
yarn init
OUTPUT
yarn init <yarn-version-id>
question name (my-package):
question version (1.0.0):
question description:
question entry point (index.js):
question repository url:
question author:
question license (MIT):
question private:
success Saved package.json
Done in ...s.
Initialize Git Repository
git init
Install Development Dependencies
Having initialized the project, we will install the dependencies used during development.
Install TypeScript
First, we will install TypeScript as a development dependency.
Even if you installed TypeScript globally as part of the optional prerequisites, you still have to install it as a development dependency. Otherwise, the package scripts we will add to package.json will not work on other systems.
- npm
- yarn
npm install --save-dev typescript@latest
yarn add --dev typescript@latest
Install reflect-metadata
- npm
- yarn
npm install --save-dev reflect-metadata@latest
yarn add --dev reflect-metadata@latest
Install Testdeck and Test Framework
Next, we will install Testdeck and the required test framework dependencies.
- Mocha
- Jest
- Vitest
- Jasmine
- npm
- yarn
npm install --save-dev @testdeck/mocha@0.3
npm install --save-dev @types/mocha@latest
npm install --save-dev mocha@latest
yarn add --dev @testdeck/mocha@0.3
yarn add --dev @types/mocha@latest
yarn add --dev mocha@latest
- npm
- yarn
npm install --save-dev @testdeck/jest@0.3
npm install --save-dev @types/jest@latest
npm install --save-dev jest@latest
yarn add --dev @testdeck/jest@0.3
yarn add --dev @types/jest@latest
yarn add --dev jest@latest
- npm
- yarn
npm install --save-dev @testdeck/vitest@0.3
npm install --save-dev vitest@latest
npm install --save-dev vite@latest
yarn add --dev @testdeck/vitest@0.3
yarn add --dev vitest@latest
yarn add --dev vite@latest
- npm
- yarn
npm install --save-dev @testdeck/jasmine@0.3
npm install --save-dev @types/jasmine@latest
npm install --save-dev jasmine@latest
yarn add --dev @testdeck/jasmine@0.3
yarn add --dev @types/jasmine@latest
yarn add --dev jasmine@latest
Install ESLint (optional)
- npm
- yarn
npm install --save-dev eslint@latest
npm install --save-dev typescript-eslint@latest
yarn add --dev eslint@latest
yarn add --dev typescript-eslint@latest
Install NYC (optional)
Both jest and vitest already include support for code coverage.
- Mocha
- Jasmine
- npm
- yarn
npm install --save-dev requirejs@latest
npm install --save-dev nyc@latest
npm install --save-dev @istanbuljs/nyc-config-typescript@latest
yarn add --dev requirejs@latest
yarn add --dev nyc@latest
yarn add --dev @istanbuljs/nyc-config-typescript@latest
TODO
Configure Dependencies
Configure Typescript
First, we will create a basic TypeScript configuration file.
For Testdeck, we have to enable the experimentalDecorators feature, which is still experimental.
In addition, we will also enable the emitDecoratorMetadata feature required by reflect-metadata, which we already installed.
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"lib": [ "es6" ],
"noImplicitAny": true,
"removeComments": false,
"preserveConstEnums": true,
"sourceMap": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}
Configure Test Framework
- Mocha
- Jest
- Vitest
- Jasmine
Configure ESLint (optional)
- Mocha
- Jest
- Vitest
- Jasmine
TODO .eslintignore
TODO .eslintrc.js
TODO .eslintignore
TODO .eslintrc.js
TODO .eslintignore
TODO .eslintrc.js
TODO .eslintignore
TODO .eslintrc.js
Package Scripts
- npm
- yarn
"scripts": {
"prebuild": "npm run lint",
"lint": "eslint ...",
"lint-fix": "eslint --fix ..."
}
"scripts": {
"prebuild": "yarn run lint",
"lint": "eslint ...",
"lint-fix": "eslint --fix ..."
}
Configure NYC (optional)
- Mocha
- Jest
- Vitest
- Jasmine
TODO .nycrc
{
"extends": "@istanbuljs/nyc-config-typescript",
"all": true,
"report-dir": "coverage",
"reporter": [
"lcov",
"clover",
"json",
"text"
],
"include": [
"index.ts"
],
"check-coverage": true,
"branches": 80,
"lines": 80,
"functions": 80,
"statements": 80
}
Configure Package Scripts
Basic Setup
- npm
- yarn
"scripts": {
"build": "tsc",
"clean": "npx rimraf dist",
"pretest": "npm run clean && npm run build"
}
"scripts": {
"build": "tsc",
"clean": "npx rimraf dist",
"pretest": "yarn run clean && yarn run build"
}
Integrate Test Framework
Integrate ESLint (optional)
Integrate NYC (optional)
Summary
achievements