Usage, beautiful with prettier
import { createContext } from 'jest-with-context';
const withContext = createContext({ foo: 0, whatever: 1 });
withContext('test a api called whatever', ({ test, foo }) => {
test('foo', ({ whatever }) => {
expect(foo).toBe(0);
expect(whatever).toBe(1);
});
});
Usage, weird with prettier
import { withContext } from '../src';
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);
});
});
withContext<{ foo: number }>(
'before each without context',
({ test, beforeEach }) => {
let fooInternal = 0;
beforeEach(() => {
fooInternal++;
return { foo: fooInternal };
});
test('foo 1', ({ foo }) => {
expect(foo).toBe(1);
});
test('foo 2', ({ foo }) => {
expect(foo).toBe(2);
});
}
);