-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.ts
76 lines (66 loc) · 1.84 KB
/
config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import NodeHttpAdapter from '@pollyjs/adapter-node-http'
import { PollyConfig } from '@pollyjs/core'
import FSPersister from '@pollyjs/persister-fs'
import merge from 'lodash.merge'
import path from 'node:path'
import { POLLY_MODE, POLLY_RECORD_IF_MISSING } from './environment'
export type Secrets = Record<string, string> | (string | undefined)[]
interface PollyConfigWithSecrets extends PollyConfig {
/**
* Secrets to filter out before persisting the recording.
*
* Format:
*
* ```ts
* secrets: {
* 'my secret data': 'replace with'
* }
* ```
*
* or just use an array of secrets and they will be replaced with "x":
*
* ```ts
* secrets: [process.env.SECRET, 'my secret data', 'another secret']
* ```
*/
secrets?: Secrets
}
export class JestPollyConfigService {
private $config!: PollyConfigWithSecrets
// eslint-disable-next-line class-methods-use-this
private factory(): PollyConfigWithSecrets {
const recordingsRoot = path.dirname(expect.getState().testPath ?? process.cwd())
const recordingsDirectory = path.join(recordingsRoot, '__recordings__')
return {
adapters: [NodeHttpAdapter],
persister: FSPersister,
persisterOptions: {
keepUnusedRequests: false,
fs: {
recordingsDir: recordingsDirectory,
},
},
mode: POLLY_MODE,
recordIfMissing: POLLY_RECORD_IF_MISSING,
recordFailedRequests: true,
matchRequestsBy: {
headers: false,
body: false,
},
}
}
private init() {
if (!this.$config) {
this.$config = this.factory()
}
}
get config(): PollyConfigWithSecrets {
this.init()
return this.$config
}
set config(config: PollyConfigWithSecrets) {
this.init()
merge(this.$config, config)
}
}
export const jestPollyConfigService = new JestPollyConfigService()