Enforce Function File Contract earlier#27
Conversation
Codecov Report
@@ Coverage Diff @@
## main #27 +/- ##
===========================================
+ Coverage 10.60% 31.13% +20.52%
===========================================
Files 4 4
Lines 198 212 +14
Branches 0 12 +12
===========================================
+ Hits 21 66 +45
+ Misses 177 145 -32
- Partials 0 1 +1
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
|
Need to do more testing before reopening this PR |
filmaj
left a comment
There was a problem hiding this comment.
LGTM! Left a few small nitpicks, looks good otherwise. Any larger tasks coming out of any of my comments can be filed as separate issues and addressed later.
deno.jsonc
Outdated
| "tasks": { | ||
| "test": "deno fmt --check && deno lint && deno test src", | ||
| "coverage": "deno test --coverage=.coverage src && deno coverage --exclude=fixtures --exclude=test .coverage" | ||
| "test": "deno fmt --check && deno lint && deno test src --allow-read", |
There was a problem hiding this comment.
Small suggestion, the flags should go before the directory parameter, just to align with deno test CLI help/usage docs.
| "test": "deno fmt --check && deno lint && deno test src --allow-read", | |
| "test": "deno fmt --check && deno lint && deno test --allow-read src", |
| const functionPathHasDefaultExport = async ( | ||
| functionFilePath: string, | ||
| ) => { | ||
| const functionModule = await import(`file://${functionFilePath}`); |
There was a problem hiding this comment.
Random thought: we could potentially enforce the type of default export returned by userland function code, ensuring that they are e.g. exporting the return of SlackFunction.
Not a blocker for this PR but just food for thought / future enhancement.
There was a problem hiding this comment.
Thinking more about how these default exports are consumed at runtime, and in particular:
- At runtime, if no
defaultexport exists, the runtime will throw an exception, and - The runtime directly invokes the
defaultexport as a function,
Perhaps we should ensure that the default export is, at least, a function? Maybe not as far as my previous comment suggested (to check the specific return type), but maybe as simple as a typeof functionModule.default == 'function' check?
Adding this additional check would prevent a developer from doing something silly like export default 3; or some other non-function type.
There was a problem hiding this comment.
Good call to ensure the default export is a function, like that the footprint is minimal
There was a problem hiding this comment.
Ive tested and implemented the typeof functionModule.default == 'function' check good idea @filmaj 💯
src/tests/functions_test.ts
Outdated
| assertExists(validateAndCreateFunctions); | ||
| }); | ||
|
|
||
| Deno.test("Function files should have a default export", async () => { |
There was a problem hiding this comment.
I am a bit of a pedant when it comes to test names - I really like for the test name to make clear what the assumptions around a test are, as well as what the expected behaviour is. I'd change this to something like:
| Deno.test("Function files should have a default export", async () => { | |
| Deno.test("validateAndCreateFunctions should not throw an exception when fed a function file that has a default export", async () => { |
Summary
The goal of this PR is to allow the deno slack builder to detect if a function file has a
default exportand notify the user is it does not.ticket: HERMES-2867
Test:
cdto an existing projectexport default handlerI'm looking for feedback on additional testing, making sure this will work with the other components of the system
Requirements