Receiving The 'task' event has not been registered in the plugins file. You must register it before using cy.task() - task

I don't know how to use the task and for which purpose, I have one project and I have got this project for solving errors. and I am getting this error. Any help or suggestions are welcome.
I have this code in my plugins/index.js file.
module.exports = (on, config) => {
on("task", { "setUserId": (val) => { return (userId = val); }});
};
And in my test i have implement like this.
cy.task('setUserId', randomEmail);
I got this below error.

Related

System.Web.Optimization.JsMinify produces invalid JavaScript

I am using System.Web.Optimization v1.3 in what I believe is a standard configuration, to bundle and minify the JavaScript and CSS files in an MVC 5 web application. For the most part, this works very well, but I have discovered a case where JavaScript is corrupted by the minification process.
Here is a simplified version of what was originally an AngularJS input validation directive:
var myApp;
(function (myApp) {
myApp.module.directive("validator", [
function () {
return {
link: function (scope, element) {
var validators = [
{ signal: "required", message: "Please enter a value", value: null },
{ signal: "email", message: "Please enter a valid email address", value: null }
];
$("input", element).on("blur", function () {
for (var i in validators) {
var validator = validators[i];
if (scope.$parent.form[scope.modelName].$error[validator.signal]) {
element.removeClass("has-success");
scope.errorMessage = myApp.Utility.formatString(validator.message, eval(validator.value));
break;
}
}
});
}
};
}
]);
})(myApp || (myApp = {}));
Although the above code no longer does anything useful (because it has been trimmed), it does demonstrate the minification problem. When minified, the resulting JavaScript is as follows:
var myApp;
function(n){
n.module.directive("validator",[
function(){
return{
link:function(t,i){
var r=[
{signal:"required",message:"Please enter a value",value:null},
{signal:"email",message:"Please enter a valid email address",value:null}
];
$("input",i).on("blur",function(){
var i,
validator;
for(i in r)
if(validator=r[i],t.$parent.form[t.modelName].$error[validator.signal]){
i.removeClass("has-success");
t.errorMessage=n.Utility.formatString(validator.message,eval(validator.value));
break
}
})
}
}
}
])
})(myApp||(myApp={}))
Note how minification has assigned the parameter names t and i to the link function, despite the fact that a loop variable i is used in the original code.
Needless to say, this breaks the code. In this case I can fix it by renaming my loop variable, but I am concerned that there may be other adverse consequences to JsMinify's minification that I am not aware of.
So, I have 3 questions related to this issue:
Am I right to assume that this is a minification bug and, if so, is
there somewhere I should report it?
Is there any practical way of finding any other instances of this issue in my minified code?
Is it possible to replace the JavaScript minification engine used by
System.Web.Optimization and, if so, what would be a good
alternative?
Many thanks, in advance, for your ideas.
Tim
Update: After some further investigation, I have discovered that it is actually WebGrease that performs minification on behalf of System.Web.Optimization and this issue appears to be the one that I am seeing. This seems to answer my first question, but I would still appreciate advice regarding alternative minifiers.
In the past, I have successfully used YUI Compressor for .Net (now moved to GitHub), which is a .NET port from Java of YUI Compressor. The great thing about it is that it's based on Mozilla's Rhino JavaScript interpreter, meaning it actually understands the code, and not just runs regular expressions on it. As a result, it will fail your build if you have JavaScript syntax errors.
I found the Bundle Transformer (https://bundletransformer.codeplex.com/) to work well. It has a number of minifier options, such as YUI, JSMin, Microsoft Ajax Minifier, etc.
Example code in Global.asax.cs:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
var yuiMinifier = new BundleTransformer.Yui.Minifiers.YuiJsMinifier();
var customerTransformer = new BundleTransformer.Core.Transformers.ScriptTransformer(yuiMinifier);
var customBundle = new BundleTransformer.Core.Bundles.CustomScriptBundle("~/js/my-javascript.min.js");
customBundle.Include("~/js/script1.js");
customBundle.Include("~/js/script2.js");
customBundle.Transforms.Clear();
customBundle.Transforms.Add(customerTransformer);
BundleTable.Bundles.Add(customBundle);
}
}
Note: For me the minification only occurred if debug mode was set to false in the web.config.

Silverstripe 3: removeByName not working

