Writing Karma + Mocha tests with dependency injection and done?

What is the most elegant way to write Karma unit tests in mocha that have dependency injection and done ?

Dependency Inclusion:

 describe('cows', function(){ it('farts a lot', inject(function(cow){ // do stuff })) }) 

Done

 describe('cows', function(){ it('farts a lot', function(done){ // do stuff }) }) 

What if I want cow and done be available in unit test? Right now, this is what I am doing and it is unsatisfactory.

 beforeEach(inject(function(cow){ this.cow = cow; })) it('farts a lot', function(done){ this.cow // etc }) 
+5
source share
1 answer

A nested function can be inserted into a test function

 it("should nested inject function into test function", function(done) { inject(function($timeout) { $timeout(function() { expect(true).toBeTruthy(); done(); }, 10); $timeout.flush(10); }); }); 

inject is a global function defined in the ngMock module and can be used anywhere in the test.

+6
source

Source: https://habr.com/ru/post/1214275/


All Articles