Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   unit тесты как писать? (https://javascript.ru/forum/misc/82626-unit-testy-kak-pisat.html)

prototip 02.06.2021 13:10

unit тесты как писать?
 
ребята, покажите пожалуйста, как написать любые юнит тесты с помощью jest или mocha на примере этого кода. Это код бинарного дерева.
class TreeNode {
    public left: TreeNode = null;
    public right: TreeNode = null;

    constructor(
        public value: number
    ) { }
}

class BinaryTree {
    public root: TreeNode = null;

    public add(value: number): void {
        const node: TreeNode = new TreeNode(value);

        if (this.isEmpty()) {
            this.root = node;
        } else {
            let currentNode: TreeNode = this.root;

            while (currentNode) {
                if (value > currentNode.value) {
                    if (currentNode.right === null) {
                        currentNode.right = node;
                        return;
                    }

                    currentNode = currentNode.right;
                } else {
                    if (currentNode.left === null) {
                        currentNode.left = node;
                        return;
                    }

                    currentNode = currentNode.left;
                }
            }
        }
    }

    public search(value: number): number {
        let currentNode: TreeNode = this.root;

        while (currentNode) {
            if (value === currentNode.value) {
                return value;
            } else if (value > currentNode.value) {
                currentNode = currentNode.right;
            } else {
                currentNode = currentNode.left;
            }
        }

        return null;
    }

    private getSuccessor(node: TreeNode): TreeNode {
        let currentNode: TreeNode = node;

        while (currentNode) {
            if (currentNode.right === null) {
                break;
            }

            currentNode = currentNode.right;
        }

        return currentNode;
    }

    public isEmpty(): boolean {
        return this.root === null;
    }
}

ksa 02.06.2021 14:30

Цитата:

Сообщение от prototip
как написать любые юнит тесты с помощью jest или mocha

Вот статья как это можно сделать именно для JS кода...
https://learn.javascript.ru/testing

prototip 02.06.2021 21:17

ребята, помогите хотя бы один тест написать, не получается
Например:
10
7 12
3 9 11 13
const BinaryTree = require('../src/.tree')

describe("BinaryTree: add", () => {

    const _ = new BinaryTree()

    test("что писать в expect(_.add).toBe()?", () => {
        expect(_.add).toBe()

    })
})


Часовой пояс GMT +3, время: 01:31.