Как запустить Jest-тест в приложении Typescript - PullRequest
0 голосов
/ 16 февраля 2020

Я написал приложение как часть требования о приеме на работу. Это общее JS, все тесты пройдены, однако мне нужно, чтобы Jest смог протестировать типы c stati, на данный момент он застревает в первом экземпляре описания типа stati c ( n: число ):

index.ts

  convertNumberToEnglishText: (n:number) => {
    var string = n.toString(),
      units,
      tens,
      scales,
      start,
      end,
      chunks,
      chunksLen,
      chunk,
      ints,
      i,
      word,
      words;

    /* Remove spaces and commas */
    string = string.replace(/[, ]/g, "");

    /* Is number zero? */
    if (parseInt(string) === 0) {
      return "zero";
    }

    // 0 < int <20
    units = [
      "",
      "one",
      "two",
      "three",
      "four",
      "five",
      "six",
      "seven",
      "eight",
      "nine",
      "ten",
      "eleven",
      "twelve",
      "thirteen",
      "fourteen",
      "fifteen",
      "sixteen",
      "seventeen",
      "eighteen",
      "nineteen"
    ];

    // 20 <= int *= 10 < 100
    tens = [
      "",
      "",
      "twenty",
      "thirty",
      "forty",
      "fifty",
      "sixty",
      "seventy",
      "eighty",
      "ninety"
    ];

    // 1000 <= int *= 1000 <= 1000000
    scales = ["", "thousand", "million"];

    /* Split argument into 3 digit chunks from right to left, e.g. 1000000 -> 1,000,000 */
    start = string.length;
    chunks = [];
    while (start > 0) {
      end = start;
      chunks.push(string.slice((start = Math.max(0, start - 3)), end));
    }

    /* Check if function has enough scale words to be able to stringify the user argument */
    chunksLen = chunks.length;
    if (chunksLen > scales.length) {
      return "";
    }

    /* Stringify each integer in each chunk */
    words = [];
    for (i = 0; i < chunksLen; i++) {
      chunk = parseInt(chunks[i]);

      if (chunk) {
        /* Split chunk into array of individual integers */
        ints = chunks[i]
          .split("")
          .reverse()
          .map(parseFloat);

        /* If tens integer is 1, i.e. 10, then add 10 to units integer */
        if (ints[1] === 1) {
          ints[0] += 10;
        }

        /* Add scale word if chunk is not zero and array item exists */
        if ((word = scales[i])) {
          words.push(word);
        }

        /* Add unit word if array item exists */
        if ((word = units[ints[0]])) {
          words.push(word);
        }

        /* Add tens word if array item exists */
        if ((word = tens[ints[1]])) {
          words.push(word);
        }

        /* Add hundreds word if array item exists */
        if ((word = units[ints[2]])) {
          words.push(word + " hundred");
        }
      }
    }

    // Add word 'negative', if there is a '-' sign before the numbers' string
    if (string[0] === "-") {
      words.push("negative");
    }
    // Reverse the words array
    return words.reverse().join(" ");
  }
};

module.exports = functions;

index.test. js


describe('Number to English converter test suite', () => {
    it('should print zero for 0', () => {
        expect(functionsImp.convertNumberToEnglishText(0)).toBe('zero');
    });

    it('should print negative one for -1', () => {
        expect(functionsImp.convertNumberToEnglishText(-1)).toBe('negative one');
    });

    it('should print nineteen for 19', () => {
        expect(functionsImp.convertNumberToEnglishText(19)).toBe('nineteen');
    });

    it('should print twenty for 20', () => {
        expect(functionsImp.convertNumberToEnglishText(20)).toBe('twenty');
    });

    it('should print correctly for 41', () => {
        expect(functionsImp.convertNumberToEnglishText(41)).toBe('forty one');
    });

    it('should print correctly for 100', () => {
        expect(functionsImp.convertNumberToEnglishText(100)).toBe('one hundred');
    });

    it('should print correctly for 101', () => {
        expect(functionsImp.convertNumberToEnglishText(101)).toBe('one hundred one');
    });

    it('should print correctly for 739', () => {
        expect(functionsImp.convertNumberToEnglishText(739)).toBe('seven hundred thirty nine');
    });

});

npm запустить тест -> журнал ошибок

 SyntaxError: C:\Users\lukas\Desktop\Side Projects\challenge-1-number-conversion\src\index.ts: Unexpected token, expected "," (2:32)

      1 | const functions = {
    > 2 |   convertNumberToEnglishText: (n:number) => {
        |                                 ^
      3 |     var string = n.toString(),
      4 |       units,
      5 |       tens

Можно ли шутить, чтобы проверить, правильно ли совпадают типы * stati c?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...