This is my module declaration:
(angular.module 'services.date', [])
.service '$date', () ->
# stuff
this
This is my injection code:
describe 'Service: dateService', () ->
dateService = null
beforeEach module 'services.date'
beforeEach inject (_$date_) ->
dateService = _$date_
describe 'Test', () ->
it 'should test stuff', () ->
(expect true).toBe true
If I run this code I get this error:
Firefox 37.0.0 (Ubuntu 0.0.0) Service: dateService Test should test stuff FAILED
minErr/<#/home/pv/Sites/web/ngbp-coffee/vendor/angular/angular.js:68:12
loadModules/<#/home/pv/Sites/web/ngbp-coffee/vendor/angular/angular.js:4411:15
forEach#/home/pv/Sites/web/ngbp-coffee/vendor/angular/angular.js:336:11
loadModules#/home/pv/Sites/web/ngbp-coffee/vendor/angular/angular.js:4372:5
createInjector#/home/pv/Sites/web/ngbp-coffee/vendor/angular/angular.js:4297:11
workFn#/home/pv/Sites/web/ngbp-coffee/vendor/angular-mocks/angular-mocks.js:2172:44
Now here's the interesting part: If I comment out the injection code, the test passes:
describe 'Service: dateService', () ->
# dateService = null
# beforeEach module 'services.date'
# beforeEach inject (_$date_) ->
# dateService = _$date_
describe 'Test', () ->
it 'should test stuff', () ->
(expect true).toBe true
// after running
Firefox 37.0.0 (Ubuntu 0.0.0): Executed 1 of 1 SUCCESS (0.003 secs / 0.002 secs)
Done, without errors.
What is it about how I'm injecting the service that's wrong? Please help.
UPDATE
It looks like the $injector service cannot recognize my other service. If I do
$injector.has '$date'
I get false
WTF am I doing wrong?????
The "Underscore Wrapping" is only useful when you do not want to hide your variable (dateService) with the injected reference (_ $date _). Meaning that using _ $date _ is only useful if you want to call your variable "$date" (not dateService).
However, it does not solve your issue. $injector should have '$date'. I guess we can't help you without more code. Something is probably wrong with your factory declaration or with the configuration file given to Karma, as, obviously, "$date" has not been loaded.
The same code is working for me (with an empty factory, however).
Related
I'm trying to write tests for an old codebase of mine. It uses the isbn package from NPM, which is tiny but does the trick. But whenever I write tests that involve the module, it disappears. That is to say - the value of the module is set to {}.
I've written up two files to try and isolate this issue. I just can't figure out how this test is failing.
First file, isbnTest.js:
const { ISBN } = require("isbn");
function isbnExists() {
return ISBN !== undefined;
}
console.log(isbnExists());
module.exports = {
isbnExists,
};
Pretty simple:
import the module,
one function that just checks if the module has been successfully imported,
log the results, and
export the tester function.
Running this file from the console logs
true
But what happens when we run this code from within Jest?
The second file, ./isbnTest.test.js:
const { isbnExists } = require("./isbnTest.js");
test("isbn should exist", () => {
expect(isbnExists()).toBe(true);
});
When I run npm test with these two files, the test fails.
FAIL
./isbnTest.test.js
✕ isbn should exist (4ms)
● isbn should exist
expect(received).toBe(expected) // Object.is equality
Expected: true
Received: false
2 |
3 | test("isbn should exist", () => {
> 4 | expect(isbnExists()).toBe(true);
| ^
5 | });
6 |
at Object.<anonymous> (isbnTest.test.js:4:24)
console.log isbnTest.js:7
false
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
It seems as though Jest must be doing some custom importing stuff that's somehow missing this module. Almost like it's being replaced with an empty mock? But I really don't know.
I'd love to remove the isbn package from my program, but without any tests I don't feel confident that I can ensure I won't break anything.
EDIT:
A commenter has pointed out that this doesn't reproduce, which means there's something askew on my machine. Can anyone provide as guess as to what that might be? Deleting and reinstalling the NPM modules doesn't do the trick. I don't think I have any jest config files.
After some further investigation, the problem is being caused by this line in the module:
var exports = typeof window === 'object' && window ? window: module.exports;
If I comment that out, Jest picks it up fine.
I am using karma/mocha/chrome headless for my test stack.
--js 1
document.addEventListener('test', ...);
do sth more.
--js 2
document.addEventListener('test', ...);
do sth more differently.
-- test suite 1
require(test1)
i am dispatching the test event here to test js 1
-- test suite 2
require(test2)
I am dispatchinh the test event here to test js 2
The problem is that both of these files will be now available globally. When I am running the test suite 2, both js1 and js2 events are going to listen my dispatch because js1 will be still globally available.
My perfect scenario would be. Load js1 to test suite 1 and encapsulate it there. Hacky way would be remove it after the tests are ran.
My question is, how to make sure that needed js files are loaded locally in the scope of one test suite? Basically load only nesseccary files for the specific test suite and keep them isolated from rest of test suites.
Thanks.
If I understand the question correctly, you want to scope your modules to the relevant test file?
If that's the case then you can load your modules like so:
const sharedModule = require('some/shared/module');
describe('some test', function() {
const scopedModule = require('some/path/to/module');
it('should...', function() {
// Test code...
})
)
This would result in your module being loaded only when the test begins to execute, being scoped inside that block.
I have a unit test that is created with the Jasmine framework. When I put a single test specification in the describe block I get a pass in the karma console. If I copy that describe block with the test in it ( the it(); ) then I suddenly start getting problems with dependencies that the module uses.
In the console I get errors around unknown providers.
Here is my simple test:
describe('service definition tests', function () {
it('should be defined', function () {
expect(sut).toBeDefined();
});
});
and that passes okay. If I copy this block I get an error about dependencies. Which is strange as I've already proved that I can test the 'sut' is defined in the first test.
One thing to note is that I have a beforeEach block that loads the module and provides a dependency and it is this dependency that errors when I've duplicated the test. Here is the beforeEach:
var mockConfig = {};
beforeEach(function () {
module('app');
module(function ($provide) {
$provide.value('myConfig', mockConfig);
});
});
the problem has to be something to do with this beforeEach being as the error I get is about the myConfig dependency.
Here is the error:
uncaught Error: [$injecor:unpr] Unknown provider: myConfigProvider <- myConfig <- authorisation
http://errors.angularjs.org/1.4.6/$injector/unpr?p0=myConfiProvider
I managed to resolve this issue by creating a dummy implementation of myConfig factory so that the test files used this.
angular.module('app').factory('myConfig', function() {
var env = 'test';
return {
env: env
}
});
This code lives in a js file that is loaded with the rest of the tests.
I am new to Mocha and AngularJS Unit Testing but want to test my application using Mocha. I have basic language tests working, but I cannot run tests against my applications Factory or Controller.
I have the following basic files.
apps.js
aangular.module('MyApp', []);
file1.js
angular.module('MyApp').factory('Factory1' ...);
file2.js
angular.module('MyApp').factory('Factory2' ...);
angular.module('MyApp').factory('Controller' ...);
describe('Main Test', function() {
var FactoryToTest;
beforeEach(module('MyApp'));
beforeEach(inject(function (_Factory_) {
FactoryToTest = _Factory_;
}));
describe('Factory2', function () {
it('should return "unknown"', function () {
Game = {};
expect(new Factory2(Game)).to.equal('unknown');
});
});
});
When I run the test, it generates an error, and I am not sure what to fix to get this to work.
Error:
Message:
object is not a function
Stack:
TypeError: object is not a function
at Suite.<anonymous> (b:\app\test.js:5:16)
You're getting an error because the beforeEach function should take a callback function instead of an object. According to the Angular guide on module unit testing (scroll to bottom of the page) :
Each module can only be loaded once per injector. Usually an Angular app has only one injector and modules are only loaded once. Each test has its own injector and modules are loaded multiple times.
I am working with karma & jasmine for unit testing my javascript pages. After all configuration done i was able to run the test cases. However, the expect statement is falining stating undefined. even though i hard code 2 strings in the expect, its failing. I tried toBe & toEqual but without success. Below is my code:
describe('Sanity Test', function() {
var scope;
beforeEach(angular.mock.module('serviceApp'));
beforeEach(angular.mock.inject(function($rootScope, $controller) {
scope = $rootScope.$new();
$controller('welcomeController', {
$scope : scope
});
}));
it('Sanity test Jasmine"', function() {
scope.text = 'Hi';
expect('Hi').toEqual('Hi');
});
});
Error:
INFO [launcher]: Starting browser Chrome
INFO [Chrome 30.0.1599 (Windows 7)]: Connected on socket DjMqv6LulftBpkJ2ph7g
Chrome 30.0.1599 (Windows 7) Sanity Test Sanity test Jasmine" FAILED
expect undefined toEqual "Hi"
.....src/main/webapp/test/spec/controllers/welcome.test.js:15:3: expected "Hi" but was undefined
Chrome 30.0.1599 (Windows 7): Executed 1 of 1 (1 FAILED) ERROR (0.424 secs / 0.025 secs)
Been struggling for last 2 days.
May be something to do with the karma version i was using. I moved to mac with a slightly older version of karma and everything worked smooth.
I was getting undefined messages like this for all my tests in an Ionic project which ran with grunt. It ended up being because my karma settings were configured to use mocha and chai instead of jasmine. Just edit your configuration (whether its gulpfile.js, Gruntfile.js or karma.my.js) from
frameworks: ['mocha', 'chai'],
to
frameworks: ['jasmine'],