How to use invokeNode method in Mocha

Best JavaScript code snippet using mocha

Run Mocha automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

InvokeNode.js

Source: InvokeNode.js Github

copy
1/*
2 * Copyright 2011 eBay Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *    http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17define.Class(
18    'raptor/templating/taglibs/core/InvokeNode',
19    'raptor/templating/compiler/Node',
20    ['raptor'],
21    function(raptor, require) {
22        'use strict';
23        
24        var forEach = raptor.forEach;
25        
26        var InvokeNode = function(props) {
27            InvokeNode.superclass.constructor.call(this);
28            if (props) {
29                this.setProperties(props);
30            }
31        };
32        
33        InvokeNode.prototype = {
34
35            doGenerateCode: function(template) {
36                
37                var func = this.getProperty("function"),
38                    funcDef,
39                    bodyParam,
40                    definedFunctions = template.getAttribute("core:definedFunctions");
41                
42                if (!func) {
43                    this.addError('"function" attribute is required');
44                    return;
45                }
46                
47                if (func.indexOf('(') === -1) {
48                    
49                    funcDef = definedFunctions ? definedFunctions[func] : null;
50//                    if (!funcDef) {
51//                        this.addError('Function with name "' + func + '" not defined using <c:define>.');
52//                    }
53                    
54                    var argParts = [];
55                    var validParamsLookup = {};
56                    var params = [];
57                    
58                    if (funcDef) {
59                        params = funcDef.params || [];
60                        bodyParam = funcDef.bodyParam;
61                        /*
62                         * Loop over the defined parameters to figure out the names of allowed parameters and add them to a lookup
63                         */
64                        forEach(params, function(param) {
65                            validParamsLookup[param] = true;
66                        }, this);
67                        
68                    }
69
70                    var bodyArg = null;
71                    
72                    if (this.hasChildren()) {
73                        if (!funcDef || !funcDef.bodyParam) {
74                            this.addError('Nested content provided when invoking macro "' + func + '" but defined macro does not support nested content.');
75                        }
76                        else {
77                            bodyArg = this.getBodyContentExpression(template, false);
78                        }
79                        
80                    }
81
82                    /*
83                     * VALIDATION:
84                     * Loop over all of the provided attributes and make sure they are allowed 
85                     */
86                    this.forEachPropertyNS('', function(name, value) {
87                        if (name === 'function') {
88                            return;
89                        }
90                        
91                        if (!validParamsLookup[name]) {
92                            this.addError('Parameter with name "' + name + '" not supported for function with name "' + func + '". Allowed parameters: ' + params.join(", "));
93                        }
94                    }, this);
95                    
96                    /*
97                     * One more pass to build the argument list
98                     */
99                    forEach(params, function(param) {
100                        validParamsLookup[param] = true;
101                        if (param === bodyParam) {
102                            argParts.push(bodyArg ? bodyArg : "undefined");
103                        }
104                        else {
105                            var arg = this.getAttribute(param);
106                            if (arg == null) {
107                                argParts.push("undefined");
108                            }
109                            else {
110                                argParts.push(this.getProperty(param));
111                            }
112                        }
113                        
114                    }, this);
115                    
116                    template.write(func + "(" + argParts.join(",") + ")");
117                }
118                else {
119                    var funcName = func.substring(0, func.indexOf('('));
120                    funcDef = definedFunctions ? definedFunctions[funcName] : null;
121                    if (funcDef) {
122                        template.write(func);
123                    }
124                    else {
125                        template.statement(func + ";\n");
126                    }
127                }
128            }
129            
130        };
131        
132        return InvokeNode;
133    });
Full Screen

multiple-runs.spec.js

Source: multiple-runs.spec.js Github

copy
1'use strict';
2
3const {invokeNode} = require('./helpers');
4
5describe('multiple runs', function() {
6  it('should be allowed to run multiple times if cleanReferences is turned off', function(done) {
7    var path = require.resolve(
8      './fixtures/multiple-runs/run-thrice.fixture.js'
9    );
10    invokeNode([path], function(err, res) {
11      if (err) {
12        done(err);
13        return;
14      }
15      expect(res.code, 'to be', 0);
16      var results = JSON.parse(res.output);
17      expect(results, 'to have length', 3);
18      expect(results[0].pending, 'to have length', 1);
19      expect(results[0].failures, 'to have length', 0);
20      expect(results[0].passes, 'to have length', 0);
21      expect(results[1].pending, 'to have length', 0);
22      expect(results[1].failures, 'to have length', 1);
23      expect(results[1].passes, 'to have length', 0);
24      expect(results[2].pending, 'to have length', 0);
25      expect(results[2].failures, 'to have length', 0);
26      expect(results[2].passes, 'to have length', 1);
27      done();
28    });
29  });
30
31  it('should not be allowed if cleanReferences is true', function(done) {
32    var path = require.resolve(
33      './fixtures/multiple-runs/clean-references.fixture.js'
34    );
35    invokeNode(
36      [path],
37      function(err, res) {
38        if (err) {
39          done(err);
40          return;
41        }
42        expect(res, 'to have failed').and(
43          'to contain output',
44          /ERR_MOCHA_INSTANCE_ALREADY_DISPOSED/
45        );
46
47        done();
48      },
49      {stdio: ['ignore', 'pipe', 'pipe']}
50    );
51  });
52
53  it('should not be allowed if the instance is disposed', function(done) {
54    var path = require.resolve('./fixtures/multiple-runs/dispose.fixture.js');
55    invokeNode(
56      [path, '--directly-dispose'],
57      function(err, res) {
58        if (err) {
59          done(err);
60          return;
61        }
62        expect(res.code, 'not to be', 0);
63        expect(res.output, 'to contain', 'ERR_MOCHA_INSTANCE_ALREADY_DISPOSED');
64        done();
65      },
66      {stdio: ['ignore', 'pipe', 'pipe']}
67    );
68  });
69
70  it('should not be allowed to run while a previous run is in progress', function(done) {
71    var path = require.resolve(
72      './fixtures/multiple-runs/start-second-run-if-previous-is-still-running.fixture'
73    );
74    invokeNode(
75      [path],
76      function(err, res) {
77        if (err) {
78          done(err);
79          return;
80        }
81        expect(res.output, 'to contain', 'ERR_MOCHA_INSTANCE_ALREADY_RUNNING');
82        done();
83      },
84      {stdio: ['ignore', 'pipe', 'pipe']}
85    );
86  });
87
88  it('should reset the hooks between runs', function(done) {
89    var path = require.resolve(
90      './fixtures/multiple-runs/multiple-runs-with-flaky-before-each.fixture'
91    );
92    invokeNode([path], function(err, res) {
93      expect(err, 'to be null');
94      expect(res.code, 'to be', 0);
95      var results = JSON.parse(res.output);
96      expect(results, 'to have length', 2);
97      expect(results[0].failures, 'to have length', 1);
98      expect(results[0].passes, 'to have length', 0);
99      expect(results[1].passes, 'to have length', 1);
100      expect(results[1].failures, 'to have length', 0);
101      done();
102    });
103  });
104});
105
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Mocha on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)