stevebuik / greased

Faster Lightning development and testing
MIT License
26 stars 2 forks source link

FEATURE: test.assert support for local variables (instead of attributes) #22

Closed stevebuik closed 7 years ago

stevebuik commented 7 years ago

var i = 1;

// start....

.then(test.assert(i != 0,"I expected a zero here"))

likewise for assertEquals and assertNotEquals

these should create assertion components to make failure reading easier.

stevebuik commented 7 years ago

the workaround for now is

.then(test.wait(function (context) { var i = 1; if (i != 0) { throw Error("I expected a zero here"); } }))

Szandor72 commented 7 years ago

This is a very crucial feature to me. I haven't tried the workaround but throwing an error is not the same as having the test fail, correct?

stevebuik commented 7 years ago

I started to work on this and realised that it's not trivial or obvious how to built it. I'll start with your use case. Can you paste in an example of how you would like to write an assertion that checks a variable.

In particular where is that variable defined (inside a test.wait or before the chain?) and what can change it before it is used in an assertion. Feel free to use a gist. Best would be to show a test using the "throw Error" technique above and then I can see how you would like to use this.

Szandor72 commented 7 years ago

Hey Steve,

basically I'd be absolutely content if the following assertions within a chain would work accordingly:

.then(testDriver.assert(true,"Test cannot fail, Assertion always true")) .then(testDriver.assert(false,"Test always fails, Assertion always false"))

As for my approach, please see

https://github.com/Szandor72/greased/blob/master/src/aura/searchBarInputText/searchBarInputText.cmp and https://github.com/Szandor72/greased/blob/master/src/aura/searchBarInputTextTest/searchBarInputTextTestController.js

My goal is to be able to use:

.then(testDriver.assert(!$A.util.isUndefined(cmp.find("ConditionallyRenderedComponent")),"Component must be rendered in current state"))

stevebuik commented 7 years ago

There's one problem with .then(testDriver.assert(true,"Test cannot fail, Assertion always true") and that's when the first argument (i.e. value you are checking) is evaluated.

The way you (and I) want to write it, the expression will be evaluated immediately when the chain is built, not delayed until it's position in the chain.

One answer is that these kinds of assertions have to be inside a test.wait (this is pretty common as you often want to execute some code before asserting) which will wrap the expression in a promise a delay execution/evaluation.

Would that work for you?

stevebuik commented 7 years ago

btw: for the example you gave above, it's simpler to add a boolean attribute to the component, use that to control the aura:if and then the assert is very simple

Szandor72 commented 7 years ago

Thank you for explaining the async background of the issue.

Inside the wait would work for me if I could feed the result back into the overall test results, i.e. avoid using Aura.Error.

As for the boolean attributes: Imho adding an attribute for the sole purpose of test-visibility will bloat the code after a while. It will also not work for my usecase in mind: I want to verify if a component has been successfully loaded dynamically. Granted, this is not yet visible from my repo but that's how far I want to take it with greased :)

On Mon, 16 Jan 2017 at 23:05 Steve Buikhuizen notifications@github.com wrote:

btw: for the example you gave above, it's simpler to add a boolean attribute to the component, use that to control the aura:if and then the assert is very simple

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/stevebuik/greased/issues/22#issuecomment-272972532, or mute the thread https://github.com/notifications/unsubscribe-auth/AQBpeiIfQKf45-ifLP10D_HFL_DOkGEcks5rS-mdgaJpZM4LjdJi .

stevebuik commented 7 years ago

Great. I can do exactly that in test.wait. I'll let you know when it's done.