/cljs-lambda/doc/testing.md

https://github.com/nervous-systems/cljs-lambda · Markdown · 67 lines · 49 code · 18 blank · 0 comment · 0 complexity · 5c05eb0ef0370b3f23850e0741cf2074 MD5 · raw file

  1. # Testing
  2. While it's strongly suggested that Lambda functions are minimal abstractions of
  3. the execution environment (i.e. input/output processing only, delegating to
  4. generic Clojurescript functions), there are times when it's going to make sense
  5. to test the entry points off of EC2.
  6. [[cljs-lambda.local/invoke]] invokes a Lambda handler with the supplied event
  7. (optional, nil), supplying a context suitable for local evaluation. The context
  8. record'll contain superficially plausible values for the context's string keys,
  9. and invoke will ensure that the eventual result of the invocation, however
  10. delivered, will be conveyed to the caller via a promise.
  11. [[cljs-lambda.local/channel]] behaves identically, with the exception of placing
  12. the result on a `core.async` channel, in a vector tagged with either `:success`
  13. or `:fail`.
  14. ### Example
  15. ```clojure
  16. (deflambda testable [x ctx]
  17. (go
  18. ;; TODO Think hard
  19. (inc x)))
  20. (invoke testable 5) ;; => <Promise 6>
  21. (channel testable 5) ;; => <Channel [:succeed 6]>
  22. ```
  23. ### Example
  24. ```clojure
  25. (def fs (.promisifyAll promesa/Promise (nodejs/require "fs")))
  26. (deflambda read-file [path ctx]
  27. (.readFileAsync fs path))
  28. (invoke read-file "static/config.edn") ;; => <Promise "{:x ...">
  29. ```
  30. ### Example
  31. ```clojure
  32. (deflambda identify [caller-name {my-name :function-name}]
  33. (str "Hi " caller-name " I'm " my-name))
  34. ;; The default context values aren't particularly exciting
  35. (invoke identify "Mary") => ;; <Promise "Hi Mary, I'm functionName">
  36. ;; But custom values may be supplied
  37. (invoke identify "Mary" (->context {:function-name "identify"}))
  38. ;; => ;; <Promise "Hi Mary, I'm identify">
  39. ```
  40. ## Environment Variables
  41. When testing functions which retrieve environment variables via
  42. [[cljs-lambda.context/env]], alternate values may be supplied in a test's
  43. context object.
  44. ```clojure
  45. (deflambda env [k ctx]
  46. (ctx/env ctx k))
  47. (invoke read-file "USER" (->context {:env {"USER" "moe"}}))
  48. ```
  49. When deployed, `ctx/env` would read the value from Node's `process.env`.