diff --git a/packages/node/src/__tests__/graceful-shutdown-integration.test.ts b/packages/node/src/__tests__/graceful-shutdown-integration.test.ts index 3ed9045b3..d74522b8a 100644 --- a/packages/node/src/__tests__/graceful-shutdown-integration.test.ts +++ b/packages/node/src/__tests__/graceful-shutdown-integration.test.ts @@ -206,7 +206,7 @@ describe('Ability for users to exit without losing events', () => { expect(elapsedTime).toBeLessThan(100) const calls = _helpers.getFetchCalls() expect(calls.length).toBe(1) - //expect(calls[0].body.batch.length).toBe(2) + expect(JSON.parse(calls[0].body).batch.length).toBe(2) }) test('should wait to flush if close is called and an event has not made it to the segment.io plugin yet', async () => { @@ -238,7 +238,7 @@ describe('Ability for users to exit without losing events', () => { expect(elapsedTime).toBeLessThan(TRACK_DELAY * 2) const calls = _helpers.getFetchCalls() expect(calls.length).toBe(1) - //expect(calls[0].body.batch.length).toBe(2) + expect(JSON.parse(calls[0].body).batch.length).toBe(2) }) }) }) diff --git a/packages/node/src/__tests__/test-helpers/assert-shape/segment-http-api.ts b/packages/node/src/__tests__/test-helpers/assert-shape/segment-http-api.ts index 22d7fdc07..3d6022cfc 100644 --- a/packages/node/src/__tests__/test-helpers/assert-shape/segment-http-api.ts +++ b/packages/node/src/__tests__/test-helpers/assert-shape/segment-http-api.ts @@ -6,7 +6,9 @@ import { HTTPClientRequest } from '../../../lib/http-client' */ export const httpClientOptionsBodyMatcher = { messageId: expect.stringMatching(/^node-next-\d*-\w*-\w*-\w*-\w*-\w*/), - timestamp: expect.any(Date), + timestamp: expect.stringMatching( + /^20\d{2}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d+Z/ + ), _metadata: expect.any(Object), context: { library: { @@ -29,6 +31,5 @@ export function assertHTTPRequestOptions( "User-Agent": "analytics-node-next/latest", } `) - //expect(body).toHaveLength(contexts.length) - expect(body.length).toBeGreaterThan(contexts.length) // Just to make errors go away, please suggest something + expect(JSON.parse(body).batch).toHaveLength(contexts.length) } diff --git a/packages/node/src/__tests__/typedef-tests.ts b/packages/node/src/__tests__/typedef-tests.ts index 32128161c..f847c26c8 100644 --- a/packages/node/src/__tests__/typedef-tests.ts +++ b/packages/node/src/__tests__/typedef-tests.ts @@ -1,5 +1,5 @@ /* eslint-disable @typescript-eslint/no-var-requires */ -//import axios from 'axios' +import axios from 'axios' import { Analytics, Context, @@ -9,7 +9,7 @@ import { HTTPClient, FetchHTTPClient, HTTPFetchFn, - //HTTPClientRequest, + HTTPClientRequest, } from '../' /** @@ -104,17 +104,17 @@ export default { new Analytics({ writeKey: 'foo', httpClient: {} as BadFetch }) }, - // 'httpClient setting should be compatible with axios': () => { - // new (class implements HTTPClient { - // async makeRequest(options: HTTPClientRequest) { - // return axios({ - // url: options.url, - // method: options.method, - // data: options.body, - // headers: options.headers, - // timeout: options.httpRequestTimeout, - // }) - // } - // })() - // }, + 'httpClient setting should be compatible with axios': () => { + new (class implements HTTPClient { + async makeRequest(options: HTTPClientRequest) { + return axios({ + url: options.url, + method: options.method, + data: options.body, + headers: options.headers, + timeout: options.httpRequestTimeout, + }) + } + })() + }, } diff --git a/packages/node/src/plugins/segmentio/__tests__/methods.test.ts b/packages/node/src/plugins/segmentio/__tests__/methods.test.ts index 07e09efed..85f972413 100644 --- a/packages/node/src/plugins/segmentio/__tests__/methods.test.ts +++ b/packages/node/src/plugins/segmentio/__tests__/methods.test.ts @@ -6,7 +6,7 @@ import { Context } from '../../../app/context' import { Emitter } from '@segment/analytics-core' import { assertHTTPRequestOptions, - //httpClientOptionsBodyMatcher, + httpClientOptionsBodyMatcher, } from '../../../__tests__/test-helpers/assert-shape' import { TestFetchClient } from '../../../__tests__/test-helpers/create-test-analytics' @@ -52,16 +52,16 @@ test('alias', async () => { expect(fetcher).toHaveBeenCalledTimes(1) validateFetcherInputs(context) - // const [request] = fetcher.mock.lastCall - // const data = request.data - - // expect(data.batch).toHaveLength(1) - // expect(data.batch[0]).toEqual({ - // ...httpClientOptionsBodyMatcher, - // type: 'alias', - // previousId: 'from', - // userId: 'to', - // }) + const [request] = fetcher.mock.lastCall + const data = JSON.parse(request.body) + + expect(data.batch).toHaveLength(1) + expect(data.batch[0]).toEqual({ + ...httpClientOptionsBodyMatcher, + type: 'alias', + previousId: 'from', + userId: 'to', + }) }) test('group', async () => { @@ -82,19 +82,19 @@ test('group', async () => { expect(fetcher).toHaveBeenCalledTimes(1) validateFetcherInputs(context) - // const [request] = fetcher.mock.lastCall - // const data = request.data - - // expect(data.batch).toHaveLength(1) - // expect(data.batch[0]).toEqual({ - // ...httpClientOptionsBodyMatcher, - // traits: { - // name: 'libraries', - // }, - // type: 'group', - // groupId: 'foo-group-id', - // userId: 'foo-user-id', - // }) + const [request] = fetcher.mock.lastCall + const data = JSON.parse(request.body) + + expect(data.batch).toHaveLength(1) + expect(data.batch[0]).toEqual({ + ...httpClientOptionsBodyMatcher, + traits: { + name: 'libraries', + }, + type: 'group', + groupId: 'foo-group-id', + userId: 'foo-user-id', + }) }) test('identify', async () => { @@ -111,17 +111,18 @@ test('identify', async () => { expect(fetcher).toHaveBeenCalledTimes(1) validateFetcherInputs(context) - // const [request] = fetcher.mock.lastCall - // const data = request.data - // expect(data.batch).toHaveLength(1) - // expect(data.batch[0]).toEqual({ - // ...httpClientOptionsBodyMatcher, - // traits: { - // name: 'Chris Radek', - // }, - // type: 'identify', - // userId: 'foo-user-id', - // }) + const [request] = fetcher.mock.lastCall + const data = JSON.parse(request.body) + + expect(data.batch).toHaveLength(1) + expect(data.batch[0]).toEqual({ + ...httpClientOptionsBodyMatcher, + traits: { + name: 'Chris Radek', + }, + type: 'identify', + userId: 'foo-user-id', + }) }) test('page', async () => { @@ -141,21 +142,21 @@ test('page', async () => { expect(fetcher).toHaveBeenCalledTimes(1) validateFetcherInputs(context) - // const [request] = fetcher.mock.lastCall - // const data = request.data - - // expect(data.batch).toHaveLength(1) - // expect(data.batch[0]).toEqual({ - // ...httpClientOptionsBodyMatcher, - // type: 'page', - // userId: 'foo-user-id', - // name: 'Home', - // category: 'Category', - // properties: { - // category: 'Category', - // url: 'http://localhost', - // }, - // }) + const [request] = fetcher.mock.lastCall + const data = JSON.parse(request.body) + + expect(data.batch).toHaveLength(1) + expect(data.batch[0]).toEqual({ + ...httpClientOptionsBodyMatcher, + type: 'page', + userId: 'foo-user-id', + name: 'Home', + category: 'Category', + properties: { + category: 'Category', + url: 'http://localhost', + }, + }) }) test('screen', async () => { @@ -175,20 +176,20 @@ test('screen', async () => { expect(fetcher).toHaveBeenCalledTimes(1) validateFetcherInputs(context) - // const [request] = fetcher.mock.lastCall - // const data = request.data - - // expect(data.batch).toHaveLength(1) - // expect(data.batch[0]).toEqual({ - // ...httpClientOptionsBodyMatcher, - // type: 'screen', - // userId: 'foo-user-id', - // name: 'Home', - // category: 'Category', - // properties: { - // variation: 'local', - // }, - // }) + const [request] = fetcher.mock.lastCall + const data = JSON.parse(request.body) + + expect(data.batch).toHaveLength(1) + expect(data.batch[0]).toEqual({ + ...httpClientOptionsBodyMatcher, + type: 'screen', + userId: 'foo-user-id', + name: 'Home', + category: 'Category', + properties: { + variation: 'local', + }, + }) }) test('track', async () => { @@ -207,17 +208,17 @@ test('track', async () => { expect(fetcher).toHaveBeenCalledTimes(1) validateFetcherInputs(context) - // const [request] = fetcher.mock.lastCall - // const data = request.data - - // expect(data.batch).toHaveLength(1) - // expect(data.batch[0]).toEqual({ - // ...httpClientOptionsBodyMatcher, - // type: 'track', - // event: 'test event', - // userId: 'foo-user-id', - // properties: { - // foo: 'bar', - // }, - // }) + const [request] = fetcher.mock.lastCall + const data = JSON.parse(request.body) + + expect(data.batch).toHaveLength(1) + expect(data.batch[0]).toEqual({ + ...httpClientOptionsBodyMatcher, + type: 'track', + event: 'test event', + userId: 'foo-user-id', + properties: { + foo: 'bar', + }, + }) })