samedi 18 novembre 2017

Simple tests of Observable results in nodejs with chai and mocha

I am developing an app using Nodejs, RxJS and Typescript.

The app has a function which returns an Observable of a string

myObsFunction() : Observable<string> {
... do stuff
}

I would like to be able to make a simple test to check that when I subscribe to this function I get the expected string. I am using chai and mocha and so I write the following test case

import { expect } from 'chai';
import 'mocha';

import {myObsFunction} from './my-source-file';

describe('myObsFunction function', () => {

    it('check myObsFunction', () => {
        const expectedString = 'abc';
        let receivedString: string;
        myObsFunction().subscribe(
            data => receivedString = data,
            error => console.error(error),
            () => expect(receivedString).to.equal(expectedString)
        )
    });

});

Unfortunately this test case does not work as expected by me. It always behaves as it has been successfully passed even in case of errors. The expect check which I have written in the onCompleted function does not signal anything even when the expectedString is not equal the the receivedString. The onCompleted function is actually executed (I can see this just adding a console.log instruction in the onCompleted function) but the expect does not signal any error when there are errors

Is there any way to run such simple tests without having to start using Schedulers and more complex mechanisms?

Aucun commentaire:

Enregistrer un commentaire