From Types to Testing: TypeScript Basics, Jest Mocking, and Git Etiquette
When learning modern full-stack development, knowing a programming language is not enough. We also need to write clean code, test our code, and work properly with Git.
In this post, we will look at some important basics of TypeScript, Jest testing, Git workflows, and code review etiquette.
1. TypeScript Basics: Types, Interfaces & Generics
TypeScript is JavaScript with static typing.
It helps us find many errors before running our code.
Basic Types
We can specify the type of a variable:
const developerName: string = "Harshith";
const experienceYears: number = 2;
const isDeveloper: boolean = true;
Here:
-
string→ text -
number→ numbers -
boolean→trueorfalse
Interfaces
An interface defines the structure of an object.
interface User {
readonly id: number;
name: string;
email: string;
role?: string;
}
Now we can create a User object:
const currentUser: User = {
id: 101,
name: "Harshith",
email: "harshith@example.com",
};
Important Interface Features
readonly
readonly id: number;
The value cannot be changed after the object is created.
Optional property
role?: string;
The role property is optional.
So this is also valid:
const user: User = {
id: 102,
name: "Rahul",
email: "rahul@example.com",
};
2. Why TypeScript Is Useful
JavaScript allows this:
let age = 20;
age = "twenty";
TypeScript can prevent this mistake:
let age: number = 20;
// Error
age = "twenty";
This makes large applications easier to maintain and debug.
3. Jest Testing Basics
Jest is a JavaScript testing framework.
It allows us to check whether our functions are working correctly.
For example:
function add(a, b) {
return a + b;
}
We can test it using Jest:
test("adds two numbers", () => {
expect(add(2, 3)).toBe(5);
});
Here:
-
test()defines a test. -
expect()checks the result. -
toBe()compares the actual value with the expected value.
Testing helps us find bugs before they reach production.
4. Jest Mocking
Sometimes our function depends on another function or service.
Instead of calling the real service during a test, we can use a mock.
For example:
const getUser = jest.fn();
getUser.mockReturnValue({
name: "Harshith"
});
Now getUser() returns our test data instead of calling a real database or API.
Mocking is useful because tests become:
- Faster
- More predictable
- Independent of external services
5. Git Feature Branches
When working on a project, we should avoid making every change directly on the main branch.
Instead, we can create a feature branch:
git checkout -b feature/typescript-basics
Then make our changes and commit them:
git add .
git commit -m "feat: add TypeScript basics"
Finally, push the branch:
git push origin feature/typescript-basics
We can then create a Pull Request and ask other developers to review our changes.
6. Code Review Etiquette
Code reviews are not about finding mistakes in someone's code to criticize them.
The main goal is to improve the code together.
Instead of saying:
"This code is bad."
We can say:
"Could we simplify this function to make it easier to maintain?"
Good code review comments should be:
- Clear
- Respectful
- Specific
- Helpful
We should also explain why we are suggesting a change.
Conclusion
Today I learned that modern development involves more than just writing JavaScript.
TypeScript helps us write safer code with types and interfaces.
Jest helps us test our code and find problems early.
Git branches help us work safely without directly changing the main branch.
Code review etiquette helps teams communicate and improve code together.
Learning these concepts will help me write cleaner code and work more effectively in a professional development environment.
Top comments (0)