Почему не хочет запускаться тест Jest?

Рейтинг: 0Ответов: 1Опубликовано: 06.02.2023

const getAge = require('./mainScripts');
const { test } = require('picomatch');
const { expect } = require('@jest/globals');


test (
    'Проверяем функцию getAge на тип данных',
    () => {
        expect(getAge(typeof (`${this.firstName}'s old is ${this.age} years`)).toBe('string'));
    }
)

введите сюда описание изображения

введите сюда описание изображения

введите сюда описание изображения

введите сюда описание изображения

Мой код:

class Student {
    constructor(firstName,lastName,yearOfBirth,arrayOfGrades) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.yearOfBirth = yearOfBirth;
        this.arrayOfGrades = arrayOfGrades;
        this.age = new Date().getFullYear() - this.yearOfBirth;
        this.avgMark = arrayOfGrades.reduce((sum,item) => {
            return sum += item
        }, 0) / arrayOfGrades.length;

    }
    //______Возраст студента______//
    getAge () {
        return (`${this.firstName}'s old is ${this.age} years`);
    }
    //______Средний бал______//
    averageMark () {
        return(`${this.firstName}'s average mark is ${this.avgMark}`);
    }
}

class Visit extends Student {
    constructor(firstName, lastName, yearOfBirth, arrayOfGrades) {
        super(firstName, lastName, yearOfBirth, arrayOfGrades);
        this.visitinMagazine = [];
    }
    //______Используется когда студент был на занятие______//
    present () {
        if (this.visitinMagazine.length < 26) {
            this.visitinMagazine.push(true);
            return this;
        } else {
            return this.visitinMagazine.pop();
        }
    }
    //______Используется когда студент НЕ был на занятие______//
    absent () {
        if (this.visitinMagazine.length < 26) {
            this.visitinMagazine.push(false);
            return this;
        } else {
            return this.visitinMagazine.pop();
        }
    }
}

 class Calculation extends Visit {
    constructor(firstName, lastName, yearsOfBirth, arrayOfGrades) {
        super(firstName, lastName, yearsOfBirth, arrayOfGrades);
    }

     //______Проверяем среднюю оценку и посейщение______//
     summary () {
         const averageVisit = this.visitinMagazine.filter((element) => element === true).length / this.visitinMagazine.length;
         if (this.avgMark > 90 && averageVisit > 0.9) {
             return console.log('Cool!');
         } else if (this.avgMark > 90 || averageVisit > 0.9) {
             return console.log('Good, but it can be better!');
         } else {
             return console.log('Radish');
         }
     }
 }
//______Экземпляры расчетов______//
const calculate = new Calculation('Dmitriy', 'Yaroshchuk', 2001, [70, 80, 90, 100, 90, 90, 99, 100, 95, 100]);
const calculate1 = new Calculation('Andrew', 'Kavetsky', 2000, [90, 90, 90, 90, 90, 90, 100, 100, 95, 93]);
const calculate2 = new Calculation('Diana', 'Koko', 1999, [70, 70, 70, 70, 70, 70, 75, 75, 75, 93]);

console.log(calculate.getAge());
console.log(calculate1.getAge());
console.log(calculate2.getAge());

console.log(calculate.averageMark());
console.log(calculate1.averageMark());
console.log(calculate2.averageMark());


//______Посейщение уроков______//
calculate.present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().absent().absent();

//______Посейщение уроков______//
calculate1.absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().present().present();

//______Посейщение уроков______//
calculate2.present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().absent().absent().absent().absent().absent();


calculate.summary();
calculate1.summary();
calculate2.summary();

module.exports = this.getAge;
// module.exports = this.averageMark();
// module.exports = this.present();
// module.exports = this.absent();

введите сюда описание изображения

введите сюда описание изображения

введите сюда описание изображения

Ответы

▲ 1Принят

В файле с классами добавьте перед классом Calculation export

export class Calculation extends Visit {

А дальше создавайте тест

const { Calculation } = require('./mainScripts');

describe('Тестируем класс Calculation', () => {
  const name = 'Dmitriy';
  const age = 2001;
  const lastName = 'Yaroshchuk';
  const calc = new Calculation(name, lastName, age, [70, 80, 90, 100, 90, 90, 99, 100, 95, 100]);
  test('Проверка на типы', () => {
    expect(calc.getAge()).toBeTruthy()
    expect(typeof calc.getAge()).toBe('string')
  })

  test('Выводит имя и год рождения', () => {
    expect(calc.getAge()).toBe(`${name}'s old is ${2023 - age} years`)
  })

})

Если хотите протестировать именно экземпляр класса, то экспортируете его при создании

export const calculate = new Calculation(...)

Импортируете и пишите тест

const { calculate } = require('./mainScripts');

describe('Тестируем экземпляр класса', () => {
  test('Проверка на типы', () => {
    expect(calculate.getAge()).toBeTruthy()
    expect(typeof calculate.getAge()).toBe('string')
  })

  test('Выводит имя и год рождения', () => {
    expect(calculate.getAge()).toBe(`Dmitriy's old is 22 years`)
  })
})