Best Practice – Lightning JS (Code reuse, testing frameworks and IDE support)

I asked about the difference between controller and helper and it prompted me to ask another, more specific set of questions:

  1. What is the best way to reuse JavaScript code across Lightning Components?
  2. What is the best way to test JavaScript code within Lightning Components?
  3. Do Lightning JavaScript files (Controller, Helper, Renderer, etc) have corresponding metadata components? I would like to pull this down via IDE for version control.

My main doubts around the platform revolve around how it fits in with our best practices of unit testing and code review, so answers to these questions could go a long way towards helping me cement it or eliminate it as an option. Thanks!


1. Reuseability

Helper.js is a good thing to share code within a bundle – but not across many bundles. I think a strong library concept is still missing. Actually it’s not possible to use CDN due to CSP but they are planning to introduce whitelists. It even looks like whitelists could be already possible:

Requesting CSP Exceptions

If your app is not working due to a CSP violation, contact Salesforce
to request a CSP exception for your org. Include the violation message
from your browser’s developer console in any communication.

You can use Static Resources to provide libraries (package- or org-wide).

2. Testing

since it’s very js-centric you might use some JS testing frameworks – but I expect salesforce to deliver here somthing.

3. Metadata / IDEs

All the Lightning metadata is available via Tooling API:

Alternatively access via Metadata API is working, too.

As far as I know, IDE still missing Lightning support. I think they are still on v30 or v31

You might also try a plugin for sublime/Mavensmate.

And CodeFusion actually does support Lightning since last year – however by design still without local files 😉

But you need this package for lightning support – both, metadata and tooling API (v32) is supported.

I always planned to write a version control integration, but probably before that happens I’m going to open source the project.

A really cool thing about Lightning is the speed for saving files. It’s not like apex or visualforce which needs about 3 to 10 seconds to save. Try saving a Lightning file it usually takes 0.300 to 0.800 seconds. First time fun coding! Doug’s team unleashed a turbo here.

Source : Link , Question Author : Adrian Larson , Answer Author : Community

Leave a Comment