-
Notifications
You must be signed in to change notification settings - Fork 189
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
Conversation
@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. |
@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? |
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? |
@xavdid the feature request makes sense to me, but I don't think the 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? |
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. |
@xavdid looking good! Let me know once this PR is ready for review. |
Ok, this is ready! added tests, docs, and cleaned up |
There was a problem hiding this 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.
Co-authored-by: Chang-Hung Liang <[email protected]>
Addresses #16.
see also: PDE-2539
Todo
Testing
yarn link
in thecore
directoryzapier init ad-hoc-test -t custom-auth
cd ad-hoc-test && yarn && yarn link zapier-platform-core
Now,
appTester
can be fed ad-hoc functions. Try the following test:It's a simple test, but it proves a point: a test function can use
z.request
and take advantage of middleware.