Sto usando karma, gelsomino, dattiloscritto per scrivere test unitario per l'applicazione helloworld da https://angular.io/docs/js/latest/quickstart.html.unit test utilizzando dattiloscritto e karma
Di seguito si riporta il codice di prova:
///<reference path="../typings/jasmine/jasmine.d.ts"/>
import {
MyAppComponent
} from '../spray1';
describe("name is Alice",() => {
var comp = new MyAppComponent();
it("verify name",() => {
expect(comp.name).toBe("Alice");
});
});
TSC (con "--module commonjs") transpiles questo codice di prova in:
///<reference path="../typings/jasmine/jasmine.d.ts"/>
var spray1_1 = require('../spray1');
describe("name is Alice", function() {
var comp = new myAppComponent_1.MyAppComponent();
it("verify name", function() {
expect(comp.name).toBe("Alice");
});
});
karma non riesce ad eseguire il test di unità:
Uncaught Error: Module name "../myAppComponent" has not been loaded yet for context: _. Use require([]) http://requirejs.org/docs/errors.html#notloaded at /Users/spray1/web2/node_modules/requirejs/require.js:141
Chrome 43.0.2357 (Mac OS X 10.10.3): Executed 0 of 0 SUCCESS (0 secs/0 secs)
Se uso tsc con "--module amd", il codice di prova transpiled è:
define(["require", "exports", '../spray1'], function (require, exports, spray1_1) {
describe("name is Alice", function() {
var comp = new spray1_1.MyAppComponent();
it("verify name", function() {
expect(comp.name).toBe("Alice");
});
});
});
"test karma avviare/karma.conf.js" gettato sotto l'errore sui file transpiled JS:
Uncaught Error: Mismatched anonymous define() module: function (require, exports, spray1_1) { describe("name is Alice", function() { var comp = new spray1_1.MyAppComponent(); it("verify name", function() { expect(comp.name).toBe("Alice"); }); }); } http://requirejs.org/docs/errors.html#mismatch at /Users/spray1/web2/node_modules/requirejs/require.js:141 Chrome 43.0.2357 (Mac OS X 10.10.3): Executed 0 of 0 ERROR (0.04 secs/0 secs)
Come vedete, ho difficoltà a farlo funzionare in entrambi i casi (--module commonjs/AMD). Qual è la strada giusta da percorrere e come farlo funzionare? Apprezzo qualsiasi aiuto!
Come si fa riferimento ai file? Stai usando più tag di script? – Fenton
usa gulp per compilare ed eseguire dattiloscritto e il loro rispettivo banco di prova. Leggi https://medium.com/@bojzi/anatomy-of-a-large-angular-application-f098e5e36994#.mmfb9jtml per maggiori informazioni. – Ajay