jest-with-context
Advanced tools
Comparing version 1.0.0 to 1.1.0
{ | ||
"name": "jest-with-context", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"version": "1.1.0", | ||
"main": "dist/src/index.js", | ||
"repository": "git@github.com:EduardoRFS/jest-with-context.git", | ||
@@ -6,0 +6,0 @@ "author": "EduardoRFS <theeduardorfs@gmail.com>", |
@@ -36,1 +36,39 @@ ## Usage, beautiful with prettier | ||
``` | ||
## Lifecycle | ||
```typescript | ||
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 }) => { | ||
let fooInternal = 0; | ||
beforeEach(() => { | ||
fooInternal++; | ||
return { foo: fooInternal }; | ||
}); | ||
test('foo 1', ({ foo }) => { | ||
expect(foo).toBe(1); | ||
}); | ||
test('foo 2', ({ foo }) => { | ||
expect(foo).toBe(2); | ||
}); | ||
} | ||
); | ||
``` |
@@ -1,3 +0,3 @@ | ||
type ItCallback<T extends object> = (context: T) => void | Promise<void>; | ||
interface ContextIt<T extends object> { | ||
type ItCallback<T> = (context: T) => void | Promise<void>; | ||
interface ContextIt<T> { | ||
(name: string, fn?: ItCallback<T>, timeout?: number): void; | ||
@@ -9,11 +9,17 @@ only: ContextIt<T>; | ||
} | ||
type DescribeCallback<T extends object> = ( | ||
context: T & { test: ContextIt<T> } | ||
type Lifecycle<T> = ( | ||
fn: (context: T) => void | Promise<void> | T | Promise<T>, | ||
timeout?: number | ||
) => any; | ||
type DescribeCallback<T> = ( | ||
context: T & { | ||
test: ContextIt<T>; | ||
beforeAll: Lifecycle<T>; | ||
beforeEach: Lifecycle<T>; | ||
afterAll: Lifecycle<T>; | ||
afterEach: Lifecycle<T>; | ||
} | ||
) => void; | ||
export const withContext = <T extends object>( | ||
name: string, | ||
context: T, | ||
cb: DescribeCallback<T> | ||
) => { | ||
const createContextIt = <T>(context: () => T) => { | ||
const types = ['only', 'skip', 'todo', 'concurrent'] as const; | ||
@@ -23,3 +29,3 @@ const createTest = (type?: typeof types[number]): ContextIt<T> => { | ||
return ((name, fn, timeout) => { | ||
testFn(name, fn && (() => fn(context)), timeout); | ||
testFn(name, fn && (() => fn(context())), timeout); | ||
}) as ContextIt<T>; | ||
@@ -29,6 +35,47 @@ }; | ||
const [only, skip, todo, concurrent] = types.map(createTest); | ||
const tests = { only, skip, todo, concurrent }; | ||
const contextTest = Object.assign(createTest(), tests); | ||
return Object.assign(createTest(), { only, skip, todo, concurrent }); | ||
}; | ||
const createLifecycles = <T>( | ||
get: () => T, | ||
set: (context: T) => void | ||
): Lifecycle<T>[] => { | ||
const types = [beforeAll, beforeEach, afterAll, afterEach] as const; | ||
return types.map(type => (fn, timeout) => | ||
type(async () => { | ||
const context = get(); | ||
const value = await fn(context); | ||
if (value) { | ||
set(value); | ||
} | ||
}, timeout) | ||
); | ||
}; | ||
return describe(name, () => cb({ ...context, test: contextTest })); | ||
export const withContext = <T extends object>( | ||
name: string, | ||
context: T | DescribeCallback<T>, | ||
cb?: DescribeCallback<T> | ||
) => { | ||
const callback = cb || (context as DescribeCallback<T>); | ||
let state: T = (typeof context !== 'function' ? context : undefined) as T; | ||
const set = (newContext: T) => (state = newContext); | ||
const get = () => state; | ||
const contextTest = createContextIt(get); | ||
const [beforeAll, beforeEach, afterAll, afterEach] = createLifecycles( | ||
get, | ||
set | ||
); | ||
return describe(name, () => | ||
callback({ | ||
...state, | ||
test: contextTest, | ||
beforeAll, | ||
beforeEach, | ||
afterAll, | ||
afterEach, | ||
}) | ||
); | ||
}; | ||
@@ -35,0 +82,0 @@ export const createContext = <T extends object>(context: T) => ( |
{ | ||
"include": ["src", "protos", "tests"], | ||
"include": ["src", "tests"], | ||
"compilerOptions": { | ||
@@ -9,7 +9,7 @@ /* Basic Options */ | ||
// "lib": [], /* Specify library files to be included in the compilation. */ | ||
"allowJs": true /* Allow javascript files to be compiled. */, | ||
// "allowJs": true /* Allow javascript files to be compiled. */, | ||
// "checkJs": true, /* Report errors in .js files. */ | ||
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ | ||
// "declaration": true, /* Generates corresponding '.d.ts' file. */ | ||
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ | ||
"declaration": true /* Generates corresponding '.d.ts' file. */, | ||
"declarationMap": true /* Generates a sourcemap for each corresponding '.d.ts' file. */, | ||
"sourceMap": true /* Generates corresponding '.map' file. */, | ||
@@ -16,0 +16,0 @@ // "outFile": "./", /* Concatenate and emit output to single file. */ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
48751
25
320
74
1