Adrian B Adrian B - 2 years ago 110
Javascript Question

Testing an invalid argument w/ Chai in JavaScript

Preface: Last week I started tinkering with JavaScript, so this may or may not be a dumb question (though I'll wager the former).

I created this method to return the day of the week when

is initialized with a
. I'm trying to create a test with
to see what it does when it's fed a bogus argument (like a string).

Two questions:

  1. Am I checking the type correctly?

  2. How would I test a bogus argument for

    DateHelper.prototype.getDayOfTheWeekFromDate = function(inputDate) {
    if (inputDate instanceof Date) {
    inputDate = new Date();
    var dayOfTheWeek = inputDate.getDay();

    switch(dayOfTheWeek) {
    case 0: return "Sunday";
    case 1: return "Monday";
    case 2: return "Tuesday";
    case 3: return "Wednesday";
    case 4: return "Thursday";
    case 5: return "Friday";
    case 6: return "Saturday";
    default: return;
    } else {
    // I'm shooting for doing nothing if the input isn't a Date()
    // How would I test this?

I've got a test class set up to test a Date helper class.

'use strict';
var chai = require('chai');
var expect = chai.expect;
var DateHelper = require('../date_helper');
chai.config.includeStack = true; // true enables stack trace

describe('DateHelper', function() {

context('With a valid date value, getDayOfTheWeekFromDate', function() {
it('should return Tuesday', function() {
expect(subject.getDayOfTheWeekFromDate(new Date())).to.eq('Tuesday');

I tried this, but the console wigs out when I feed the initializer a string, doesn't know what undefined is. What should I be testing here?:

context('With a invalid date value, getDayOfTheWeekFromDate', function() {
it('should return nothing', function() {

Answer Source

It would work fine if you had all the parentheses balanced:

                            //                         ^
                            //    this one was missing /
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download