JavaScript testing: quick start

How to quickly setup a JS testing with Karma runner and Jasmine:

Install node (http://nodejs.org/download/)
Open the command prompt, and run
npm install -g karma
//Packages are installed to c:\Users\your-user-name\AppData\Roaming
pm
Generate karma config file.
In a command prompt: cd to your development folder, and

karma init

If you get a warning

WARN [init]: No binary for Chrome. Create symlink at "C:\Users\user-name\AppDataLocal\Google\Chrome\Application\chrome.exe", or set "CHROME_BIN" env variable.

then run this command: set CHROME_BIN=C:/Program Files (x86)/Google/Chrome/Application/chrome.exe

Open karma.conf.js, and set files array to files = [JASMINE, JASMINE_ADAPTER, ‘fileWithTests.js’];, where fileWithTest is a test container.

To write tests consult Jasmine help page: http://pivotal.github.io/jasmine/

describe("A suite", function() {
  it("contains spec with an expectation", function() {
    expect(true).toBe(true);
  });
});

Done.

Late binding in SignalR

Once in a while there’s a need to add a callback to signalR after the hubs/connections have been started. Out of the box – you can’t, though its pretty easy to add.

Here’s a solution we’re using for one of internal projects, which basically wraps the hub, and allows to (optionally) add the callbacks at any time: Continue reading “Late binding in SignalR”

JavaScript gotchas

Sort of part 2 to JavaScript notes.

 

JavaScript has a minimum time resolution of 4ms. If you do a setInterval with 1ms delay – it will be bumped to 4 ms.

var x = { val: 0 };
var id = setInterval(() => { x.val++; }, 1);
setTimeout(() => { clearInterval(id); console.log("X: ", x); }, 10000);

This code will print same value of x.val whether the delay is 1 or 4 ms.

Registering custom NLog target in code

Created a custom target, and wanted to add it in code, instead of adding it in nlog.config file, at least for now.

Documentation says all you need is this

ConfigurationItemFactory.Default.Targets.RegisterDefinition("myTarget", typeof(SignalRTarget));

 which is 1/3 of the story.

After some some googling and thinking I realized I probably need to specify a rule for the new target. This is the working code:

ConfigurationItemFactory.Default.Targets.RegisterDefinition("SignalRTarget", typeof(SignalRTarget)); 

LogManager.Configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Trace, new SignalRTarget())); 

LogManager.ReconfigExistingLoggers();