Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions lessons/lesson12/demo/__tests__/tokenService.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { createToken } from "../src/tokenService.js";

describe("tokenService", () => {
describe("createToken", () => {
test("should create token with correct format", () => {
const userId = "test-user-123";
const token = createToken(userId);

expect(token).toHaveProperty("token");
expect(token).toHaveProperty("expiresIn");
expect(token.token).toMatch(/^token-test-user-123-\d+$/);
expect(token.expiresIn).toBe(3600);
});

test("should create unique tokens for same user", () => {
const userId = "user1";
const token1 = createToken(userId);

// Небольшая задержка чтобы timestamp отличался
setTimeout(() => {
const token2 = createToken(userId);
expect(token1.token).not.toBe(token2.token);
}, 1);
});

test("should handle different user IDs", () => {
const token1 = createToken("user1");
const token2 = createToken("user2");

expect(token1.token).toContain("user1");
expect(token2.token).toContain("user2");
expect(token1.token).not.toBe(token2.token);
});

test("should return consistent expiresIn value", () => {
const token1 = createToken("user1");
const token2 = createToken("user2");

expect(token1.expiresIn).toBe(3600);
expect(token2.expiresIn).toBe(3600);
});
});
});
51 changes: 51 additions & 0 deletions lessons/lesson12/demo/__tests__/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { sum, isAdult } from "../src/utils.js";

describe("utils", () => {
describe("sum", () => {
test("should add two numbers correctly", () => {
expect(sum(2, 3)).toBe(5);
expect(sum(-1, 1)).toBe(0);
expect(sum(0, 0)).toBe(0);
});

test("should handle negative numbers", () => {
expect(sum(-5, -3)).toBe(-8);
expect(sum(-10, 5)).toBe(-5);
});

test("should handle decimal numbers", () => {
expect(sum(1.5, 2.5)).toBe(4);
expect(sum(0.1, 0.2)).toBeCloseTo(0.3);
});
});

describe("isAdult", () => {
test("should return true for adults", () => {
/** @type {User} */
const adult = { id: "1", name: "John", age: 25, isActive: true };
expect(isAdult(adult)).toBe(true);
});

test("should return false for minors", () => {
/** @type {User} */
const minor = { id: "2", name: "Jane", age: 16, isActive: true };
expect(isAdult(minor)).toBe(false);
});

test("should return true for exactly 18 years old", () => {
/** @type {User} */
const eighteenYearOld = { id: "3", name: "Bob", age: 18, isActive: true };
expect(isAdult(eighteenYearOld)).toBe(true);
});

test("should handle edge cases", () => {
/** @type {User} */
const almostAdult = { id: "4", name: "Alice", age: 17, isActive: true };
expect(isAdult(almostAdult)).toBe(false);

/** @type {User} */
const veryOld = { id: "5", name: "Elder", age: 100, isActive: false };
expect(isAdult(veryOld)).toBe(true);
});
});
});
49 changes: 49 additions & 0 deletions lessons/lesson12/demo/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/** @type {import('jest').Config} */
const config = {
// Среда выполнения тестов (node для серверного кода)
testEnvironment: "node",

// Поддержка ES модулей, так как в package.json указан "type": "module"
preset: "ts-jest/presets/default-esm",

// Паттерны для поиска тестовых файлов
testMatch: [
"**/__tests__/**/*.js", // Файлы в папке __tests__
"**/?(*.)+(spec|test).js", // Файлы с суффиксами .test.js или .spec.js
],

// Настройки покрытия кода
collectCoverage: true,
coverageDirectory: "coverage",
coverageReporters: ["text", "lcov", "html"],

// Исключить из анализа покрытия
coveragePathIgnorePatterns: ["/node_modules/", "/coverage/", "/docs/"],

// Настройки для работы с ES модулями (новый синтаксис)
transform: {
"^.+\\.js$": [
"ts-jest",
{
useESM: true,
},
],
},

// Расширения файлов для разрешения модулей
moduleFileExtensions: ["js", "json"],

// Verbose вывод для детальной информации о тестах
verbose: true,

// Очистка моков между тестами
clearMocks: true,

// Показать покрытие в консоли
collectCoverageFrom: [
"src/**/*.js",
"!src/index.js", // Исключить главный файл из анализа покрытия
],
};

export default config;
Loading