Sulla base delle risposte precedenti, ho creato la seguente configurazione per angolare cli. Ho anche bisogno di un modulo esterno a mio matcher (in questo caso moment.js)
Nota In questo esempio ho aggiunto un equalityTester, ma dovrebbe funzionare con un matcher cliente
Creare un file con l'src/spec_helper.ts
seguenti contenuti:
// Import module
import { Moment } from 'moment';
export function initSpecHelper() {
beforeEach(() => {
// Add your matcher
jasmine.addCustomEqualityTester((a: Moment, b: Moment) => {
if (typeof a.isSame === 'function') {
return a.isSame(b);
}
});
});
}
Poi, nel src/test.ts
importare la funzione initSpecHelper()
aggiungere eseguirlo. L'ho posizionato prima dell'iniziale TestBed di Angular, che sembra funzionare bene.
import { initSpecHelper } from './spec_helper';
//...
// Prevent Karma from running prematurely.
__karma__.loaded = function() {};
// Init our own spec helper
initSpecHelper();
// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting()
);
//...
Ho anche trovato un bell'esempio qui: http://tobyho.com/2012/01/30/write-a-jasmine-matcher/ – Dancrumb
ho provato link condiviso da Dancrumb, idealmente il suo approccio ordinato e pulito tuttavia non ha funzionato, quindi ho adottato l'approccio di Alex. – joy
Ecco uno per Jasmine 2.0+ '' '' javascript beforeeach (function() { jasmine.addMatchers ({ toEqualData: function() {{ ritorno confronto: la funzione (reale, atteso) {{ ritorno pass: angular.equals (actual, expected)} } } } }); }); '' '' – mikker