Reset JS knockout view function in QUnit tests

I'm just getting started with Knockout JS and with QUnit to do unit tests of my model with a knockout. The problem I am facing is that if I have several tests in the qunit javascript file and I link to a javascript file that includes my definition of Model Model Model Model ... any changes I make to the View Model in one test are also present when I run the next test in the test module. I'm used to having a NUnit environment where my state is automatically cleared between tests.

Is there a method, template, or example that anyone points to that best defines the view model and does it have a reset state to start each unit test?

+4
source share
1 answer

Do you use the second parameter (life cycle) module ? If not, you should be able to instantiate your view model at this level, for example:

 module("foo", { setup: function() { this.model = instantiateModel(); }, tearDown: function() { // execute reset here }); test("bar", function() { ok(this.model.hasSomething() !== null, "msg"); }); 

From what I remember, for reading, QUnit tags are executed in the same area as setup and tearDown , so any members defined in setup will be available in any subsequent tests.

+2
source

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


All Articles