Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Заменил кастомные тесты на Jest #1

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
coverage
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ npm install
npm start
```
## Запуск тестов
На текущий момент не используем каких-то стороних тест-раннеров. Тесты запускаем нодой, а тестируем стандартным нодовским assert.
Так как тест это es6 module, версия ноды должна быть не меньше 13. Сам запускал на v16.13.2.
Запустить тест можно командой:
Unit-тестируем calculator.js, потому что там лежит вся "бизнес-логика" нашего калькулятора демонения.
```
npm test
```

Чтобы посмотреть с code coverage:
```
npm run test:run
```

## Формула демонения
Количество созданных существ определяется здоровьем убитого отряда и количеством "Питов".

Expand Down
4 changes: 4 additions & 0 deletions jest.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"coverageDirectory": "./coverage",
"collectCoverageFrom": ["src/js/calculator.js"]
}
10,144 changes: 9,308 additions & 836 deletions package-lock.json

Large diffs are not rendered by default.

13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
{
"name": "heroes3-demonology",
"version": "0.1.0",
"type": "module",
"devDependencies": {
"serve": "^13.0.2"
"serve": "14.2.1",
"jest": "29.7.0",
"prettier": "3.0.3",
"serve": "13.0.2"
},
"type": "module",
"scripts": {
"start": "serve ./src",
"test": "node ./test/calc_test.js"
"jest": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
"test": "npm run jest -- --watch",
"test:run": "npm run jest -- --coverage"
}
}
}
2 changes: 1 addition & 1 deletion src/js/calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ const getOptimalHealth = function (overallHealth) {
return overallHealth - leftovers;
}

export default { getDemonsNumber: getDemonsNumber, getOptimalNumber: getOptimalNumber };
export default { getDemonsNumber, getOptimalNumber };
108 changes: 108 additions & 0 deletions src/js/calculator.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { test, describe, expect } from "@jest/globals";
import calc from "./calculator.js";

const testData = [
{
name: "3 pitlords and 15 imps",
data: [3, 15, 4],
expectedDemonsNumber: 1,
expectedOptimalNumber: 9
},
{
name: "demons into demons",
data: [3, 5, 35],
expectedDemonsNumber: 4,
expectedOptimalNumber: 4
},
{
name: "Case when demons by pitlords is BIGGER then by creatures",
data: [25, 100, 4],
expectedDemonsNumber: 11,
expectedOptimalNumber: 97
},
{
name: "Case when demons by pitlords is LESS then demons by creatures",
data: [4, 100, 4],
expectedDemonsNumber: 5,
expectedOptimalNumber: 44
},
{
name: "Check that demons is does not more than creatures",
data: [10, 5, 80],
expectedDemonsNumber: 5,
expectedOptimalNumber: 5
},
{
name: "Case when creatures is not enough for one demon",
data: [10, 11, 3],
expectedDemonsNumber: 0,
expectedOptimalNumber: 0
},
{
name: "Case when creatures is just enough for one demon",
data: [10, 7, 5],
expectedDemonsNumber: 1,
expectedOptimalNumber: 7
},
{
name: "Creatures a little bit more for one demon",
data: [10, 36, 1],
expectedDemonsNumber: 1,
expectedOptimalNumber: 35
},
{
name: "Creatures a little bit more for one demon",
data: [10, 36, 1],
expectedDemonsNumber: 1,
expectedOptimalNumber: 35
},
{
name: "All zeroes",
data: [0, 0, 0],
expectedDemonsNumber: 0,
expectedOptimalNumber: 0
},
{
name: "Zero pitlords",
data: [0, 10, 10],
expectedDemonsNumber: 0,
expectedOptimalNumber: 0
},
{
name: "Zero creatures",
data: [10, 0, 10],
expectedDemonsNumber: 0,
expectedOptimalNumber: 0
},
{
name: "Zero creature hp",
data: [10, 10, 0],
expectedDemonsNumber: 0,
expectedOptimalNumber: 0
},
{
name: "A lot of everything",
data: [9999, 9999, 9999],
expectedDemonsNumber: 9999,
expectedOptimalNumber: 9999
},
{
name: "Passing Strings",
data: ["10", "36", "1"],
expectedDemonsNumber: 1,
expectedOptimalNumber: 35
},
];

describe.each(testData)("getDemonsNumber()",
({ name, data, expectedDemonsNumber }) => test(name, () =>
expect(calc.getDemonsNumber(...data)).toBe(expectedDemonsNumber)
)
);

describe.each(testData)("getOptimalDemonsNumber()",
({ name, data, expectedDemonsNumber, expectedOptimalNumber }) =>
test(name, () =>
expect(calc.getOptimalNumber(...data, expectedDemonsNumber)).toBe(expectedOptimalNumber)
)
);
57 changes: 0 additions & 57 deletions test/calc_test.js

This file was deleted.