Table of contents
1.
Introduction
2.
Global Setup Fixtures 
3.
Global Teardown Fixtures
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Global Fixtures

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Whenever we perform any testing, initialization before it starts and cleans up after it is executed, is necessary to be done globally. This global initialization and global cleanup are called global fixtures. The global fixture design allows us to define any number of global fixtures in any test file that constitutes a test module. The global fixture methods are monitored by an execution monitor, which makes error handling and reporting easier than initialization functions. 

The global fixtures are applied to every test runner when we execute testing. The global fixtures are similar to life cycle hooks in react, but these are executed only once, unlike the hooks. We have two types of global fixtures.

  1. Global Setup Fixtures 
  2. Global Teardown Fixtures

Let’s learn how the fixtures work with codes.

Global Setup Fixtures 

Global setup fixtures run before you run your tests and start a server required to run your tests using a specified port. To create a global fixture, we must first export the mochaGlobalSetup from the script. We can import it by including the following lines into our code.

exports.mochaGlobalSetup = async function () {
  this.server = await startServer({port: process.env.TEST_PORT});
  console.log(`server running on port ${this.server.port}`);
};


Run the mocha --require fileName.cjs command to load this file so that we can create a setup globally. This code executes the global setup fixture and starts a server for your mocha tests before you mocha runs all the tests. The server can be started using the method startServer by providing a port number from the environment variables. Finally, a message “server running on port port-number” is printed to the console. The provided port number is displayed in the place of the port number in the message. 

The created server will not shut down unless you stop it. So to stop it, we can use the Global teardown fixture.

Global Teardown Fixtures

The Global Teardown fixture shuts down the server created by the global setup fixture for the testing. To use the teardown fixture, we have to export it from the script file first. 

exports.mochaGlobalTeardown = async function () {
  await this.server.stop();
  console.log('server stopped!');
};


We can access the server created in the setup fixture using this.server and shut it down using the stop() method. This prints a message “Server stopped” to the console.

You can use Global fixtures only when you want to run them once and do not want to share the state with tests. If you want to access an in-memory value, you cannot use the global fixtures directly. Instead, we can create the fixtures to start and stop the server and then connect it to a server in your tests. 

let server;
export const mochaGlobalSetup = async () => {
  server = await startServer({port: process.env.TEST_PORT});
  console.log(`server running on port ${server.port}`);
};

export const mochaGlobalTeardown = async () => {
  await server.stop();
  console.log('server stopped!');
};


This program starts and stops the server as we discussed before in this article. 

import {connect} from 'my-server-connector-thingy';
describe('my API', function () {
  let connection;
  before(async function () {
    connection = await connect({port: process.env.TEST_PORT});
  });
  it('API', function () {
    // write tests here
  });

  after(async function () {
    return connection.close();
  });
});


We imported "connect" from the server library to connect to the server of the test port and then execute your tests using the it() method to execute them individually. After the tests are performed, the server can be closed using the after() and connection.close() methods.

FAQs

  1. What are global fixtures?
    This global initialization and clean-up after and before running the tests to start and stop the servers are called global fixtures. The global fixture methods are monitored by an execution monitor, which makes error handling and reporting easier than initialization functions. 
     
  2. What does a global setup fixture do?
    Global setup fixtures run before you run your tests and start a server required to run your tests using a specified port. The server created by this fixture runs until you shut it down.
     
  3. What does a global teardown fixture do?
    The Global Teardown fixture shuts down the server created by the global setup fixture for the testing after all the tests are run individually or as a group. 
     
  4. Is the export function async?
    The export function used to export the global fixtures can either be async or not async. It depends on the code of tests we want to run.
     
  5. When do we use global fixtures?
    We use global fixtures for spinning servers, creating resources for tests, creating sockets or schedulers, etc.

Key Takeaways

We have discussed global fixtures in Mocha, Global Setup, and Teardown. The global fixtures are used to start and clean up before and after a test is executed. We also discussed how to use global fixtures in different cases.

Hey Ninjas! We hope this blog helped you understand global fixtures better. If you want to learn more, check out Coding Ninjas for more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems. Please upvote our blog to help the other ninjas grow.

Happy Coding!

Live masterclass