Good morning,
I've been trying to use the removeByName method and it doesn't work.
I'm basically trying to hide a field in my DataObject within the forms that's generated by ModelAdmin, which manages the object.
See sample code below:
///DataObject snippet...
class MyObject extends DataObject{
public static $db = array(
'Title' => 'Varchar',
'Desc' => 'Text',
'Template' => 'HTMLText',
);
//#Override
public function getCMSField(){
$fields = parent::getCMSField();
$fields->removeByName('Template'); /// DOESN'T WORK!!!
return $fields;
}
}//class
Note: I'm not getting any errors. I'm just still seeing the field on the forms (Add and Edit) as usual.
Any help appreciated, thanks.
Okay, I found the issue.
I was just going over the API again for the millionth time, and recognized that I've named the function wrong. See correction below:
///Correction, forgot to add the 's' at the end of both the function and the parent call.
public function getCMSFields(){
$fields = parent::getCMSFields();
}
I can understand an error not being generated in Apache logs for the function because it's legit. But as for the parent call, it should of generated an error since the method don't exists. (Theory: Perhaps, since the function was never actually being called, the parent call wasn't being executed and thus no errors [run-time error]).

How should I respond with different status codes to a GET request in Spray?

For POST and PUT requests I use the following syntax:
put {
entity(as[CaseClass]) { entity =>
returnsOption(entity).map(result => complete{(Created, result)})
.getOrElse(complete{(NotFound, "I couldn't find the parent resource you're modifying")})
}
}
Now for GET requests I'm trying to do the same, but I can't get it to work analogously to my PUT solution. What is a good way to do this with GET requests?
Update:
I've got this working with the following hack:
(get & parameters('ignored.?)) {
//TODO find a way to do this without ignored parameters
(ingored:Option[String]) => {
returnsOption().map(result => complete(result))
.getOrElse(complete{(NotFound, "")})
}
}
I'd expect something similar to be possible with () => or ctx => , but that doesn't fly, because it gives trouble with marshalling:
... could not find implicit value for evidence parameter of type spray.httpx.marshalling.Marshaller[(spray.http.StatusCodes.ClientError, String)]
}).getOrElse(ctx.complete{(NotFound, "")})
^
Could it be that this somehow relates to the fact that I'm using spray-json?
Use HttpResponse like this for example
complete{
HttpResponse(StatusCodes.OK, HttpBody(ContentType(`text/html`), "test test: " + System.currentTimeMillis.toString))
}
Update: I've been using Spray for a while now. Turned out there is better way:
complete {
StatusCodes.BandwidthLimitExceeded -> MyCustomObject("blah blah")
}
This code should work:
get {
ctx =>
ctx.complete(returnsOption())
}
If don't use ctx => at the start, your code might only be executed at route build time.
Here you can find some explanations: Understanding the DSL Structure

Throw exception from closure

I've been trying to setup a simple Serversocket and I would like to have an exception thrown (other than some other stuff ie. setting a var to false) if some error is encountered, it works using an external callback but what about closures?
The Dart editor gives me an error and refuses to run it!
Server(String address,int port,int backlog)
{
this.s = new ServerSocket(address,port,backlog);
this.s.onError = (e) => throw new Exception(e);
}
I've tried also "throw e" and stuff like that, but as long as "throw" is present the ide won't run it.
I have had the same problem, Dart seams to be unable to accepts throws in single line closures. You should be able to do:
Server(String address,int port,int backlog)
{
this.s = new ServerSocket(address,port,backlog);
this.s.onError = (e) {
throw new Exception(e);
};
}
I have not looked in the spec so I don't know if its intentional or is a bug.

Query Orchard CMS from IShapeFactoryEvents

I am trying to insert shapes into my layout from my module once it has been enabled. I figured IShapeFactoryEvents would be perfect for this, but querying the CMS from here gives me a "TransactionScope nested incorrectly exception" if this occurs during a POST. Wondering if anyone had any words of wisdom for me again? See my code snippet below.
public void Created(ShapeCreatedContext context)
{
if (context.ShapeType == "Layout")
{
if (!AdminFilter.IsApplied(_services.WorkContext.HttpContext.Request.RequestContext))
{
var route = RouteTable.Routes.GetRouteData(_services.WorkContext.HttpContext);
object location;
if (route.Values.TryGetValue("location", out location))
{
var region = _services.ContentManager.Query("Region")
.Join<RoutePartRecord>()
.Where(x => x.Slug == (string)location)
.Slice(1).FirstOrDefault();
context.Shape.Header.Add(context.New.CurrentRegion(Title: region.As<RoutePart>().Title), "10");
}
context.Shape.Navigation.Add(context.New.RegionSelector(Regions: _services.ContentManager.Query(VersionOptions.Published, "Region").List()), "11");
}
}
}
Once again, thanks in advance. You guys are awesome.
See the blog post I did on this very topic here: http://chrisbower.com/2011/02/15/orchard-shape-wizardry/
From the blog post:
"One thing I need to note, and this took me an entire day to discover, is that you can't just inject your data service into your IShapeTableProvider implementation. If you do, it'll try to use a transaction out of scope and that will cause you all kinds of problems. After tearing my hair out for hours on end, I finally discovered what the Orchard team is doing inside of the CoreShapes class: The trick to resolve a service dependency within the function itself by using a property that loads the service per-request."
Try using your service like this instead:
private IOrchardServices Services
{
get
{
return _workContextAccessor.GetContext(_httpContextAccessor.Current()).Resolve<IOrchardServices>();
}
}

Resources