2.9.0 • Published 6 years ago

jest-plugin-context v2.9.0

Weekly downloads
30,874
License
MIT
Repository
github
Last release
6 years ago

jest-plugin-context

npm npm npm

Adds context as an alternative to describe for jest.

Getting Started

Install jest-plugin-context using yarn:

yarn add --dev jest-plugin-context

Motivation

RSpec took the ruby world by storm with its declarative method of TDD. In RSpec, describe it used to wrap a set of tests against one functionality while context is to wrap a set of tests against one functionality under the same state.

The difference being you should only describe to test the User model and specifically describe the #name method. However, testing different states of the #name method should use different context. You can view an example of this below.

Usage

If you want, you can import context from jest-plugin-context at the top of every test:

import context from 'jest-plugin-context';

If you want to install context as a global, you can modify the jest section of your package.json to include:

"jest": {
  "setupFiles": [
    "jest-plugin-context/setup"
  ]
}

Example

Here's an example test that uses context:

describe('User', () => {

  describe('#name', () => {
    set('firstName', () => 'Harry');
    set('lastName', () => 'Potter');
    set('user', () => new User({firstName, lastName}));

    context('with blank first name', () => {
      set('firstName', () => null);

      it('should return only the last name', () => {
        expect(user.name).toEqual('Potter');
      });
    });

    context('with blank last name', () => {
      set('lastName', () => null);

      it('should return only the first name', () => {
        expect(user.name).toEqual('Harry');
      });
    });
  });
});
2.9.0

6 years ago

2.8.2

6 years ago

2.8.1

6 years ago

2.8.0

6 years ago

2.7.0

6 years ago

2.6.0

6 years ago

2.5.1

6 years ago

2.5.0

7 years ago

2.4.2

7 years ago

2.4.1

7 years ago

2.4.0

7 years ago

2.3.4

7 years ago

2.3.3

7 years ago

2.3.2

7 years ago

2.3.1

7 years ago

2.3.0

7 years ago

2.2.4

7 years ago

2.2.3

7 years ago

2.2.2

7 years ago

2.2.1

7 years ago

2.2.0

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.8.3

7 years ago

1.8.0

7 years ago

1.7.1

7 years ago

1.7.0

7 years ago

1.5.0

7 years ago

1.4.0

7 years ago

1.3.0

7 years ago

1.2.0

7 years ago

1.0.0

7 years ago

0.3.0

7 years ago

0.1.0

7 years ago