title: Is it possible to define a Jasmine spec inside a function and still have beforeEach apply to it?
body: <p>I have a lot of tests which are virtually the same. In the interests of DRY and scanability I'd like to abstract the tests into a single function and then call that function with a few parameters. The function would then call <code>it</code> and add the spec to the suite.</p>
  
  <p>It seems to work, except the specs don't get run in the same way as the other specs and <code>beforeEach</code> is not called before the specs defined in the common function.</p>
  
  <pre><code>define(['modules/MyModule','jasmine/jasmine'], function(MyModule) {
  
      describe('myModule', function() {
  
          function commonTests(params) {
              it('should pass this test OK', function() {
                  expect(true).toBe(true);
              });
  
              it('should fail because module is undefined', function() {
                  expect(module[params.method]()).toBe('whatever');
              });
          }
  
          var module;
  
          beforeEach(function() {
              module = new MyModule();
          });
  
          describe('#function1', function() {
              commonTests({
                  method: 'function1'
              });
          });
  
          describe('#function2', function() {
              commonTests({
                  method: 'function2'
              });
          });
  
      });
  
  });
  </code></pre>
  
  <p>Is there any way of doing this and maintaining the functionality of <code>beforeEach</code> and <code>afterEach</code>?</p>
  
  <p><strong>UPDATE:</strong> </p>
  
  <p>Looks like I got my example wrong, sorry. Here's the case that fails:</p>
  
  <pre><code>define(['modules/MyModule'], function(MyModule) {
  
      function commonTests(params) {
          it('will fail because params.module is undefined', function() {
              expect(typeof params.module).toBe('object');
              expect(typeof params.module[params.method]).toBe('function');
          });
  
          it('has a few tests in here', function() {
              expect(true).toBe(true);
          });
      }
  
  
      describe('MyModule', function() {
  
          var module;
  
          beforeEach(function() {
              module = new MyModule();
          });
  
          describe('#function1', function() {
              commonTests({
                  module: module,
                  method: 'function1'
              });
          });
  
          describe('#function2', function() {
              commonTests({
                  module: module,
                  method: 'function2'
              });
          });
  
      });
  });
  </code></pre>
  
  <p>I think it fails because the value of <code>module</code> is preserved as part of the call to <code>commonTests</code> instead of always using the current value of <code>module</code> as in the first example. I'll post my solution when I get there...</p>