Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): allow running adhoc functions in tests #385

Merged
merged 6 commits into from
Jul 7, 2021
Merged

Conversation

xavdid
Copy link
Contributor

@xavdid xavdid commented Jun 4, 2021

Addresses #16.

see also: PDE-2539

Todo

  • verify that TS works nicely
  • write unit tests

Testing

  1. With this PR cloned locally, run yarn link in the core directory
  2. elsewhere, run zapier init ad-hoc-test -t custom-auth
  3. run cd ad-hoc-test && yarn && yarn link zapier-platform-core

Now, appTester can be fed ad-hoc functions. Try the following test:

/* globals describe, it, expect */

const zapier = require("zapier-platform-core");

const App = require("../index");
const appTester = zapier.createAppTester(App);

describe("custom auth", () => {
  it("passes authentication and returns json", async () => {
    const bundle = {
      authData: {
        apiKey: "secret",
      },
    };

    const response = await appTester(
      (z, bundle) =>
        z.request({ url: "https://auth-json-server.zapier-staging.com/me" }),
      bundle    
    );
    expect(response.data).toHaveProperty("username");
  });
});

It's a simple test, but it proves a point: a test function can use z.request and take advantage of middleware.

@xavdid
Copy link
Contributor Author

xavdid commented Jun 4, 2021

@eliangcs this isn't ready for an actual review yet, but I figured it would be worth getting some early eyes just in case there's a reason we shouldn't pursue this.

@eliangcs
Copy link
Member

eliangcs commented Jun 7, 2021

@xavdid this is cool! But I wonder what prevents developers from doing this injection inside their code. In #16, @Lardpower mentioned they could inject the function in the test code:

let App = require('../../index');

// include any custom functions in any custom App field like "lib"
App.lib = {
    user: require('./lib/user.js')
}

...

// use it later during the test
appTester(App.lib.user.get).then(response => {});

I'm probably missing something. Does the above code have any problems?

@xavdid
Copy link
Contributor Author

xavdid commented Jun 8, 2021

That's a valid workaround, but it's sort of an antipattern to tell users to add invalid properties to their app. If it's behavior we want to support, then being able to call functions inline (pre-defined or otherwise) feels better. Feels more "above the board".

Does that make sense? Should I go ahead with finishing this PR?

@eliangcs
Copy link
Member

eliangcs commented Jun 8, 2021

@xavdid the feature request makes sense to me, but I don't think the adHoc flag is necessary. Could we make appTester automatically inject the function if it doesn't exist in the app definition? Something like:

    let method;
    try {
      method = resolveMethodPath(appRaw, methodOrFunc);
    } catch (err) {
      // We may need to let resolveMethodPath throw different error types to do this
      if (e instanceof MethodNotfound) {
        appRaw._testRequest = (z, bundle) => methodOrFunc(z, bundle);
        method = resolveMethodPath(appRaw, methodOrFunc);
      } else {
        throw err;
      }
    }

What do you think?

@xavdid
Copy link
Contributor Author

xavdid commented Jun 8, 2021

Sure, makes sense. I did it without a property in 67b48f6. We'll run any function we get, but it correctly throws errors if you give an object (or other allowed type) that isn't found.

@eliangcs
Copy link
Member

eliangcs commented Jun 8, 2021

@xavdid looking good! Let me know once this PR is ready for review.

@xavdid xavdid dismissed a stale review via 661fe62 July 2, 2021 20:09
@xavdid xavdid marked this pull request as ready for review July 2, 2021 21:08
@xavdid xavdid requested a review from eliangcs as a code owner July 2, 2021 21:08
@xavdid
Copy link
Contributor Author

xavdid commented Jul 2, 2021

Ok, this is ready! added tests, docs, and cleaned up resolveMethodPath while I Was in there.

eliangcs
eliangcs previously approved these changes Jul 6, 2021
Copy link
Member

@eliangcs eliangcs left a comment

Choose a reason for hiding this comment

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

Thanks for the good test coverage! I only have a suggestion on the docs.

packages/cli/README.md Outdated Show resolved Hide resolved
Co-authored-by: Chang-Hung Liang <chang-hung.liang@zapier.com>
@xavdid xavdid merged commit 50ccdf5 into master Jul 7, 2021
@xavdid xavdid deleted the PDE-2539 branch July 7, 2021 04:40
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.

2 participants