1.1.1 • Published 5 years ago

jest-with-context v1.1.1

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

Usage, beautiful with prettier

// your.spec.ts
import { createContext } from 'jest-with-context';

const withContext = createContext({ foo: 0, whatever: 1 });

// just a describe wrapper, YourContext & { test }
withContext('test a api called whatever', ({ test, foo }) => {
  test('foo', ({ whatever }) => {
    expect(foo).toBe(0);
    expect(whatever).toBe(1);
  });
});

Usage, weird with prettier

// your.spec.ts
import { withContext } from '../src';

// just a describe wrapper, YourContext & { test }
withContext(
  'test a api called whatever',
  { foo: 0, whatever: 1 },
  ({ test, foo }) => {
    test('foo', ({ whatever }) => {
      expect(foo).toBe(0);
      expect(whatever).toBe(1);
    });
  }
);

Lifecycle

import { withContext } from '../src';

withContext('after each', { foo: 0 }, ({ test, afterEach }) => {
  let fooInternal = 0;
  afterEach(() => {
    fooInternal++;
    return { foo: fooInternal };
  });
  test('foo 0', ({ foo }) => {
    expect(foo).toBe(0);
  });
  test('foo 1', ({ foo }) => {
    expect(foo).toBe(1);
  });
});

// if you know what you're doing
withContext<{ foo: number }>(
  'before each without context',
  ({ test, beforeEach }) => {
    beforeEach((state = { foo: 0 }) => {
      return { foo: state.foo + 1 };
    });
    test('foo 1', ({ foo }) => {
      expect(foo).toBe(1);
    });
    test('foo 2', ({ foo }) => {
      expect(foo).toBe(2);
    });
  }
);