Skip to content

Conversation

@perrin4869
Copy link
Collaborator

So @gdp88 was trying to use sinon to mock the sdk, but was encountering issues. The root cause is that the SDK was still compiling down to an old version of es and was producing:

export default class SDK extends PaymentsSDK {
    constructor() {
        super(...arguments);
        this.bankAccounts = new BankAccounts(this.api);
        this.cancels = new Cancels(this.api);
        this.captures = new Captures(this.api);
        this.charges = new Charges(this.api);
        this.checkoutInfo = new CheckoutInfo(this.api);
        this.ecInfo = new ECInfo(this.api);
        this.ecFormLinks = new ECFormLinks(this.api);
        this.ecForms = new ECForms(this.api);
        this.ecLinksProducts = new LinksProducts(this.api);
        this.emails = new Emails(this.api);
        this.exchangeRates = new ExchangeRates(this.api);
        this.ledgers = new Ledgers(this.api);
        this.merchants = new Merchants(this.api);
        this.platforms = new Platforms(this.api);
        this.products = new Products(this.api);
        this.refers = new Refers(this.api);
        this.refunds = new Refunds(this.api);
        this.stores = new Stores(this.api);
        this.subscriptions = new Subscriptions(this.api);
        this.transactionTokens = new TransactionTokens(this.api);
        this.transfers = new Transfers(this.api);
        this.verification = new Verification(this.api);
        this.webHooks = new WebHooks(this.api);
    }
}

instead of

export default class SDK extends PaymentsSDK {
    bankAccounts = new BankAccounts(this.api);
    cancels = new Cancels(this.api);
    captures = new Captures(this.api);
    charges = new Charges(this.api);
    checkoutInfo = new CheckoutInfo(this.api);
    ecInfo = new ECInfo(this.api);
    ecFormLinks = new ECFormLinks(this.api);
    ecForms = new ECForms(this.api);
    ecLinksProducts = new LinksProducts(this.api);
    emails = new Emails(this.api);
    exchangeRates = new ExchangeRates(this.api);
    ledgers = new Ledgers(this.api);
    merchants = new Merchants(this.api);
    platforms = new Platforms(this.api);
    products = new Products(this.api);
    refers = new Refers(this.api);
    refunds = new Refunds(this.api);
    stores = new Stores(this.api);
    subscriptions = new Subscriptions(this.api);
    transactionTokens = new TransactionTokens(this.api);
    transfers = new Transfers(this.api);
    verification = new Verification(this.api);
    webHooks = new WebHooks(this.api);
}

Setting the properties inside the contructor makes them invisible because they are not part of the prototype until the contructor is actually run. You can see here in this experiment I ran inside the repl:

Welcome to Node.js v20.11.0.
Type ".help" for more information.
> const { createStubInstance } = require('sinon');
undefined
> createStubInstance
[Function: createStubInstance]
> class Foo { bar() {} }
undefined
> const { stub } = require('sinon');
undefined
> const foo = createStubInstance(Foo, { bar: stub().returns("omg") })
undefined
> foo.bar()
'omg'
> class Foo2 { constructor() { this.bar = () => {} } }
undefined
> const foo2 = createStubInstance(Foo2, { bar: stub().returns("omg") })
Uncaught Error: Found no methods on object to which we could apply mutations
    at walkObject (/home/perrin4869/univapay/services-typescript/node_modules/sinon/lib/sinon/util/core/walk-object.js:47:15)
    at stub (/home/perrin4869/univapay/services-typescript/node_modules/sinon/lib/sinon/stub.js:103:16)
    at createStubInstance (/home/perrin4869/univapay/services-typescript/node_modules/sinon/lib/sinon/create-stub-instance.js:19:27)
    at createStubInstance (/home/perrin4869/univapay/services-typescript/node_modules/sinon/lib/sinon/sandbox.js:112:49)

This shouldn't really be a breaking change though, I kept the cjs version as before, any any system that's actually supported and running the esm version under node should also support the es2022 syntax, and allow us to test using sinon in the services repo

@coveralls
Copy link

coveralls commented Jan 30, 2024

Coverage Status

coverage: 99.433% (+0.003%) from 99.43%
when pulling c1298ab on chore/typescript-target-es2022
into 3e77f26 on master.

@perrin4869
Copy link
Collaborator Author

oops, actually these properties aren't good either:

> class Foo3 { bar = () => {} }
undefined
> const foo3 = createStubInstance(Foo3, { bar: stub().returns("omg") })
Uncaught Error: Found no methods on object to which we could apply mutations
    at walkObject (/home/perrin4869/univapay/services-typescript/node_modules/sinon/lib/sinon/util/core/walk-object.js:47:15)
    at stub (/home/perrin4869/univapay/services-typescript/node_modules/sinon/lib/sinon/stub.js:103:16)
    at createStubInstance (/home/perrin4869/univapay/services-typescript/node_modules/sinon/lib/sinon/create-stub-instance.js:19:27)
    at createStubInstance (/home/perrin4869/univapay/services-typescript/node_modules/sinon/lib/sinon/sandbox.js:112:49)
`

@perrin4869 perrin4869 closed this Jan 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants