Skip to content

Commit 6114e85

Browse files
authored
refactor node tests (#681)
1 parent 49945ac commit 6114e85

File tree

6 files changed

+55
-36
lines changed

6 files changed

+55
-36
lines changed

packages/node/src/__tests__/graceful-shutdown-integration.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ const testPlugin: CorePlugin = {
1818
describe('Ability for users to exit without losing events', () => {
1919
let ajs!: Analytics
2020
beforeEach(async () => {
21-
jest.resetAllMocks()
2221
fetcher.mockReturnValue(createSuccess())
2322
ajs = new Analytics({
2423
writeKey: 'abc123',

packages/node/src/__tests__/http-integration.test.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,16 @@ import { createSuccess } from './test-helpers/factories'
55
import { version } from '../../package.json'
66
import { Analytics } from '..'
77
import { resolveCtx } from './test-helpers/resolve-ctx'
8-
9-
const myDate = new Date('2016')
10-
const _Date = Date
8+
import { createTestAnalytics } from './test-helpers/create-test-analytics'
9+
import { isValidDate } from './test-helpers/is-valid-date'
1110

1211
describe('Analytics Node', () => {
1312
let ajs: Analytics
1413

1514
beforeEach(async () => {
16-
jest.resetAllMocks()
1715
fetcher.mockReturnValue(createSuccess())
1816

19-
ajs = new Analytics({
20-
flushInterval: 500,
21-
writeKey: 'abc123',
22-
})
23-
24-
// @ts-ignore
25-
global.Date = jest.fn(() => myDate)
26-
global.Date.UTC = _Date.UTC
27-
global.Date.parse = _Date.parse
28-
global.Date.now = _Date.now
17+
ajs = createTestAnalytics()
2918
})
3019

3120
test(`Fires an "identify" request with the expected data`, async () => {
@@ -57,9 +46,16 @@ describe('Analytics Node', () => {
5746
batch: [
5847
{
5948
messageId: expect.any(String),
49+
context: {
50+
library: {
51+
version: expect.any(String),
52+
},
53+
},
54+
6055
_metadata: {
6156
nodeVersion: expect.any(String),
6257
},
58+
timestamp: expect.any(String),
6359
},
6460
],
6561
},
@@ -73,12 +69,12 @@ describe('Analytics Node', () => {
7369
"context": Object {
7470
"library": Object {
7571
"name": "AnalyticsNode",
76-
"version": "${version}",
72+
"version": Any<String>,
7773
},
7874
},
7975
"integrations": Object {},
8076
"messageId": Any<String>,
81-
"timestamp": "2016-01-01T00:00:00.000Z",
77+
"timestamp": Any<String>,
8278
"traits": Object {
8379
"foo": "bar",
8480
},
@@ -89,6 +85,10 @@ describe('Analytics Node', () => {
8985
}
9086
`
9187
)
88+
89+
const event = body.batch[0]
90+
expect(event.context.library.version).toBe(version)
91+
expect(isValidDate(event.timestamp)).toBeTruthy()
9292
})
9393

9494
test('Track: Fires http requests to the correct endoint', async () => {

packages/node/src/__tests__/integration.test.ts

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,19 @@
11
const fetcher = jest.fn()
22
jest.mock('../lib/fetch', () => fetcher)
33

4-
import { Analytics } from '../index'
54
import { CorePlugin as Plugin } from '@segment/analytics-core'
65
import { resolveCtx } from './test-helpers/resolve-ctx'
76
import { testPlugin } from './test-helpers/test-plugin'
87
import { createSuccess, createError } from './test-helpers/factories'
9-
import { AnalyticsSettings } from '../app/settings'
8+
import { createTestAnalytics } from './test-helpers/create-test-analytics'
109

1110
const writeKey = 'foo'
1211
jest.setTimeout(10000)
1312

14-
const createTestAnalytics = (settings: Partial<AnalyticsSettings> = {}) => {
15-
return new Analytics({ writeKey, flushInterval: 100, ...settings })
16-
}
17-
1813
beforeEach(() => {
1914
fetcher.mockReturnValue(createSuccess())
2015
})
2116

22-
describe('Plugin Init', () => {
23-
it('loads analytics-node-next plugin', async () => {
24-
const analytics = createTestAnalytics()
25-
await analytics.ready
26-
27-
const ajsNodeXt = analytics.queue.plugins.find(
28-
(xt) => xt.name === 'Segment.io'
29-
)
30-
expect(ajsNodeXt).toBeDefined()
31-
expect(ajsNodeXt?.isLoaded()).toBeTruthy()
32-
})
33-
})
34-
3517
describe('Settings / Configuration Init', () => {
3618
it('throws if no writeKey', () => {
3719
expect(() =>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const fetcher = jest.fn()
2+
jest.mock('../lib/fetch', () => ({ fetch: fetcher }))
3+
4+
import { createSuccess } from './test-helpers/factories'
5+
import { createTestAnalytics } from './test-helpers/create-test-analytics'
6+
7+
describe('Plugins', () => {
8+
beforeEach(() => {
9+
fetcher.mockReturnValue(createSuccess())
10+
})
11+
12+
describe('Initialize', () => {
13+
it('loads analytics-node-next plugin', async () => {
14+
const analytics = createTestAnalytics()
15+
await analytics.ready
16+
17+
const ajsNodeXt = analytics.queue.plugins.find(
18+
(xt) => xt.name === 'Segment.io'
19+
)
20+
expect(ajsNodeXt).toBeDefined()
21+
expect(ajsNodeXt?.isLoaded()).toBeTruthy()
22+
})
23+
})
24+
})
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Analytics } from '../../app/analytics-node'
2+
import { AnalyticsSettings } from '../../app/settings'
3+
4+
export const createTestAnalytics = (
5+
settings: Partial<AnalyticsSettings> = {}
6+
) => {
7+
return new Analytics({ writeKey: 'foo', flushInterval: 100, ...settings })
8+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const isValidDate = (date: string) => {
2+
if (!date) {
3+
throw new Error('no date found.')
4+
}
5+
return !isNaN(Date.parse(date))
6+
}

0 commit comments

Comments
 (0)