I know that for primitive datatype passing by reference doesn't work in javascript, so a workaround is to wrap them in object. But consider a scenario where the initial state of the variable is null
and then the variable is reassigned as an Object
. Now if that varible is passed as an argument to an external function will it be passed as reference or will it end up being undefined
inside the function.
For reference consider this usecase:
in a mocha test for Login endpoint
Method 1
describe('Login Endpoint Test', function(){
let response = null;
before('test pre-requisites', async function(){
this.timeout(15000);
response = await endpointCall(); //returns response Object
});
it('simple endpoint test', function(){
//response is availble here.
response.should.have.status(200);
});
/*Importing an external testfile.*/
require('./externalTest.spec.js')(response);
})
in externalTest.spec.js
module.exports = (response) => {
it('external test', function(){
console.log(response); // null;
})
}
If i wrap the response in an Object
it works. Why is it so?
Method 2
If i wrap the response in an Object
it works. Why is it so?
describe('Login Endpoint Test', function(){
let data = {response: null};
before('test pre-requisites', async function(){
this.timeout(15000);
data.response = await endpointCall(); //returns response Object
});
it('simple endpoint test', function(){
//response is availble here.
data.response.should.have.status(200);
});
/*Importing an external testfile.*/
require('./externalTest.spec.js')(data);
})
in externalTest.spec.js
module.exports = (data) => {
it('external test', function(){
console.log(data.response); // response object;
})
}
NOTE: Pls let me know if you find there is a lack of clarity. Thanks in advance.