2

i want to execute an external function inside .execute() function of dalekjs. Is it possible to do that?

Pratima
  • 21
  • 3

1 Answers1

3

Depends on what you mean with external.

If you want to execute a function that already exists in your client side JavaScript it must be accessible via the global window object. For example:

In one of my client side scripts I have something like this:

function myAwesomeFn (message) {
  $('body').append('<p>' + message + '</p>');
}

If that function is defined in the global scope (not within some IIFE f.e.), you can trigger it in your execute function like so:

test.execute(function () {
  window.myAwesomeFn('Some message');
});

If you mean "a function that is defined within a Dalek Testsuite" with external, I might must disappoint you, because Daleks testfiles and the contents of the execute function are invoked in different contexts (different JavaScript engines even).

So this does not work:

'My test': function (test) {
   var myFn = function () { // does something };

   test.execute(function () {
     myFn(); // Does not work, 'myFn' is defined in the Node env, this functions runs in the browser env

   })
 }

What does work:

'My test': function (test) {


   test.execute(function () {
     var myFn = function () { // does something };
     myFn(); // Does work, myFn is defined in the correct scope

   })
 }

Hope that answers your question, if not, please provide some more details.

EDIT:

Load the file(s) using node own require

var helper = require('./helper');
module.exports = {
  'My test': function (test) { 
     test.execute(helper.magicFn)
   }
 };

In your helper.js, you can do whatever you want, this would make sense (more or less):

module.exports = {
  magicFn: function () { 
     alert('I am a magic function, defined in node, executed in the browser!');
   }
 };

For further strategies on how to keep your testing code DRY ;), check this repo/file: Dalek DRY Example

  • By external i meant if i could write a file which contains javascript function and i could include that file and call that function inside the .execute() of dalekjs like any other js does. As dalek is all about javascript so is it okay to think that way too? – Pratima Mar 10 '14 at 06:38
  • 1
    Updated the answer with some more info about file includes. – Sebastian Golasch Mar 11 '14 at 18:04
  • I hope that works for me. Your strong support for Dalekjs is really helpful. – Pratima Mar 14 '14 at 06:06