Initialize:
mkdir usv-npm && cd $_
Run:
git init
git remote add origin [email protected]:SixArm/usv-npm.git
git branch -M main
touch .gitignore && git add -A && git commit -m "Init"
git push -u origin main
Verify:
git status
Result:
… Your branch is up to date with 'origin/main'.
Run:
npm init -y
Verify:
npm --version
Output:
10.5.2
Run:
npm install typescript --save-dev
Verify:
tsc -v
Output:
Version 5.4.5
Initialize:
tsc --init
Review:
cat tsconfig.json
Output:
Created a new tsconfig.json with:
target: es2016
module: commonjs
strict: true
esModuleInterop: true
skipLibCheck: true
forceConsistentCasingInFileNames: true
Run:
mkdir src && cd $_
echo 'console.log("hello world")' > index.ts
tsc index.ts
node index.js
Output:
hello world
Commit:
git add index.{js,ts}
git commit -m "Add hello world"
Run:
npm install jest ts-jest @types/jest --save-dev
Add a script to your file package.json
:
"scripts": {
"test": "jest"
}
Create file jest.config.js
:
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
};
Create source file src/sum.ts
:
export function sum(a: number, b: number): number {
return a + b;
}
Create corresponding test file tests/sum.test.ts
:
import { sum } from "../src/sum";
describe("Math functions", () => {
it("should sum two numbers correctly", () => {
expect(sum(1, 2)).toEqual(3);
});
});
Run:
npm test
Output:
PASS tests/sum.test.ts
Math functions
✓ should sum two numbers correctly (1 ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 0.653 s
Ran all test suites.