UNPKG

4.89 kBMarkdownView Raw
1# extra-jest
2
3Some extra functions to work with jest testing framework
4
5## Requirements
6
7* Node.js ≥ 8.9.0
8
9## APIs
10
11**NOTE:**
12 * `xjest` is an alias of module `extra-jest`
13 * `jsYaml` is an alias of module `js-yaml`
14 * `fsTreeUtils` is an alias of module `fs-tree-utils`
15
16### `xjest.snap.mkfn`
17
18```typescript
19type DumpFunction<X> = (x: X, options?: jsYaml.DumpOptions) => string
20type TestFunction<X> = (x: X, options?: jsYaml.DumpOptions) => () => void
21
22declare function mkfn<X> (fn: DumpFunction<X>, defaultOptions?: jsYaml.DumpOptions): TestFunction<X>
23```
24
25This function is used to created `xjest.snap.*` functions below.
26
27### `xjest.snap.*`
28
29```typescript
30declare function unsafe (x: any, options?: jsYaml.DumpOptions): () => void
31declare function safe (x: any, options?: jsYaml.DumpOptions): () => void
32declare function pureSafe (x: any, options?: jsYaml.DumpOptions): () => void
33declare function pureUnsafe (x: any, options?: jsYaml.DumpOptions): () => void
34declare function noRefs (x: any, options?: jsYaml.DumpOptions): () => void
35```
36
37Create jest snapshots but in YAML format.
38
39**Features:**
40 * `xjest.snap.unsafe` dumps JavaScript objects before writing to snapshot.
41 * `xjest.snap.safe` safely dumps JavaScript objects before writing to snapshot.
42 * `xjest.snap.pureUnsafe` is `xjest.snap.unsafe` without default options.
43 * `xjest.snap.pureSafe` is `xjest.snap.safe` without default options.
44 * `xjest.snap.noRefs` is `xjest.snap.unsafe` with self references.
45
46**Default Options:**
47
48```typescript
49namespace defaultOptions {
50 export const skipInvalid = true
51 export const sortKeys = true
52 export const noRefs = false
53 export const noCompatMode = true
54}
55```
56
57### `xjest.setupTeardown.base.createFactory`
58
59```typescript
60type PromiseFunc<X, Y> = (x: X) => Promise<Y>
61type SyncFunc<X, Y> = (x: X) => Y
62type SetupFunc<Y> = () => Promise<Y>
63type TeardownFunc<X> = (x: X) => Promise<void>
64type CalledFunc<X, Y> = PromiseFunc<X, Y>
65type SyncCalledFunc<X, Y> = SyncFunc<X, Y>
66type Tester = () => Promise<void>
67type AsyncTesterFactory<SM, MT> = (fn: CalledFunc<SM, MT>) => Tester
68type SyncTesterFactory<SM, MT> = (fn: SyncCalledFunc<SM, MT>) => Tester
69
70interface Config<SY, TX> {
71 readonly setup: SetupFunc<SY>
72 readonly teardown: TeardownFunc<TX>
73}
74
75type TesterFactory<SM, MT> = AsyncTesterFactory<SM, MT> & {
76 forAsync: AsyncTesterFactory<SM, MT>,
77 forSync: SyncTesterFactory<SM, MT>
78}
79
80declare function createFactory<SM, MT> (config: Config<SM, MT>): TesterFactory<SM, MT>
81```
82
83Creates a `TesterFactory<SM, MT>` from a `config: Config<SM, MT>`.
84
85**Parameters:**
86 * `config.setup`: A function that runs before main test.
87 * `config.teardown`: A function that runs after main test.
88
89**Returns:**
90 * A `TesterFactory<SM, MT>` is both a namespace and a function, it has the same effect as `TesterFactory<SM, MT>::forAsync`.
91 * A `TesterFactory::forAsync` is a function, it creates a `Tester` from `fn: CalledFunc<SM, MT>`.
92 * A `TesterFactory::forSync` is a function, it creates a `Tester` from `fn: SyncCalledFunc<SM, MT>`.
93 * A `Tester` can be passed into jest `test` functions as a test function (second argument).
94
95**Aliases:**
96 * `xjest.setupTeardown.createFactory`
97 * `xjest.setupTeardown.default`
98
99### `xjest.setupTeardown.virtualEnvironment.createFactory`
100
101```typescript
102interface Info {
103 readonly tree: Tree.Write.Node
104 readonly container: string
105 readonly previousWorkingDirectory: string
106}
107
108interface Factory {
109 readonly info: Info
110 readonly apply: base.TesterFactory<Info, void>
111}
112
113declare function createFactory (tree: fsTreeUtils.Tree.Write.Node, container?: string): Factory
114```
115
116**Parameters:**
117 * `tree`: Specifies directory structure of target directory.
118 * `container` (optional): Intended target directory, default to random name in `os.tmpdir()` folder.
119
120**Returns:**
121 * `Factory::info`: Contains some information that might be useful (see `interface Info`).
122 * `Factory::apply`: Takes an async function and returns a `Tester`.
123
124### `xjest.snapSpawn.snap`
125
126```typescript
127type SpawnFunc = (argv: string[], options: SpawnSyncOptions) => SpawnSyncReturns<string | Buffer>
128declare function snap (fn: SpawnFunc, argv?: string[], options?: SpawnSyncOptions): () => void
129```
130
131Create a function that creates a snapshot from result of process execution.
132
133**Parameters:**
134 * `fn`: A function that calls `spawnSync`.
135 * `argv` (optional): Additional arguments.
136 * `options` (optional): Options to pass to `spawnSync`.
137 * `snap` (optional): Snap function to use.
138
139**Returns:**
140 * A function that makes snapshots from process execution result.
141
142**Aliases:**
143 * `xjest.snapSpawn.snap.default`
144
145### `xjest.snapSpawn.snap.withCommand`
146
147Like `xjest.snapSpawn.snap` above but with `command: string` as the first parameter instead of `fn: SpawnFunc`.
148
149## License
150
151[MIT](https://git.io/vhaEz) © [Hoàng Văn Khải](https://github.com/KSXGitHub)