Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's a good way to reuse test code using Jasmine?

I'm using the Jasmine BDD Javascript library and really enjoying it. I have test code that I'd like to reuse (for example, testing multiple implementations of a base class or running the same tests in a slightly different context) and I'm not sure how to do it using Jasmine. I know that I could move code out of the jasmine functions and into reusable classes but I like the way the code reads interspersed with the Jasmine functions (describe, it) and I don't want to separate the specs from the test code unless I have to. Has anyone out there using Jasmine come across this issue and how have you handled it?

like image 839
glenn Avatar asked Feb 21 '11 01:02

glenn


People also ask

What is Jasmine used for testing?

Jasmine is one of the popular JavaScript unit testing frameworks which is capable of testing synchronous and asynchronous JavaScript code. It is used in BDD (behavior-driven development) programming which focuses more on the business value than on the technical details.

Can Jasmine be used for integration testing?

Angular ships with Jasmine, a JavaScript framework that enables you to write and execute unit and integration tests.

How can I speed up my jasmine test?

One of the quickest ways to get your tests running faster is by running your tests in parallel, with karma-parallel. This npm package splits your unit tests into multiple suites that run in parallel with each other, on different threads of your processor.


1 Answers

Here is an article by a guy at Pivotal Labs that goes into detail about how to wrap a describe call:

DRYing up Jasmine Specs with Shared Behavior

Snippet from the article that shows part of the wrapper function:

function sharedBehaviorForGameOf(context) {   describe("(shared)", function() {     var ball, game;     beforeEach(function() {       ball = context.ball;       game = context.game;     });   }); } 
like image 136
starmer Avatar answered Oct 12 '22 02:10

starmer