I had previously been unit testing my Knockout app and components with Jasmine and Karma. As I don't need these tests to test any of the DOM, I am switcing to Mocha.
I am testing a Knockout component like this (very simplified):
MyComponent.js:
function MyComponent(params) {
this.forename = params.forename;
this.surname = params.surname;
}
exports.MyComponent = MyComponent
ko.components.register('my-component', {
viewModel: MyComponent,
template: {
fromUrl: 'my-component.html'
}
});
MyComponent.test.js:
var expect = require("expect.js");
var MyComponent = require("./src/components/my-component/my-component");
describe("Should do stuff", function () {
it("should do stuff, function () {
var myComponent = new MyComponent({});
expect(myComponent.forename).to.be(undefined);
});
});
When I run this (using gulp-mocha), I get an error saying that ko is not defined
. Now I'm pretty sure that I need to require knockout in MyComponent.js but I'm not sure how. I've tried doing:
var ko = require("ko");
... require("knockout");
... require("knockout.js");
...require("path/to/knockout/knockout");
And I still get this error message. What am I doing wrong here?