Skip to content

Commit

Permalink
Adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mjhenkes committed May 31, 2023
1 parent 33323fc commit bce60ad
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 15 deletions.
4 changes: 2 additions & 2 deletions .circleci/workflows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ commands:
name: Run tests using browser "<<parameters.browser>>"
working_directory: /tmp/<<parameters.repo>>/<<parameters.folder>>
command: |
<<parameters.command>> --browser <<parameters.browser>>
<<parameters.command>> --browser <<parameters.browser>> --record false
- unless:
condition: <<parameters.browser>>
steps:
Expand All @@ -839,7 +839,7 @@ commands:
- run:
name: Run tests using browser "<<parameters.browser>>"
working_directory: /tmp/<<parameters.repo>>
command: <<parameters.command>> --browser <<parameters.browser>>
command: <<parameters.command>> --browser <<parameters.browser>> --record false
- unless:
condition: <<parameters.browser>>
steps:
Expand Down
12 changes: 5 additions & 7 deletions packages/telemetry/src/span-exporters/cloud-span-exporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,11 @@ export class OTLPTraceExporter extends OTLPTraceExporterHttp {
}

abortDelayedItems () {
if (this.headers.Authorization) {
this.delayedItemsToExport.forEach((item) => {
item.onError(new Error('Spans cannot be sent, exporter has unmet requirements, either project id or record key are undefined.'))
})
this.delayedItemsToExport.forEach((item) => {
item.onError(new Error('Spans cannot be sent, exporter has unmet requirements, either project id or record key are undefined.'))
})

this.delayedItemsToExport = []
}
this.delayedItemsToExport = []
}

/**
Expand All @@ -138,7 +136,7 @@ export class OTLPTraceExporter extends OTLPTraceExporterHttp {
return
}

if (this.enc && this.requirementsToExport === 'unmet') {
if (this.requirementsToExport === 'unmet') {
onError(new Error('Spans cannot be sent, exporter has unmet requirements, either project id or record key are undefined.'))
}

Expand Down
72 changes: 66 additions & 6 deletions packages/telemetry/test/span-exporters/cloud-span-exporter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ describe('cloudSpanExporter', () => {
const exporter = new OTLPTraceExporter(genericRequest)

expect(exporter.headers['x-cypress-encrypted']).to.equal('1')
expect(exporter.requirementsToExport).to.equal('unknown')
expect(exporter.enc).to.not.be.undefined
})

it('does not set encrypted header if not set', () => {
const exporter = new OTLPTraceExporter()

expect(exporter.headers['x-cypress-encrypted']).to.be.undefined
expect(exporter.requirementsToExport).to.equal('met')
expect(exporter.enc).to.be.undefined
})
})
Expand All @@ -42,15 +44,20 @@ describe('cloudSpanExporter', () => {
expect(callCount).to.equal(1)
})

it('does nothing if id is not passed', () => {
const exporter = new OTLPTraceExporter()
it('sets requirements to unmet if id is not passed', () => {
const exporter = new OTLPTraceExporter(genericRequest)

let callCount = 0
let abortCallCount = 0

exporter.setAuthorizationHeader = () => {
callCount++
}

exporter.abortDelayedItems = () => {
abortCallCount++
}

expect(exporter.headers['x-project-id']).to.be.undefined
expect(exporter.projectId).to.be.undefined

Expand All @@ -59,6 +66,9 @@ describe('cloudSpanExporter', () => {
expect(exporter.headers['x-project-id']).to.be.undefined
expect(exporter.projectId).to.be.undefined
expect(callCount).to.equal(0)

expect(exporter.requirementsToExport).to.equal('unmet')
expect(abortCallCount).to.equal(1)
})
})

Expand All @@ -80,21 +90,29 @@ describe('cloudSpanExporter', () => {
expect(callCount).to.equal(1)
})

it('does nothing if record key is not passed', () => {
const exporter = new OTLPTraceExporter()
it('sets requirements to unmet if record key is not passed', () => {
const exporter = new OTLPTraceExporter(genericRequest)

let callCount = 0
let abortCallCount = 0

exporter.setAuthorizationHeader = () => {
callCount++
}

exporter.abortDelayedItems = () => {
abortCallCount++
}

expect(exporter.recordKey).to.be.undefined

exporter.attachRecordKey(undefined)

expect(exporter.recordKey).to.be.undefined
expect(callCount).to.equal(0)

expect(exporter.requirementsToExport).to.equal('unmet')
expect(abortCallCount).to.equal(1)
})
})

Expand All @@ -109,10 +127,9 @@ describe('cloudSpanExporter', () => {

const authorization = exporter.headers.Authorization

console.log('auth', authorization)

// MTIzOjQ1Ng== is 123:456 base64 encoded
expect(authorization).to.equal(`Basic MTIzOjQ1Ng==`)
expect(exporter.requirementsToExport).to.equal('met')
})
})

Expand Down Expand Up @@ -206,6 +223,24 @@ describe('cloudSpanExporter', () => {
})
})

describe('abortDelayedItems', () => {
it('aborts any delayed items', (done) => {
const exporter = new OTLPTraceExporter()

exporter.delayedItemsToExport.push({
serviceRequest: 'req',
onSuccess: () => {},
onError: (error) => {
expect(error.message).to.equal('Spans cannot be sent, exporter has unmet requirements, either project id or record key are undefined.')
done()
},
})

exporter.abortDelayedItems()
expect(exporter.delayedItemsToExport.length).to.equal(0)
})
})

describe('send', () => {
it('returns if shutdownOnce.isCalled is true', () => {
const exporter = new OTLPTraceExporter()
Expand Down Expand Up @@ -395,5 +430,30 @@ describe('cloudSpanExporter', () => {
expect(exporter.delayedItemsToExport.length).to.equal(1)
expect(exporter.delayedItemsToExport[0].serviceRequest).to.equal('string')
})

it('errors if requirements are unmet', (done) => {
const exporter = new OTLPTraceExporter()

exporter.requirementsToExport = 'unmet'

exporter.convert = (objects) => {
throw 'convert should not be called'
}

exporter.sendWithHttp = (collector, body, contentType, resolve, reject) => {
throw 'sendWithHttp should not be called'
}

const onSuccess = () => {
throw 'onSuccess should not be called'
}

const onError = (error) => {
expect(error.message).to.equal('Spans cannot be sent, exporter has unmet requirements, either project id or record key are undefined.')
done()
}

exporter.send('string', onSuccess, onError)
})
})
})

1 comment on commit bce60ad

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on bce60ad May 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.13.1/linux-x64/matth/chore/add-telemetry-realworld-app-bce60adaefca10b8e1eb33453200f80f9e0e8e0a/cypress.tgz

Please sign in to comment.