Skip to content

Commit 18abf81

Browse files
authored
Add disable setting to node (#827)
1 parent 57e06b5 commit 18abf81

File tree

5 files changed

+57
-9
lines changed

5 files changed

+57
-9
lines changed

packages/node/README.md

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -205,16 +205,13 @@ const appAnalytics = new Analytics({ writeKey: 'APP_WRITE_KEY' });
205205
```
206206
207207
## Development: Disabling Analytics for Tests
208-
- If you want to intercept / disable analytics for integration tests, you can use something like [nock](https://github.com/nock/nock)
208+
- Set `disable: true`.
209209
210210
```ts
211-
// Note: nock will _not_ work if polyfill fetch with something like undici, as nock uses the http module. Undici has its own interception method.
212-
import nock from 'nock'
213-
214-
nock('https://api.segment.io')
215-
.post('/v1/batch')
216-
.reply(201)
217-
.persist()
211+
const analytics = new Analytics({
212+
...
213+
disable: process.env.NODE_ENV === 'test'
214+
})
218215
```
219216
220217
@@ -251,7 +248,7 @@ const analytics = new Analytics({ writeKey: '<MY_WRITE_KEY>' })
251248
252249
Other Differences:
253250
254-
- The `enable` configuration option has been removed-- see "Disabling Analytics" section
251+
- The configuration option for disabling analytics has changed. `enable: false` -> `disable: true`
255252
- the `errorHandler` configuration option has been remove -- see "Error Handling" section
256253
- `flushAt` configuration option -> `maxEventsInBatch`.
257254
- `callback` call signature is different
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const fetcher = jest.fn()
2+
jest.mock('../lib/fetch', () => ({ fetch: fetcher }))
3+
4+
import { createTestAnalytics } from './test-helpers/create-test-analytics'
5+
6+
describe('disable', () => {
7+
it('should dispatch callbacks and emit an http request, even if disabled', async () => {
8+
const analytics = createTestAnalytics({
9+
disable: true,
10+
})
11+
const emitterCb = jest.fn()
12+
analytics.on('http_request', emitterCb)
13+
await new Promise((resolve) =>
14+
analytics.track({ anonymousId: 'foo', event: 'bar' }, resolve)
15+
)
16+
expect(emitterCb).toBeCalledTimes(1)
17+
})
18+
19+
it('should call fetch if disabled is false', async () => {
20+
const analytics = createTestAnalytics({
21+
disable: false,
22+
})
23+
await new Promise((resolve) =>
24+
analytics.track({ anonymousId: 'foo', event: 'bar' }, resolve)
25+
)
26+
expect(fetcher).toBeCalled()
27+
})
28+
it('should not call fetch if disabled is true', async () => {
29+
const analytics = createTestAnalytics({
30+
disable: true,
31+
})
32+
await new Promise((resolve) =>
33+
analytics.track({ anonymousId: 'foo', event: 'bar' }, resolve)
34+
)
35+
expect(fetcher).not.toBeCalled()
36+
})
37+
})

packages/node/src/app/analytics-node.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export class Analytics extends NodeEmitter implements CoreAnalytics {
4949
maxRetries: settings.maxRetries ?? 3,
5050
maxEventsInBatch: settings.maxEventsInBatch ?? 15,
5151
httpRequestTimeout: settings.httpRequestTimeout,
52+
disable: settings.disable,
5253
flushInterval,
5354
},
5455
this as NodeEmitter

packages/node/src/app/settings.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export interface AnalyticsSettings {
3030
* The maximum number of milliseconds to wait for an http request. Default: 10000
3131
*/
3232
httpRequestTimeout?: number
33+
/**
34+
* Disable the analytics library. All calls will be a noop. Default: false.
35+
*/
36+
disable?: boolean
3337
}
3438

3539
export const validateSettings = (settings: AnalyticsSettings) => {

packages/node/src/plugins/segmentio/publisher.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export interface PublisherProps {
2727
maxRetries: number
2828
writeKey: string
2929
httpRequestTimeout?: number
30+
disable?: boolean
3031
}
3132

3233
/**
@@ -44,6 +45,7 @@ export class Publisher {
4445
private _closeAndFlushPendingItemsCount?: number
4546
private _httpRequestTimeout: number
4647
private _emitter: NodeEmitter
48+
private _disable: boolean
4749
constructor(
4850
{
4951
host,
@@ -53,6 +55,7 @@ export class Publisher {
5355
flushInterval,
5456
writeKey,
5557
httpRequestTimeout,
58+
disable,
5659
}: PublisherProps,
5760
emitter: NodeEmitter
5861
) {
@@ -66,6 +69,7 @@ export class Publisher {
6669
path ?? '/v1/batch'
6770
)
6871
this._httpRequestTimeout = httpRequestTimeout ?? 10000
72+
this._disable = Boolean(disable)
6973
}
7074

7175
private createBatch(): ContextBatch {
@@ -209,6 +213,11 @@ export class Publisher {
209213
body: requestInit.body,
210214
})
211215

216+
if (this._disable) {
217+
clearTimeout(timeoutId)
218+
return batch.resolveEvents()
219+
}
220+
212221
const response = await fetch(this._url, requestInit)
213222

214223
clearTimeout(timeoutId)

0 commit comments

Comments
 (0)