Subscribe

Scoping Jest tests

โœ๏ธ

Let's take a look at how we can write isolated test scopes in Jest

5 Apr, 2022 ยท 3 min read

So far, we only looked at tests executing on the highest level. All tests are basically in the same scope.

But Jest gives us the option to scope down. This can be super easy for recurring tests setup.

For instance, in the previous article, we learned about Jest recurring actions, which are prime examples of use for scoping.

How to define scope in Jest

To define a scope in Jest, we can use the describe function to wrap all our tests.

describe('user related actions', () => {
  test('Create new user', () => {
    expect(createUser('Chris', 'p4sSw0rD')).toBe(true);
  });

  test('Update user', () => {
    expect(updateUser(1, 'Chris new name')).toBe(true);
  });
});

Now, these two tests will run inside this scope, and besides it being nice to organize, we get some added benefits of this scope.

For instance, this scope can get its own recurring actions. These defined actions will only fire for this scope. However, global scope-defined actions will also fire!

For instance, letโ€™s say we have a public database, but for one section, we want to apply a hobby database as well.

beforeAll(() => {
  return createDatabase();
});

beforeEach(() => {
  return populateUsers();
});

afterEach(() => {
  return clearUsers();
});

afterAll(() => {
  return removeDatabase();
});

test('Add a new user', () => {
  expect(addUser('Chris')).toBe(true);
});

describe('hobby related tasks', () => {
  beforeEach(() => {
    return populateHobbies();
  });

  beforeEach(() => {
    return clearHobbies();
  });

  test('Add a new hobby', () => {
    expect(addHobby(1, 'Photography')).toBe(true);
  });
});

Letโ€™s run this code and see what gets called to see what happens.

  • before all
  • before each (global scope)
  • user test
  • after each (global scope)
  • before each (global scope)
  • before each (hobby scope)
  • hobby test
  • after each (hobby scope)
  • after each (global scope)
  • after all

As you can see, a complete riddle of executions, but itโ€™s essential to look at the before each and after each fire order.

The scope can also be used to define and overwrite variables that you might use.

For instance, we might have a different logged-in user for whom we want to write test cases.

describe('user related tasks', () => {
  const loggedUser = 1;
  test('Add a new user', () => {
    console.log(`adding new user: ${loggedUser}`);
  });
});

describe('hobby related tasks', () => {
  const loggedUser = 2;
  test('Add a new hobby', () => {
    console.log(`adding new hobby for: ${loggedUser}`);
  });
});

As you can see, we define the loggedUser twice, and the output of this sequence would nearly be as expected:

  • adding new user: 1
  • adding new hobby for: 2

I hope this gives you an excellent first introduction to defining scoped test blocks.

Thank you for reading, and letโ€™s connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Spread the knowledge with fellow developers on Twitter
Tweet this tip
Powered by Webmentions - Learn more

Read next ๐Ÿ“–

Testing library asFragment tests

18 Apr, 2022 ยท 2 min read

Testing library asFragment tests

Changing the default testing container

17 Apr, 2022 ยท 3 min read

Changing the default testing container

Join 2097 devs and subscribe to my newsletter