Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8774703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:38:04+00:00 2026-06-13T18:38:04+00:00

I have a lot of tests which are virtually the same. In the interests

  • 0

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 it and add the spec to the suite.

It seems to work, except the specs don’t get run in the same way as the other specs and beforeEach is not called before the specs defined in the common function.

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'
            });
        });

    });

});

Is there any way of doing this and maintaining the functionality of beforeEach and afterEach?

UPDATE:

Looks like I got my example wrong, sorry. Here’s the case that fails:

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'
            });
        });

    });
});

I think it fails because the value of module is preserved as part of the call to commonTests instead of always using the current value of module as in the first example. I’ll post my solution when I get there…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:38:05+00:00Added an answer on June 13, 2026 at 6:38 pm

    Thanks to Andreas for pointing out that my first example actually worked! The final solution I’m using is very similar:

    define(['modules/MyModule'], function(MyModule) {
    
        var module;
    
        function commonTests(params) {
            it('will not fail because module is shared', function() {
                expect(typeof module).toBe('object');
                expect(typeof module[params.method]).toBe('function');
            });
    
            it('has a few tests in here', function() {
                expect(true).toBe(true);
            });
        }
    
    
        describe('MyModule', function() {
    
            beforeEach(function() {
                module = new MyModule();
            });
    
            describe('#function1', function() {
                commonTests({
                    method: 'function1'
                });
            });
    
            describe('#function2', function() {
                commonTests({
                    method: 'function2'
                });
            });
    
        });
    });
    

    Although if you needed to have the ability to pass in module as an argument to commonTests you would have to take a slightly different approach and have a different function for each it block:

    define(['modules/MyModule'], function(MyModule) {
    
        var module;
    
        function commonTest1(params) {
            expect(typeof module).toBe('object');
            expect(typeof module[params.method]).toBe('function');
        }
    
        function commonTest2(params) {
            expect(true).toBe(true);
        }
    
    
        describe('MyModule', function() {
    
            beforeEach(function() {
                module = new MyModule();
            });
    
            describe('#function1', function() {
    
                it('will not fail because module is shared', function() {
                    commonTest1({ method: 'function1' });
                });
    
                it('has a few tests in here', function() {
                    commonTest2({ method: 'function1' });
                });
    
            });
    
            describe('#function2', function() {
    
                it('will not fail because module is shared', function() {
                    commonTest1({ method: 'function2' });
                });
    
                it('has a few tests in here', function() {
                    commonTest2({ method: 'function2' });
                });
    
            });
    
        });
    });
    

    This way the executions of the functions containing the common tests is delayed until after beforeEach has run its callback.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have lot of code in my Tornado app which looks like this: @tornado.web.asynchronous
I have a lot of integration tests which read expected results from files. My
We have a lot of integration tests written using JUnit 3 , though we're
I'm writing some unit tests and I have a lot of functions of the
I have two unit tests that should share a lot of common tests with
I have a test file that contains tests taking quite a lot of time
I've been running into this problem a lot lately. I have an NSMutableArray called
I'm looking for framework which provides unit tests for JS. Later, I'll have to
I have multiple functional tests written as NUnit test which are independent from each
I've a make file for a scripted system, with a lot of tests which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.