Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix unit test format validation #598

Merged
merged 7 commits into from
Nov 4, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[submodule "rum-events-format"]
path = rum-events-format
url = https://github.com/DataDog/rum-events-format
branch = master
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"packages/*"
],
"scripts": {
"postinstall": "git submodule update --init",
"postinstall": "git submodule update --init && git submodule update --remote",
"build": "lerna run build --stream",
"build:bundle": "lerna run build:bundle --stream",
"format": "prettier --check \"**/*.{ts,js,json,md,yml,html}\"",
Expand Down
125 changes: 87 additions & 38 deletions packages/rum/src/domain/assemblyV2.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Context } from '@datadog/browser-core'
import { createRawRumEvent } from '../../test/createRawRumEvent'
import { setup, TestSetupBuilder } from '../../test/specHelper'
import { RawRumEventV2, RumEventType } from '../typesV2'
import { RumEventType } from '../typesV2'
import { LifeCycle, LifeCycleEventType } from './lifeCycle'

interface ServerRumEvents {
Expand Down Expand Up @@ -37,21 +38,6 @@ describe('rum assembly v2', () => {
let isTracked: boolean
let viewSessionId: string | undefined

function generateRawRumEvent(
type: RumEventType,
properties?: Partial<RawRumEventV2>,
savedGlobalContext?: Context,
customerContext?: Context
) {
const event = { type, ...properties }
lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, {
customerContext,
savedGlobalContext,
rawRumEvent: event as RawRumEventV2,
startTime: 0,
})
}

beforeEach(() => {
isTracked = true
viewSessionId = '1234'
Expand Down Expand Up @@ -93,28 +79,40 @@ describe('rum assembly v2', () => {

describe('events', () => {
it('should have snake cased attributes', () => {
generateRawRumEvent(RumEventType.LONG_TASK, { longTask: { duration: 2 } })
lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, {
rawRumEvent: createRawRumEvent(RumEventType.LONG_TASK, { longTask: { duration: 2 } }),
startTime: 0,
})

expect(serverRumEvents[0].long_task!.duration).toBe(2)
})
})

describe('rum context', () => {
it('should be merged with event attributes', () => {
generateRawRumEvent(RumEventType.VIEW)
lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, {
rawRumEvent: createRawRumEvent(RumEventType.VIEW, undefined),
startTime: 0,
})

expect(serverRumEvents[0].view.id).toBeDefined()
expect(serverRumEvents[0].date).toBeDefined()
})

it('should be snake cased', () => {
generateRawRumEvent(RumEventType.VIEW)
lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, {
rawRumEvent: createRawRumEvent(RumEventType.VIEW, undefined),
startTime: 0,
})

expect(serverRumEvents[0]._dd.format_version).toBe(2)
})

it('should be overwritten by event attributes', () => {
generateRawRumEvent(RumEventType.VIEW, { date: 10 })
lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, {
rawRumEvent: createRawRumEvent(RumEventType.VIEW, { date: 10 }),
startTime: 0,
})

expect(serverRumEvents[0].date).toBe(10)
})
Expand All @@ -123,33 +121,49 @@ describe('rum assembly v2', () => {
describe('rum global context', () => {
it('should be merged with event attributes', () => {
setGlobalContext({ bar: 'foo' })
generateRawRumEvent(RumEventType.VIEW)
lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, {
rawRumEvent: createRawRumEvent(RumEventType.VIEW),
startTime: 0,
})

expect((serverRumEvents[0].context as any).bar).toEqual('foo')
})

it('should ignore subsequent context mutation', () => {
const globalContext = { bar: 'foo' }
setGlobalContext(globalContext)
generateRawRumEvent(RumEventType.VIEW)
lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, {
rawRumEvent: createRawRumEvent(RumEventType.VIEW),
startTime: 0,
})
delete globalContext.bar
generateRawRumEvent(RumEventType.VIEW)
lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, {
rawRumEvent: createRawRumEvent(RumEventType.VIEW),
startTime: 0,
})

expect((serverRumEvents[0].context as any).bar).toEqual('foo')
expect((serverRumEvents[1].context as any).bar).toBeUndefined()
})

it('should not be automatically snake cased', () => {
setGlobalContext({ fooBar: 'foo' })
generateRawRumEvent(RumEventType.VIEW)
lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, {
rawRumEvent: createRawRumEvent(RumEventType.VIEW),
startTime: 0,
})

expect((serverRumEvents[0].context as any).fooBar).toEqual('foo')
})

it('should ignore the current global context when a saved global context is provided', () => {
setGlobalContext({ replacedContext: 'b', addedContext: 'x' })

generateRawRumEvent(RumEventType.VIEW, undefined, { replacedContext: 'a' })
lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, {
rawRumEvent: createRawRumEvent(RumEventType.VIEW),
savedGlobalContext: { replacedContext: 'a' },
startTime: 0,
})

expect((serverRumEvents[0].context as any).replacedContext).toEqual('a')
expect((serverRumEvents[0].context as any).addedContext).toEqual(undefined)
Expand All @@ -158,13 +172,21 @@ describe('rum assembly v2', () => {

describe('customer context', () => {
it('should be merged with event attributes', () => {
generateRawRumEvent(RumEventType.VIEW, undefined, undefined, { foo: 'bar' })
lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, {
customerContext: { foo: 'bar' },
rawRumEvent: createRawRumEvent(RumEventType.VIEW),
startTime: 0,
})

expect((serverRumEvents[0].context as any).foo).toEqual('bar')
})

it('should not be automatically snake cased', () => {
generateRawRumEvent(RumEventType.VIEW, undefined, undefined, { fooBar: 'foo' })
lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, {
customerContext: { fooBar: 'foo' },
rawRumEvent: createRawRumEvent(RumEventType.VIEW),
startTime: 0,
})

expect((serverRumEvents[0].context as any).fooBar).toEqual('foo')
})
Expand All @@ -173,21 +195,36 @@ describe('rum assembly v2', () => {
describe('action context', () => {
it('should be added on some event categories', () => {
;[RumEventType.RESOURCE, RumEventType.LONG_TASK, RumEventType.ERROR].forEach((category) => {
generateRawRumEvent(category)
expect(serverRumEvents[0].action.id).toBeDefined()
lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, {
rawRumEvent: createRawRumEvent(category),
startTime: 0,
})
expect(serverRumEvents[0].action).toEqual({ id: '7890' })
serverRumEvents = []
})
;[RumEventType.VIEW, RumEventType.ACTION].forEach((category) => {
generateRawRumEvent(category)
expect(serverRumEvents[0].action).not.toBeDefined()
serverRumEvents = []

lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, {
rawRumEvent: createRawRumEvent(RumEventType.VIEW),
startTime: 0,
})
expect(serverRumEvents[0].action).not.toBeDefined()
serverRumEvents = []

lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, {
rawRumEvent: createRawRumEvent(RumEventType.ACTION),
startTime: 0,
})
expect(serverRumEvents[0].action.id).not.toBeDefined()
serverRumEvents = []
})
})

describe('view context', () => {
it('should be merged with event attributes', () => {
generateRawRumEvent(RumEventType.ACTION)
lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, {
rawRumEvent: createRawRumEvent(RumEventType.ACTION),
startTime: 0,
})
expect(serverRumEvents[0].view).toEqual({
id: 'abcde',
referrer: 'url',
Expand All @@ -201,28 +238,40 @@ describe('rum assembly v2', () => {
it('when tracked, it should generate event', () => {
isTracked = true

generateRawRumEvent(RumEventType.VIEW)
lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, {
rawRumEvent: createRawRumEvent(RumEventType.VIEW),
startTime: 0,
})
expect(serverRumEvents.length).toBe(1)
})

it('when not tracked, it should not generate event', () => {
isTracked = false

generateRawRumEvent(RumEventType.VIEW)
lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, {
rawRumEvent: createRawRumEvent(RumEventType.VIEW),
startTime: 0,
})
expect(serverRumEvents.length).toBe(0)
})

it('when view context has session id, it should generate event', () => {
viewSessionId = '1234'

generateRawRumEvent(RumEventType.VIEW)
lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, {
rawRumEvent: createRawRumEvent(RumEventType.VIEW),
startTime: 0,
})
expect(serverRumEvents.length).toBe(1)
})

it('when view context has no session id, it should not generate event', () => {
viewSessionId = undefined

generateRawRumEvent(RumEventType.VIEW)
lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, {
rawRumEvent: createRawRumEvent(RumEventType.VIEW),
startTime: 0,
})
expect(serverRumEvents.length).toBe(0)
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('actionCollection v2', () => {
resourceCount: 10,
},
duration: 100,
id: 'xxx',
id: 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee',
name: 'foo',
startTime: 1234,
type: ActionType.CLICK,
Expand All @@ -111,7 +111,7 @@ describe('actionCollection v2', () => {
error: {
count: 10,
},
id: 'xxx',
id: 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee',
loadingTime: 100 * 1e6,
longTask: {
count: 10,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('resourceCollection', () => {
category: RumEventCategory.RESOURCE,
},
http: {
performance: jasmine.anything() as any,
performance: undefined,
url: 'https://resource.com/valid',
},
network: {
Expand Down Expand Up @@ -258,10 +258,7 @@ describe('resourceCollection V2', () => {
expect(rawRumEventsV2[0].rawRumEvent).toEqual({
date: (jasmine.any(Number) as unknown) as number,
resource: {
download: jasmine.anything(),
duration: 100 * 1e6,
firstByte: jasmine.anything(),
redirect: jasmine.anything(),
size: undefined,
type: ResourceType.OTHER,
url: 'https://resource.com/valid',
Expand Down Expand Up @@ -400,13 +397,13 @@ describe('resourceCollection V2', () => {
lifeCycle.notify(
LifeCycleEventType.PERFORMANCE_ENTRY_COLLECTED,
createResourceEntry({
traceId: 'xxx',
traceId: '1234',
})
)

const traceInfo = (rawRumEventsV2[0].rawRumEvent as RumResourceEventV2)._dd!
expect(traceInfo).toBeDefined()
expect(traceInfo.traceId).toBe('xxx')
expect(traceInfo.traceId).toBe('1234')
})

it('should be processed from completed request', () => {
Expand All @@ -429,9 +426,21 @@ describe('resourceCollection V2', () => {

function createResourceEntry(details?: Partial<RumPerformanceResourceTiming>): RumPerformanceResourceTiming {
const entry: Partial<RumPerformanceResourceTiming> = {
connectEnd: 0,
connectStart: 0,
decodedBodySize: 0,
domainLookupEnd: 0,
domainLookupStart: 0,
duration: 100,
entryType: 'resource',
fetchStart: 0,
name: 'https://resource.com/valid',
redirectEnd: 0,
redirectStart: 0,
requestStart: 0,
responseEnd: 0,
responseStart: 0,
secureConnectionStart: 0,
startTime: 1234,
...details,
}
Expand Down
Loading