Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const nock = require('nock')
const dbs = require('../../lib/dbs')
const removeIfExists = require('../helpers/remove-if-exists')
const cancelStripeSubscription = require('../../jobs/cancel-stripe-subscription')
nock.disableNetConnect()
nock.enableNetConnect('localhost')
afterAll(async () => {
const { payments } = await dbs()
await Promise.all([
removeIfExists(payments, '123')
])
})
test('Cancel Stripe Subscription', async () => {
const { payments } = await dbs()
expect.assertions(3)
nock('https://api.stripe.com/v1')
.delete('/subscriptions/345')
.reply(200, () => {
// Stripe called
if (!has_fixtures) try {
require(path.resolve(fp));
console.log('using recorded fixtures ' + fp);
has_fixtures = true;
} catch (e) {
nock.recorder.rec({
dont_print: true
});
} else {
has_fixtures = false;
fs.readdirSync(path.join(test_folder, fixtures_folder)).filter(function(filename) {
return filename.indexOf('fixtures-') === 0 && path.extname(filename) === ".js";
}).map(function(filename) {
return path.resolve(test_folder, fixtures_folder, filename);
}).forEach(fs.unlinkSync);
nock.recorder.rec({
dont_print: true
});
}
},
// saves our recording if fixtures didn't already exist
constructor(endpoint, debug = false, record = false) {
this.nockScope = nock(endpoint, { encodedQueryParams: true })
this.recording = record
if (record) {
nock.recorder.rec({
output_objects: true,
})
}
this.anyRequestSetUp = false
this.debug = debug
// ethers hard-codes this value, see https://github.com/ethers-io/ethers.js/issues/489
this._rpcRequestId = 42
this._noMatches = []
nock.emitter.on('no match', (clientRequestObject, options, body) => {
this._noMatches.push(body)
if (debug) {
if (!this.anyRequestSetUp) {
console.log(
new Error('No mocks have been set up, but a request was made!')
// Expectations
let google = nock('http://example.test')
.get('/')
.reply(200, 'Hello from Google!')
setTimeout(() => {
google.done() // will throw an assertion error if meanwhile a "GET http://example.test" was not performed.
}, 5000)
/// .isDone()
scope = nock('http://example.test')
.get('/')
.reply(200)
scope.isDone() // will return false
nock.isDone()
/// .cleanAll()
nock.cleanAll()
/// .persist()
scope = nock('http://example.test')
.persist()
.get('/')
.reply(200, 'Persisting all the way')
/// .pendingMocks()
strings = scope.pendingMocks()
strings = nock.pendingMocks()
if (!scope.isDone()) {
console.error('pending mocks: %j', scope.pendingMocks())
}
// mutation moveCard
nock('https://api.github.com')
.post('/graphql')
.reply(200, (uri, requestBody) => {
requestBody = JSON.parse(requestBody)
expect(requestBody.query).toContain('mutation moveCard')
expect(requestBody.variables.cardId).toBeTruthy()
expect(requestBody.variables.columnId).toBeTruthy()
})
}
// Receive a webhook event
await probot.receive({ name: eventName, payload })
if (!nock.isDone()) {
console.error(nock.pendingMocks())
expect(nock.isDone()).toEqual(true)
}
}
})
/// .cleanAll()
nock.cleanAll()
/// .persist()
scope = nock('http://example.test')
.persist()
.get('/')
.reply(200, 'Persisting all the way')
/// .pendingMocks()
strings = scope.pendingMocks()
strings = nock.pendingMocks()
if (!scope.isDone()) {
console.error('pending mocks: %j', scope.pendingMocks())
}
console.error('pending mocks: %j', nock.pendingMocks())
/// .activeMocks()
nock.activeMocks() // $ExpectType string[]
nock('http://example.test').activeMocks() // $ExpectType string[]
// Logging
google = nock('http://example.test').log(console.log)
// Restoring
nock.restore()
// Enable/Disable real HTTP request
nock.disableNetConnect()
nock.enableNetConnect()
// using a string
scope.isDone() // will return false
nock.isDone()
/// .cleanAll()
nock.cleanAll()
/// .persist()
scope = nock('http://example.test')
.persist()
.get('/')
.reply(200, 'Persisting all the way')
/// .pendingMocks()
strings = scope.pendingMocks()
strings = nock.pendingMocks()
if (!scope.isDone()) {
console.error('pending mocks: %j', scope.pendingMocks())
}
console.error('pending mocks: %j', nock.pendingMocks())
/// .activeMocks()
nock.activeMocks() // $ExpectType string[]
nock('http://example.test').activeMocks() // $ExpectType string[]
// Logging
google = nock('http://example.test').log(console.log)
// Restoring
nock.restore()
// Enable/Disable real HTTP request
it('preparePull (Github response)', async () => {
// Setup
const { nockDone } = await nock.back(
'GitRemoteHTTP - preparePull (Github response).json'
)
// Test
const remote = await GitRemoteHTTP.discover({
core: 'default',
service: 'git-upload-pack',
url: 'https://github.com/isomorphic-git/isomorphic-git',
headers: {}
})
expect(remote).toBeTruthy()
expect(remote.symrefs.size > 0)
expect(remote.symrefs.get('HEAD')).toBe('refs/heads/master')
// Teardown
nockDone()
})
it('preparePull (mock response)', async () => {
// Setup
const { nockDone } = await nock.back(
'GitRemoteHTTP - preparePull (mock response).json'
)
// Test
const remote = await GitRemoteHTTP.discover({
core: 'default',
service: 'git-upload-pack',
url: 'http://example.dev/test-GitRemoteHTTP',
headers: {}
})
expect(remote).toBeTruthy()
// Teardown
nockDone()
})
it('basic pull', async () => {
// Setup
const { nockDone } = await nock.back('pull - basic pull.json')
const { dir, gitdir } = await makeFixture('test-pull-client')
// Test
const desiredOid = '97c024f73eaab2781bf3691597bc7c833cb0e22f'
await pull({
dir,
gitdir,
ref: 'master',
fastForwardOnly: true
})
const oid = await resolveRef({
gitdir,
ref: 'master'
})
expect(oid).toEqual(desiredOid)
// Teardown
nockDone()