try {
myObject = {
a: JSON.stringify(obj)
};
} catch (err) {
logError(`myMethod :::: ${err.message}`);
}
I wanted to cover catch block under jest testcases but I am new to this jest test cases so I m not getting how to mock Json.stringify and how to throw error?
I am mocking as below. But I am getting error as: TypeError: Invalid JSON Can u please tell me where I am going wrong?
JSON.stringify = jest.fn().mockImplementationOnce(() => { throw new TypeError('Invalid JSON'); });
As I understand it, you want to test the catch part of your code. You can pass an invalid Json as an object.
So, if your function is:
function jestTest(obj) {
try {
myObject = {
a: JSON.stringify(obj)
};
} catch (err) {
logError(`myMethod :::: ${err.message}`);
}
}
module.export = jestTest
then, in your test function, you can use this:
var jestTestObject = require('./jestTest') // this hold your exported module, code which you are testing
JSON.stringify = jest.fn(); // do not mock to return "Invalid Json"
test("CatchPart", () => {
var invalidJson = ({ "a": 1}) // Create invalid json
var logErrorSpy = jest.spyOn(jestTestObject, 'logError') // spy on logError
jestTest(obj); // Invalid object will take it to catch part
expect(logErrorSpy).toHaveBeenCalledTimes(1) // which will call logError once
})
Related
I'm new to NextJS, and trying to figure out, how to create a global variable that I could assign a different value anytime. Could someone give a simple example? (I know global might not be the best approach, but still I would like to know how to set up a global variable).
Let's say:
_app.js
NAME = "Ana" // GLOBAL VARIABLE
page_A.js
console.log(NAME) // "Ana"
NAME = "Ben"
page_B.js
console.log(NAME) // "Ben"
try using Environment Variables
/next.config.js
module.exports = {
env: {
customKey: 'my-value',
},
}
/pages/page_A.js
function Page() {
return <h1>The value of customKey is: {process.env.customKey}</h1>
}
export default Page
but you can not change its contents, except by changing it directly in next.config.js
Nextjs no special ways to provide global variables you want. You can achieve by:
Stateful management tool, like redux-react
Using Context
It's not like it's impossible,
I created a file called _customGlobals.jsx and put this as content
String.prototype.title = function () {
const sliced = this.slice(1);
return (
this.charAt(0).toUpperCase() +
(sliced.toUpperCase() === sliced ? sliced.toLowerCase() : sliced)
);
};
and imported it in _app.jsx like this:
import "./_customGlobals";
So now I can call this function on any string anywhere in my project like this:
"this is a title".title()
Database designed for this purpose. But for one var it's not wise to install whole db!
So, you can do it in a JSON file.
Add a var to a JSON file and use a function to update it.
this is a simple function for this usage:
const fs = require('fs');
function updateJSONFile(filePath, updates) {
fs.readFile(filePath, 'utf8', function (err, data) {
if (err) {
console.error(err);
return;
}
let json = JSON.parse(data);
for (let key in updates) {
json[key] = updates[key];
}
fs.writeFile(filePath, JSON.stringify(json, null, 2), 'utf8', function (err) {
if (err) {
console.error(err);
}
});
});
}
So, use it like this:
updateJSONFile('file.json', { name: 'John Doe', age: 30 });
You can create another function to read it dynamicly:
function readJSONFile(filePath) {
fs.readFile(filePath, 'utf8', function (err, data) {
if (err) {
return callback(err);
}
let json;
try {
json = JSON.parse(data);
} catch (error) {
return callback(error);
}
return callback(null, json);
});
}
and you can use it like this:
const readedFile = readJSONFile('file.json')
I deleted the callback function to have a simple code but you can add callback function to log error messages.
I've Used API and updated my state, so now I have an object called animals which contains functions from the API:
animals = {
bird: randomBird(),
cat: randomCat(),
dog: randomDog(),
ect...
}
Now for example, randomCat() function looks like this:
function randomCat() {
var request = async () => {
var res = await get('https://aws.random.cat/meow');
if (res.ok) {
return res.body;
} else {
console.error(`An error occured: ${res.error}`);
}
}
var url = request().then(body => body.file);
return url;
}
So I suppose to use this function to get an url.
Now, if I try to call it like this:
const cat = this.state.animals.cat;
cat();
I get Unhandled Rejection (TypeError): URL is not a constructor.
But as I check for typeof cat, I get that it is a function, so how do I call it?
The console.log in my indexedDB works and returns the result that I want; an array of objects that is currently in the store. So my code there is correct. I'm going to use this information to build a table. However, in Vue it returns undefined. I'm trying to set the leagues array in Vue to equal the result array that indexedDB gives, but it returns undefined.
This is the code in Vue:
<script>
import * as db from "../db/db.js";
export default {
name: "leaguesTable",
data: function() {
return {
leagues: []
};
},
created: function() {
this.leagues = db.getAllInStore("meta", "leagues");
console.log(this.leagues);
}
};
</script>
This is my indexedDB code:
function getAllInStore(dbName, storeName) {
let db;
var request = indexedDB.open(dbName, 1);
request.onerror = function(event) {
alert("Database error" + event.target.errorCode);
};
request.onsuccess = function(event) {
db = event.target.result;
let tx = db.transaction(storeName, "readonly");
tx.onerror = function(event) {
alert("Transaction error" + event.target.errorCode);
};
let store = tx.objectStore(storeName);
let result = store.getAll();
tx.oncomplete = function() {
alert("This should work");
console.log(result.result);
return result.result;
};
};
}
In your created hook you need to make sure to return a value from db.getAllInStore so that this.leagues assumes that value.
Next, In the getAllInStore function result.result gets returned from the transaction but not within onComplete or the enclosing getAllInStore function.
Since the db uses event hooks like onError and onComplete, Returning the request won't give you the result of the call to the db. In order to return the value of an async operation in javascript, typically callbacks or promises are used. The example below makes use of promises to solve the issue.
Vue JS:
<script>
import * as db from "../db/db.js";
export default {
name: "leaguesTable",
data: function() {
return {
leagues: []
};
},
// async is necessary to use await
created: async function() {
// await is es2016 syntactic sugar for retrieving the value of a promise
this.leagues = await db.getAllInStore("meta", "leagues");
console.log(this.leagues);
}
};
</script>
IndexDB:
function getAllInStore(dbName, storeName) {
// resolve param is a function that signifies a successful operation
// reject param is a function that should be called whenever a check or error occurs
return new Promise((resolve, reject) => {
let db;
let request = indexedDB.open(dbName, 1);
request.onerror = (event) => reject(event);
request.onsuccess = (event) => {
db = event.target.result;
let tx = db.transaction(storeName, "readonly");
request.onerror = (event) => reject(event);
let store = tx.objectStore(storeName);
let result = store.getAll();
tx.oncomplete = (result) => resolve(result.result);
};
});
}
Further Reading:
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Promises
I'm stuck with handling array of objects that contain errors inside my react-native/redux application.
I have a handleResponse function that looks something like this:
function handleResponse (response) {
let status = JSON.stringify(response.data.Status)
let res = JSON.stringify(response)
if (Number(status) === 200) {
return res
} else {
throw new SubmissionError('Something happened')
}
}
But instead of plain text passed as argument to SubmissionError function - I want somehow to take out errors from array of objects.
Array of objects containing error messages looks like this:
{
ErrorMessages: [
{ ErrorMessage: 'foo' },
{ ErrorMessage: 'bar' }
]
}
How can I, for example, throw foo and bar errors?
You can't really throw two things at once, unless you wrap them in a Promise or do other tricks, I would just concatenate the errors together and do a single throw:
function handleResponse (res) {
if (Number(res.data.Status) === 200) {
return JSON.stringify(res)
}
const errors = res.ErrorMessages.map(e => e.ErrorMessage).join(', ')
throw new SubmissionError(`Some errors occurred: ${errors}.`)
}
Any ideas? Why does node say 'filename is undefined'? Thanks.
Contract, policy ans invoice functions resolve with no data, just resolve().
var dc = function(data) {
return new Promise(function(resolve, reject) {
var filename = 'Test';
var contract = function() { ... }
var policy = function() { ... }
var invoice = function() { ... }
contract().then(invoice().then(policy().then(function() {
console.log(filename); // Test
resolve(filename); // UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): ReferenceError: filename is not defined
})))
})
}
First of all, you cannot write:
contract().then(invoice() ... )
(that would work if the invoice() function returned another function to act as a then handler)
You have to write:
contract().then(function (value) { invoice() ... })
Or:
contract().then(value => invoice() ... )
Or maybe this if one function should handle the result of other function:
contract().then(invoice).then(policy).then(function (result) { ... });
What you have to pass as an argument to then is a function, not a result of calling a function (which is probably a promise in your example).
I don't know if that's the only problem with your approach but it is certainly one of the problems. Of course it may work but probably not how you expect.
2017 Update
If you use ES2017 async/await that's available in Node since v7.0 then instead of:
contract().then(invoice).then(policy).then((result) => { ... });
you can use:
let a = await contract();
let b = await invoice(a);
let c = await policy(b);
// here your `result` is in `c`
or even this:
let result = await policy(await invoice(await contract()));
Note that you can only use it in functions declared with the async keyword. This works on Node since version 7. For older versions of Node you can use a similar thing with a slightly different syntax using generator-based coroutines, or you can use Babel to transpile your code if that's what you prefer of if that what you already do.
This is quite a new feature but there are a lot of questions on Stack Overflow about it. See:
try/catch blocks with async/await
Do async in a blocking program language way?
try/catch blocks with async/await
Use await outside async
Using acyns/await in Node 6 with Babel
When do async methods throw and how do you catch them?
using promises in node.js to create and compare two arrays
Keeping Promise Chains Readable
function will return null from javascript post/get
It looks like you don't care about the order, in which case you could use Promise.all. Does this work for you? It will resolve once all of the promises have been resolved, or it will reject as soon as any one of them rejects.
function contract(data) { return new Promise(...) }
function policy(data) { return new Promise(...) }
function invoice(data) { return new Promise(...) }
function dc(data) {
var filename = 'Test';
return new Promise(function(resolve, reject) {
Promise.all([contract(data), policy(data), invoice(data)]).then(
function (values) {
console.log(filename);
resolve(filename)
},
function (err) {
reject(err);
}
);
});
}
If you care about the order, then you do have to chain them, like you've tried to do. You're code is passing promises as an argument to then. You need to pass functions.
function contract(data) { return new Promise(...) }
function policy(data) { return new Promise(...) }
function invoice(data) { return new Promise(...) }
function dc(data) {
var filename = 'Test';
return new Promise(function(resolve, reject) {
contract(data).then(
function (contract_res) {
policy(data).then(
function (policy_res) {
invoice(data).then(
function (invoice_res) {
console.log(filename);
resolve(filename);
},
function (err) { reject(err); } // invoice promise rejected
);
},
function (err) { reject(err); } // policy promise rejected
);
},
function (err) { reject(err); } // contract policy rejected
);
});
}
You may be able to simplify this with catch, but the deep nesting is a pain. Take a look at this post about flattening Promise chains.