/NodeJSTests

5th homework assignment for Pirple's NodeJS master class. The project includes a test runner and unit tests for two simple libraries.

Primary LanguageJavaScript

Node JS Tests

5th homework assignment for Pirple's NodeJS master class. This project creates unit tests for two simple libraries: math and lang.

Features

  • Tests and test runner are within /test folder, libraries are within /app folder.
  • lang and math libraries both contain simple functions.
  • Tests are contained in test/lang_unit.js and test/math_unit.js. They check if the library functions return the correct values, that they throw or do not throw errors depending on the parameters passed in.

Screencast

See the tests in action and watch the code being explained line by line

Pirple Node JS Master Class Homework Assignment #5

Manual

Set up

  1. Download the project.
  2. Open the command prompt (for Windows, click Start icon and type in cmd) and go to the project directory.eg. :

cd C:/NodeJSTests

  1. Run the test runner:

node test

Optionally, one can set DEBUG variable to print out messages in the console. (for Windows):

set DEBUG=* & node test // Print out all debug messages

set DEBUG=helpers & node test // Print out debug messages coming from helpers module

Printscreens

All tests passed

Passed tests

One test failed, details are printed

Failed test

Libraries

Math library

Function Parameters Description
findDivisors number Returns an array of 2 greatest divisors for a given number. If number is invalid, TypeError is thrown. If number is 1, array [1] is returned.
getANumber min, max Returns a random number between min and max. If min is invalid, it is set to 1. If max is invalid, it is set to 10. If min is greater than max, then they are set to 1 and 10 respectively.

Unit tests

Test name Function parameters Expected function output
math.findDivisors returns [5,6] for 30 30 [5,6]
math.findDivisors returns [1] for 1 1 [1]
math.findDivisors throws exception for a non-number 'cat' TypeError
math.getANumber returns a random number between -100 and 100 -100,100 a number between -100 and 100
math.getANumber works when min > max 1000, 1 a number
math.getANumber does not throw exception for a non-number 'cat', null a number

Lang library

Function Parameters Description
conjugateRegularPreterite verb,person Returns a simple past form (preterite) of the verb passed in, eg. for ('cominar','tu') the result is 'caminaste'. If the verb is not in infinitive form (ends with -ar,-er,-ir), a custom VerbNotInfinitiveError is thrown. If verb is not a valid string, a custom LangError is thrown. person argument must be one of: yo, tu, el_ella, nosotros, vosotros, ellos_ellas, otherwise yo form is returned.
getOrdinalNumber number, noun Returns an ordinal number as a string in Spanish and the noun passed. Eg. for (4,'gato') result is 'cuatro gato'; for (10,'sandía') the result is 'décima sandía'. If the arguments are not number and noun respectively, a custom LangError is thrown.

Unit tests

Test name Function parameters Expected function output
lang.conjugateRegularPreterite should conjugate correctly 'beber', 'el_ella' 'bebió'
lang.conjugateRegularPreterite throws VerbNotInfinitiveError if verb is not an infinitive 'xxx', 'yo' VerbNotInfinitiveError
lang.conjugateRegularPreterite should return yo form if person argument is incorrect 'cantar', 'not_existing_personal_pronoun' 'canté'
lang.conjugateRegularPreterite should return yo form if person argument is missing 'cantar' 'canté'
lang.conjugateRegularPreterite throws LangError if arguments are of incorrect types 123, 123 LangError
lang.getOrdinalNumber works for singular nouns 2, 'manzana' 'segunda manzana'
lang.getOrdinalNumber works for plural nouns 10, 'aniversarios' 'décimos aniversarios'