Search code examples
typescriptunit-testingkarma-jasmine

Unit testing ionic function which returns or uses a promise - TS2304: Cannot find name 'done' - Jasmine / Karma for unit testing ionic apps


I'm trying to unit test a simple data access function with mocked Storage that simply uses a dictionary in memory.

When I try to use the done() function I get the error: TS2304: Cannot find name 'done' despite jasmine and karma seemingly being installed correctly.

Things I've done that have not fixed this issue:

  1. Verified Jasmine is in packages.json
  2. Added **/*.spec.ts to excludes section of tsconfig.json?
  3. Changed target from "e5" to "e6" in tsconfig.json

data.ts:

export class DataProvider {
  private foo;
  public readonly fooKey
  public getFoo() { return this.foo; }
  public setFoo(bar: number) {
        this.foo = bar;
        this.storage.ready().then(() => {
            this.storage.set(this.fooKey, JSON.stringify(this.foo));
        });
    }
}

data.spec.ts:

include StorageMock;
include DataProvider;

 it('should have correct values after loading data',
       function() {
           comp.storage.set(comp.fooKey, JSON.stringify(0.1234));
           comp.storage.get(comp.fooKey).then(result => {
               expect(JSON.parse(result)).toEqual(0.1234);
               done(); // Error - TS2304: Cannot find name 'done'
           });
});

StorageMock:

export class StorageMock {
    private internal = [];

    public driver(): any {
        return '';
    }

    public ready(): Promise<any> {
        return new Promise(function(resolve: Function): void {
            resolve({});
        });
    }

    public get(key: string): Promise<any> {
        let getval = this.internal[key];
        return new Promise(function(resolve: Function): void {
            resolve(getval);
        });
    }

    public set(key: string, value: any): Promise<any> {
        this.internal.push({key : value});
        return new Promise(function(resolve: Function): void {
            resolve();
        });
    }

    public remove(key: string): Promise<any> {
        let index = this.internal.indexOf(key);
        if(index !== -1) {
            this.internal.splice(index,1);
        }
        return new Promise(function(resolve: Function): void {
            resolve();
        });
    }

    public clear(): Promise<any> {
        this.internal = [];
        return new Promise(function(resolve: Function): void {
            resolve();
        });
    }

    public length(): Promise<any> {
        let length = this.internal.length;
        return new Promise(function(resolve: Function): void {
            resolve(length);
        });
    }

    public keys(): Promise<any> {
        let keys = Object.keys(this.internal);
        return new Promise(function(resolve: Function): void {
            resolve(keys);
        });
    }

    public forEach(i: any): Promise<any> {
        return new Promise(function(resolve: Function): void {
            resolve();
        });
    }

Solution

  • Actual answer:

    done is not a function that is part of the Jasmine framework - rather it is used within a test's declaration as a callback for when the test finishes. Notice in the below the (done) declaration:

    it("should test something async", (done) => {
     ...do the testing...
     done();
    }
    

    Original answer:

    While I am not sure why I can't use done(), it seems that using fakeAsync, flushMicrotasks, tick kind of sort of gets the job "done" (I am still having some async issues with the storage mock).