I am trying to do headless testing of my knockout viewmodels. I purposely avoid dealing with any ui constructs in my viewmodel and leave the wireup to the html page.
Th
Maybe this is because Knockout has changed (as the accepted answer is old), but today, I don't believe this is necessary (anymore). You can easily test a Knockout viewmodel. All I needed to do was set the global ko
variable in my test:
global.ko = require('../../Website/Scripts/knockout-3.4.0.js');
After that, you can run your test as usual: instantiate your viewmodel, perform any operations on it and assert.
I've written a little more about it, but in essence, this works for me:
global.ko = require('../../Website/Scripts/knockout-3.4.0.js');
var MyViewModel = require('../../Website/Scripts/myViewModel.js').MyViewModel;
describe('MyViewModel', function() {
var viewModel;
beforeEach(function(){
viewModel = new MyViewModel();
});
describe('...', function() {
/* And so on */
});
});