sujay kodamala
sujay kodamala

Reputation: 317

Can't find variable: require - Jasmine Unit testing using Chutzpah

I'm really new to Churtzpah and Jasmine. I have been trying to run tests using Chutzpah. I'm using Jasmine, Typescript , Chutzpah , angular2 to write unit tests. Whenever i try to run the tests , I see that my tests are being detected . My chutzpah.json file is as follows :

     {
  "Framework": "jasmine",

  "Compile": {
    "Mode": "Executable",
    "Executable": "../compile.bat",
    "Extensions": [ ".ts" ],
    "ExtensionsWithNoOutput": [ ".d.ts" ],
    "UseSourceMaps": true,
    "Paths": [
      {
        "SourcePath": "../",
        "OutputPath": "../"
      }
    ]
  },
  "References": [
   { "Path": "../node_modules/es6-shim/es6-shim.js" },
    { "Path": "../node_modules/reflect-metadata/Reflect.js" },
    { "Path": "../node_modules/systemjs/dist/system.src.js" },
    { "Path": "../node_modules/zone.js/dist/zone.js" },
    { "Path": "../node_modules/zone.js/dist/jasmine-patch.js" },
    { "Path": "../node_modules/zone.js/dist/async-test.js" },
    { "Path": "../node_modules/zone.js/dist/fake-async-test.js" },
    { "Path": "../node_modules/core-js/client/shim.min.js" },
    {
      "Path": "app",
      "Includes": [ "*.ts" ],
      "Excludes": [ "*.d.ts" ]
    }
  ],
  "Tests": [
    { "Include": "*.spec.ts" }

  ]
}







import {it, describe, beforeEach, inject, beforeEachProviders} from "@angular/core/testing";

import {LoginService} from "./login.service";

describe("testing LoginService", () => {
    let Myservice: LoginService = new LoginService();
    it("Should return true", () => {
        expect(Myservice.validateUser("test", "test")).toBe(true);
    });
        it("validateUser should fail if input values are null", () => {
            expect(Myservice.validateUser(null, null)).toBe(false);
        });
    });

Let me know what else I need to do. thanks

the compile.bat looks like

    @echo off tsc.cmd app/login/login.service.ts app/tests/login.service.spec.ts --sourcemap--declaration

I see the following error in the test output window

Error: ReferenceError: Can't find variable: require
at global code in file:"path"
0 passed, 0 failed, 0 total (chutzpah).

========== Total Tests: 0 passed, 0 failed, 0 total ==========

Upvotes: 0

Views: 1494

Answers (2)

Mac
Mac

Reputation: 2342

It looks like the path to your source has an error. You're specifying:

"Path": "../app/"

But this path is relative to the location of the chutzpah.json file. It should say:

"Path": "app"

Upvotes: 1

Matthew Manela
Matthew Manela

Reputation: 16762

In the Chutzpah repo there is a Angualr-Typescript sample that might be helpful to get you started. It's Chutzpah.json below, but a couple key things to notice.

  1. Ensure tests are not included in the references section
  2. Make sure you explicitly reference the key angular framework files individually in the right

Chutzpah.json

{
  "Framework": "jasmine",
  "Compile": {
    "Mode": "Executable",
    "Executable": "../compile.bat",
    "Extensions": [ ".ts" ],
    "ExtensionsWithNoOutput": [ ".d.ts" ],
    "UseSourceMaps": true,
    "Paths" : [
      { "SourcePath": "../", "OutputPath": "../" }
    ]
  },
  "References": [
    { "Path": "../../libs/angular.min.js", "IsTestFrameworkFile": true },
    { "Path": "../../libs/angular-mocks.js", "IsTestFrameworkFile": true },
    { "Path": "../src", "Includes": [ "*.ts" ], "Excludes": ["*.d.ts"] }
  ],
  "Tests": [{ "Include": "*Tests.ts" }]
}

Upvotes: 1

Related Questions