Skip to content

Commit

Permalink
Add a new network interception testing capability
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomer Ohana committed Aug 7, 2019
1 parent e087859 commit ec10a27
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 0 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,40 @@ const report = testkit.reports()[0]
expect(report).toHaveProperty(...)
```

## Network interception support
Instead of modifying your application code, you can use network interception libraries in conjunction with the testkit.\
Example with [nock](https://github.com/nock/nock):
```javascript

const nock = require('nock')
const sentryTestkit = require('sentry-testkit')
const { testkit, initNetworkInterceptor } = sentryTestkit()

beforeAll(() => {
const myAppDSN = '<your DSN goes here>'
initNetworkInterceptor(myAppDSN, (baseUrl, handleRequestBody) => {
// This callback is where we init our interceptor.
// The interceptor should intercept requests from `baseUrl` and pass the
// request body (as json) to the `handleRequestBody` function.
nock(baseUrl)
.persist()
.post(/.*/)
.reply(200, (_, requestBody) => {
handleRequestBody(requestBody)
})
})
})

test('findReport example', async function() {
const err = new Error('error to look for')

// Some faulty scenario that will report err

const report = testkit.findReport(err)
expect(report).toBeDefined()
})
```

## Yes! We Love [Puppeteer](https://pptr.dev/)
```javascript
const sentryTestkit = require('sentry-testkit')
Expand Down
1 change: 1 addition & 0 deletions docs/_sidebar.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
* [](/)
* [Getting Started](/getting-started/)
* [Network Interception Support](/api/network-interception.md)
* [Testkit API](/api/)
* Methods
* [findReport](/api/findReport.md)
Expand Down
35 changes: 35 additions & 0 deletions docs/api/network-interception.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
## Intro
Sentry testkit supports using network interception libraries, without the need to change your application's `Sentry.init` function. \
The testkit is agnostic to the interception library you use.

### Example using nock
```javascript

const nock = require('nock')
const sentryTestkit = require('sentry-testkit')
const { testkit, initNetworkInterceptor } = sentryTestkit()

beforeAll(() => {
const myAppDSN = '<your DSN goes here>'
initNetworkInterceptor(myAppDSN, (baseUrl, handleRequestBody) => {
// This callback is where we init our interceptor.
// The interceptor should intercept requests from `baseUrl` and pass the
// request body (as json) to the `handleRequestBody` function.
nock(baseUrl)
.persist()
.post(/.*/)
.reply(200, (_, requestBody) => {
handleRequestBody(requestBody)
})
})
})

test('findReport example', async function() {
const err = new Error('error to look for')

// Some faulty scenario that will report err

const report = testkit.findReport(err)
expect(report).toBeDefined()
})
```
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ declare namespace sentryTestkit {
sentryTransport: {
new (options: TransportOptions): Transport;
};
initNetworkInterceptor<T>(dsn: string, initCallback: (baseUrl: string, handleRequestBody: (requestBody: { [key: string]: any }) => void) => T): T
}
}
93 changes: 93 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"husky": "^3.0.2",
"jest": "^23.6.0",
"lint-staged": "^9.2.1",
"nock": "^10.0.6",
"prettier": "^1.18.2",
"wait-for-expect": "^1.1.0"
},
Expand Down
10 changes: 10 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict'

const { parseDsn } = require('./parseDsn')

function getException(report) {
return report.error
}
Expand Down Expand Up @@ -51,6 +53,14 @@ module.exports = () => {
sendEvent, // support for v5 API
}
},
initNetworkInterceptor: (dsn, cb) => {
const { protocol, host } = parseDsn(dsn)
const baseUrl = `${protocol}://${host}`
const handleRequestBody = requestBody =>
reports.push(transformReport(requestBody))

return cb(baseUrl, handleRequestBody)
},
testkit: {
puppeteer: {
startListening: page => {
Expand Down
20 changes: 20 additions & 0 deletions src/parseDsn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict'

const dsnKeys = 'source protocol user pass host port path'.split(' ')
const dsnPattern = /^(?:(\w+):)?\/\/(?:(\w+)(:\w+)?@)?([\w\.-]+)(?::(\d+))?(\/.*)/ //eslint-disable-line no-useless-escape

const parseDsn = dsn => {
const { protocol, host, path } = dsn.match(dsnPattern).reduce(
(parsed, current, index) =>
Object.assign({}, parsed, {
[dsnKeys[index]]: current,
}),
{}
)

const project = path.substr(path.lastIndexOf('/') + 1)

return { protocol, project, host }
}

module.exports.parseDsn = parseDsn
35 changes: 35 additions & 0 deletions test/network-interception.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const Sentry = require('@sentry/node')
const nock = require('nock')
const sentryTestkit = require('../src/index')
const { createCommonTests } = require('./commonTests')

const { testkit, initNetworkInterceptor } = sentryTestkit()
const DUMMY_DSN = 'https://acacaeaccacacacabcaacdacdacadaca@sentry.io/000001'
describe('sentry test-kit test suite - network interception', function() {
beforeAll(() => {
nock.cleanAll()
nock.disableNetConnect()

initNetworkInterceptor(DUMMY_DSN, (baseUrl, handleRequestBody) => {
nock(baseUrl)
.persist()
.post(/.*/)
.reply(200, (_, requestBody) => {
handleRequestBody(requestBody)
})
})

Sentry.init({
dsn: DUMMY_DSN,
release: 'test',
beforeSend(event) {
event.extra = { os: 'mac-os' }
return event
},
})
})

beforeEach(() => testkit.reset())

createCommonTests({ Sentry, testkit })
})

0 comments on commit ec10a27

Please sign in to comment.