Comments (4)
I suppose there are 2 approaches you can evaluate,
1- Try using mocking capabilities of the Test Framework. This is probably more practical as it doesn't require you change existing code.
For example,
- Jasmine has some out of the box support via 'spyOn' which is also being used here by jasmine example.
- In case of mocha, LTS brings in sinonjs for mocking/spying which is being used here in a mocha example.
This thread also has some discussion about jasmine spies.
In your case, I am thinking that you would spy on find(),
spyOn(component, "find").and.callFake(function(cId) {..});
to return some sort of proxy/mock.
2- Restructure/Reorganize the components for Dependency Injection where the leaf/UI components mostly act based on information provided to them by a wrapper/container, improving their testability.
from lightningtestingservice.
Hi Emad, Thanks for responding. Trying to establish good patterns here.
I think it would help if the https://github.com/forcedotcom/LightningTestingService page explained more about the execution context here i.e. what will work and what will not. In the debugger I see that the (not mocked) lightning:workspaceAPI component delegates to one:workspaceToolkit.js where $A.get("e.force:getEnclosingTabId") is undefined and so the call fails.
From my perspective, testing against a real lightning:workspaceAPI would be simpler and build more confidence that my components will work in the real environment.
Started looking at the spyOn option and its a pretty nasty peeking into the internal structure of the component under test and also requires the find of the lightning:workspaceAPI to be moved out of the init so the spy can be applied in the test in the create callback. Have you considered adding features to LTS such as tests being able to intercept <lightning:workspaceAPI aura:id="workspace"/>
creates? (Not saying this specifically is a well though out idea, just that there is probably a set of common problems that people writing tests are going to run into and ready made solutions will stop people giving up.)
from lightningtestingservice.
'real environment' in some of these cases may imply that you need end-to-end UI tests (e.g. webdriver) instead of component tests. For example if your component changes behavior based on an internal/contextual check for the container/environment, from a component test you won't have much control over what that check returns. You could break up your code into smaller parts and leave the logic around contextual checks in a 'container/stateful component' and have most of your custom logic in the presentation/functional component.
Pros/cons of spies/mocking-frameworks vs make code more testable are fairly standard topics (concepts are language agnostic) when it comes to unit/component testing. For example just google for 'writing testable code'.
Current scope of LTS is to enable the use of standard testing tools and building blocks for Lightning. I have however seen a consumer build a mock registry of sort on top of jasmine's spies functionality which they always initialized with a handful of mocks before a test run.
from lightningtestingservice.
OK thanks for taking the time to comment. We've some AngularJS Karma (and Protractor) tests that I remember being fairly painless to get working. But maybe I've forgotten the bad parts...
from lightningtestingservice.
Related Issues (20)
- test an init function
- how to tests when v.body is set HOT 7
- Question on `sfdx force:lightning:test:install -t jasmine` HOT 2
- prerequisites to write tests? HOT 1
- chain spyOn? HOT 2
- GAC when testing scenario 2 HOT 3
- Testing with namespaces and Labels HOT 1
- jasmineTests.app not working HOT 5
- Setting DateTime attribute using $T.createComponent gives internal server error HOT 3
- Reporting does not escape every attribute in Junit format.
- jasmineTests.app Failed - Definition does not exist HOT 4
- Test reporter not escaping results
- How to test component events when event and handler are in different components?
- Mocking the internal clock in jasmine
- LTS for blur component event HOT 1
- Request: Support JEST HOT 1
- Unable to install LTS with SFDX HOT 4
- lightning:test:run execution permission denied HOT 2
- Fails On Page Load w/ "Freeze Javascript Prototype" Enabled HOT 3
- Installing with sfdx causes a GACK HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lightningtestingservice.