D
D
DarthJS2015-09-08 16:32:52
JavaScript
DarthJS, 2015-09-08 16:32:52

How to properly set up Jasmine Unit testing?

Hello! I just started testing using Jasmine and ran into a situation: how to test information and books in bulk, but I didn’t fully figure out how to connect it correctly.
On the example of my test project:

src /
----/ backend
----/ frontend

As indicated in the Jasmine docs, I set it globally, then I initialize the directory:
jasmine init in the frontend folder, since I decided to start from it.
I get the spec folder, in which I actually create the test file, for example, test.spec.js.
Actually I understand that dependencies are still needed to be registered, but where it is not clear.
Help plz who can.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuri Yarosh, 2015-09-08
@DarthJS

First, boot.js or node_boot.js is loaded - all methods of the jasmine interface are written there in window or global. Like like this

'use strict';

var jasmineRequire = require('jasmine-core/lib/jasmine-core/jasmine');

var jasmine = jasmineRequire.core(jasmineRequire);
window.jasmine = jasmine;

jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000;

var env = jasmine.getEnv();
var jasmineInterface = jasmineRequire.interface(jasmine, env);

function extend(destination, source) {
  for (var property in source) {
      destination[property] = source[property];
  }

  return destination;
}

extend(window, jasmineInterface);

Then comes the addition of a reporter, well, or you can write your own, for specific tasks ...
Initialization under node.js differs only in ConsoleReporter, in the browser one HtmlReporter is used.
After initialization, window / global already has all it describe expect junk, and you can write specs,
to run them it’s just done
There is a “convenient” wrapper for the lazy for the node separately ...
If you look at the source codes , then in principle it should be clear from the main methods
showColors - отображать ли разноцветные сообщения, форточки не умеют
addSpecFile - добавить файлик спеки
addReporter - добавить репортер
configureDefaultReporter, он же ConsoleReporter
addMatchers не помню
loadSpecs - просто require спеков
loadHelpers - просто require хэлперов
loadConfigFile - загрузка конфига
loadConfig - загрузка конфига с объекта
addSpecFiles - добавить кучу файлов спек
onComplete - вызвать callback по завершению
stopSpecOnExpectationFailure - прервать выполнение тестов если возникает исключение

Basically
var Jasmine = require('jasmine');
var jasmine = new Jasmine();

jasmine.loadConfig({
    spec_dir: 'tests',
    spec_files: [
        'requests/**/*.js'
    ],
});

// или jasmine.loadConfigFile('spec/support/jasmine.json');

jasmine.configureDefaultReporter({
    showColors: false // если это форточки
});

jasmine.execute();

To run tests in node.js will be enough.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question