Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/saleh-coder/typescript-project-setup

A comprehensive guide for setting up TypeScript projects with Node.js, including testing frameworks like Jest, to streamline your development process.
https://github.com/saleh-coder/typescript-project-setup

boilerplate devtools jest nodejs npm testing ts-node typescript

Last synced: 3 days ago
JSON representation

A comprehensive guide for setting up TypeScript projects with Node.js, including testing frameworks like Jest, to streamline your development process.

Awesome Lists containing this project

README

        

# 🌐 Configuring TypeScript in Node.js Project

## πŸ“š Step-by-Step Guide to Configure TypeScript

- **🎯 Goal:** Set up a Node.js project to use TypeScript.
- **❓ Why Convert?**: TypeScript needs to be transpiled into JavaScript because Node.js and browsers don't understand TypeScript.

### 1. πŸ“ Create the Project Folder

1. Open the terminal.
2. Run the following commands to create the project folder and the `package.json` file:

```bash
cd Desktop
mkdir typescript
cd typescript
npm init -y
```

The command `npm init -y` generates a basic `package.json`:

```json
{
"name": "typescript",
"version": "1.0.0",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
}
}
```

### 2. πŸ–₯️ Open the Project in VSCode

Open the project in VSCode by running:

```bash
code .
```

### 3. πŸ“‚ Create Folder Structure and TypeScript File

In VSCode, create a `src` folder. Inside `src`, create `index.ts` and add:

```typescript
const x: number = 3;
console.log(x);
```

### 4. πŸš€ Attempt to Execute the File with Node.js

Try running the TypeScript file directly:

```bash
node src/index.ts
```

**πŸ’‘ Expected Result:** You’ll see an error like:

```bash
SyntaxError: Missing initializer in const declaration
```

**πŸ” Why?** Node.js doesn’t understand TypeScript. We need to set up TypeScript to compile the code.

### 5. πŸ“¦ Install Necessary Dependencies

Install the required packages:

```bash
npm install -D typescript ts-node-dev
```

- **πŸ“œ typescript:** TypeScript compiler.
- **πŸ”„ ts-node-dev:** Runs TypeScript directly on Node.js with hot-reloading.

### 6. βš™οΈ Configure TypeScript

Initialize TypeScript configuration:

```bash
npx tsc --init
```

Open `tsconfig.json` and replace:

```json
// "outDir": "./",
```

with:

```json
"outDir": "dist",
```

**πŸ“ Explanation:** This tells TypeScript to output compiled files to the `dist` folder.

### 7. πŸ› οΈ Add Scripts in package.json

Update your `package.json`:

```json
"scripts": {
"build": "tsc",
"dev": "ts-node-dev --respawn src/index.ts"
}
```

- **πŸ› οΈ build:** Compiles TypeScript to JavaScript.
- **πŸ”„ dev:** Runs the code with ts-node-dev for hot-reloading.

### 8. βœ… Test the Configuration

Compile the code:

```bash
npm run build
```

**πŸ’‘ Expected Result:** A `dist` folder with the compiled `index.js`.

Run the compiled code:

```bash
node dist/index.js
```

**πŸ’¬ Expected Output:** `3` is displayed in the terminal.

Run the development environment:

```bash
npm run dev
```

**πŸ’‘ Expected Result:** Code runs, with auto-reload on file changes.

**πŸ“– Command Explanation:**

- **πŸ”„ ts-node-dev:** Runs TypeScript with Node.js and hot-reloading.
- **⏩ --respawn:** Restarts the process on file changes.
- **πŸ“‚ src/index.ts:** The main file to run.

## πŸ§ͺ Configuring Jest for TypeScript Testing

### 🎯 Goal:

Set up Jest to run tests in a TypeScript project.

### Steps:

1. πŸ› οΈ Install testing dependencies:

```bash
npm i -D jest ts-jest
```

- **πŸ§ͺ jest:** Testing framework.
- **βš™οΈ ts-jest:** Adapter to use Jest with TypeScript.

2. πŸ“ Create Jest configuration:

Create a `jest.config.js` file:

```javascript
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
};
```

3. πŸ“₯ Install Jest type definitions:

```bash
npm i -D @types/jest
```

### 🧩 Creating and Running Tests

1. πŸ’» Create code to be tested:

In `src/calc.ts`, add:

```typescript
export function sum(a: number, b: number): number {
return a + b;
}
```

2. πŸ§ͺ Create test file:

In `tests/calc.test.ts`:

```typescript
import { sum } from "../src/calc";

test("It should add up correctly", () => {
const result = sum(12, 20);
expect(result).toBe(32);
});
```

3. ✏️ Update test script:

In `package.json`, add:

```json
"scripts": {
"test": "jest"
}
```

4. πŸƒ Run the tests:

```bash
npm test
```

**πŸ’‘ Expected Result:** Tests are executed and Jest shows the results.

## πŸ“Š Summary

- **πŸš€ TypeScript Setup:** You now have a working TypeScript environment in Node.js with hot-reloading.
- **πŸ§ͺ Testing Setup:** Jest is set up to run tests on TypeScript code.

## πŸ“š Additional Resources:

- [Jest Documentation](https://jestjs.io/docs/getting-started)
- [ts-jest Documentation](https://kulshekhar.github.io/ts-jest/docs/getting-started/installation)