Skip to content

Commit

Permalink
Add infrastructure for mocked tests (#1684)
Browse files Browse the repository at this point in the history
* Add infrastructure for mocked tests

* build
  • Loading branch information
pakrym-stripe committed Feb 10, 2023
1 parent fd9e8cb commit ec2b23f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
5 changes: 3 additions & 2 deletions lib/stripe.common.js

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

16 changes: 11 additions & 5 deletions src/stripe.common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,15 @@ import CryptoProvider = require('./crypto/CryptoProvider');
import PlatformFunctions = require('./platform/PlatformFunctions');
import createWebhooks = require('./Webhooks');

function createStripe(platformFunctions: PlatformFunctions): typeof Stripe {
type RequestSenderFactory = (stripe: StripeObject) => RequestSender;

const defaultRequestSenderFactory: RequestSenderFactory = (stripe) =>
new RequestSender(stripe, StripeResource.MAX_BUFFERED_REQUEST_METRICS);

function createStripe(
platformFunctions: PlatformFunctions,
requestSender: RequestSenderFactory = defaultRequestSenderFactory
): typeof Stripe {
Stripe.PACKAGE_VERSION = require('../package.json').version;
Stripe.USER_AGENT = {
bindings_version: Stripe.PACKAGE_VERSION,
Expand Down Expand Up @@ -137,10 +145,8 @@ function createStripe(platformFunctions: PlatformFunctions): typeof Stripe {
this._prevRequestMetrics = [];
this._enableTelemetry = props.telemetry !== false;

this._requestSender = new RequestSender(
this,
Stripe.StripeResource.MAX_BUFFERED_REQUEST_METRICS
);
this._requestSender = requestSender(this);

// Expose StripeResource on the instance too
// @ts-ignore
this.StripeResource = Stripe.StripeResource;
Expand Down
34 changes: 27 additions & 7 deletions testUtils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const http = require('http');
const CryptoProvider = require('../lib/crypto/CryptoProvider');
const NodePlatformFunctions = require('../lib/platform/NodePlatformFunctions');
const RequestSender = require('../lib/RequestSender');
const {createStripe} = require('../lib/stripe.common');
const stripe = require('../lib/stripe.node');

const testingHttpAgent = new http.Agent({keepAlive: false});

Expand Down Expand Up @@ -97,15 +99,33 @@ const utils = (module.exports = {

// Provide a testable stripe instance
// That is, with mock-requests built in and hookable
const stripe = require('../lib/stripe.node');
const stripeInstance = stripe('fakeAuthToken', config);

stripeInstance._requestSender = new MockRequestSender(
stripeInstance,
stripe.StripeResource.MAX_BUFFERED_REQUEST_METRICS
const stripeFactory = createStripe(
new NodePlatformFunctions(),
(stripeInstance) =>
new MockRequestSender(
stripeInstance,
stripe.StripeResource.MAX_BUFFERED_REQUEST_METRICS
)
);
return stripeFactory('fakeAuthToken', config);
},

return stripeInstance;
createMockClient: (requests) => {
return utils.getMockStripe(
{},
(method, host, path, _4, _5, _6, callback) => {
const request = requests.find(
(r) => r.method == method && r.path == path
);
if (!request) {
throw new Error(
`Unable to find a mock request for ${method} ${path}`
);
}

callback(null, Promise.resolve(JSON.parse(request.response)));
}
);
},

getSpyableStripe: (config) => {
Expand Down

0 comments on commit ec2b23f

Please sign in to comment.