SemperCallide
SemperCallide

Reputation: 2080

Jasmine ignoring typescript test files?

This is my first time making a project with Jasmine, and I'm following a tutorial but right off the bat having issues.

I've installed jasmine-node, typings, and typescript. I also ran:

typings install dt~jasmine --save-dev --global 

For Jasmine typescript.

Now I have a test file in my ./spec folder that looks like this:

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { DatePickerComponent } from '../src/components/via-datepicker.component';
import * as moment from 'moment';

const Moment: any = (<any>moment).default || moment;

describe('DatePickerComponent', () => {
  let component: DatePickerComponent;
  let fixture: ComponentFixture<DatePickerComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ DatePickerComponent ]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(DatePickerComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });

  it('should open when clicked', () => {
    fixture.debugElement.nativeElement.querySelector('body').click();
    fixture.whenStable().then(() => {
      expect(component.opened);
    });
    component.close();
  });

  describe('While open', () => {
    beforeEach(() => {
      component.open();
    });

    describe('Pressing the "Today\'s date" button', () => {
      it('should set the value of the picker to the current date and close it', () => {
        fixture.debugElement.nativeElement.querySelector('.datepicker-buttons button').click();
        expect(Moment().isSame(component.value, 'day') && Moment().isSame(component.value, 'month'));
        expect(!component.opened);
      });
    });

    describe('Clicking on a date', () => {
      it('should change the value of the picker and close it', () => {
        let oldValue: any = component.value;
        fixture.debugElement.nativeElement.querySelectorAll('.day')[10].click();
        expect(!component.opened);
        expect(!component.value.isSame(oldValue));
      });
    });

  });

});

But when I run this command:

node_modules/jasmine-node/bin/jasmine-node spec

I get this result:

Finished in 0 seconds
0 tests, 0 assertions, 0 failures, 0 skipped

So clearly my test file is being ignored. Or maybe I'm missing some library? Would I receive an error message if this were the case? The main issue here is that I'm not being given much direction as to what the issue is, other than Jasmine doesn't seem to "see" the test file for some reason.

Just trying to move forward with my project. Any advice would be greatly appreciated.

Upvotes: 1

Views: 1565

Answers (1)

JacobB
JacobB

Reputation: 334

It appears as if your test runner doesn't know that you're trying to run typescript tests. Are you using Karma as your test runner? If so, you need to add your Typescript files to your karma.config file and install karma-typescript and configure your karma.config file similar to what is shown below. Pay close attention to the addition to the frameworks, files, and preprocessors sections.

karma.config

module.exports = function(config) {
config.set({

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '',


    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['jasmine', 'karma-typescript'],


    // list of files / patterns to load in the browser
    files: [
        { pattern: "app/tests/**/*.spec.js"}
    ],


    // list of files to exclude
    exclude: [
    ],


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
        "app/tests/**/*.spec.ts": ["karma-typescript"]
    },


    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['progress'],


    // web server port
    port: 9876,


    // enable / disable colors in the output (reporters and logs)
    colors: true,


    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_INFO,


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: false,


    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: [],

    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: true
    })
};

Upvotes: 1

Related Questions