TypeScript module testing with Jasmine

575 Views Asked by At

I have the following structure : src folder with file1.ts spec folder(same level as src - siblings) with file1.spec.ts I try to run typescript Jasmine tests file1.ts looks like this :

export module moduleName {
  export class className {
    public doSomething(a: string): string {
      return a;
    }
  }
}

file1.spec.ts looks like this :

const aliasFile = require('../src/file1');

describe("tests", function () {

  let f;

  beforeEach(function () {
    f = new aliasFile.className();
  });

  it("should run", function () {
    const result = f.doSomething('aaa');
    expect(result).toEqual('aaa');
  });
});

when I ran this test I get this result :

  1. tests should run
  • TypeError: aliasFile.className is not a constructor
  • TypeError: Cannot read property 'doSomething' of undefined

what is the proper way to define the test?

1

There are 1 best solutions below

0
raksit31667 On

You can try replacing require method with aliased import keyword like this:

import { moduleName as aliasFile } from './file1';

describe("tests", function () {

  let f: aliasFile.className;

  beforeEach(function () {
    f = new aliasFile.className();
  });
  it("should run", function () {
    const result = f.doSomething('aaa');
    expect(result).toEqual('aaa');

  });
});