I am using Jest (TypeScript on Node.js) for unit testing and would like to check if a specific property is either an Array or undefined using expect.objectContaining(). This property may or may not exist in the object, hence why I want my test to include both cases.
My initial idea was to use
propertyInQuestion: expect.any(Array || undefined)
inside objectContaining(), but it fails because an Array is expected. When I checked the documentation, it stated that an expected object must be a subset of the received object, so it contains properties that are present in the expected object. So what happens when a property is either present and must be of an Array type or is undefined and therefore not present in the object?
At the moment, test looks something like this
expect(someObject).toEqual(
expect.objectContaining({
propertyInQuestion: expect.any(Array),
something: expect.any(Array),
})
);
and the property I'd like to check is propertyInQuestion, which can either be an Array or undefined.
Any sort of help is appreciated. Thank you!
Related
I've heard that it is bad practice to set an expected type of a variable to any.
However, I don't understand, how I can get a return type of an imported function, which returns an object, which is only used in the library.
As an example, if I would like to use the crypto.createCipheriv() function provided by Node.js, which returns a Cipher object as stated in the docs, I would not known how to give a variable this type.
const variable: ReturnType<crypto.createCipheriv>;
I am using Typescript and NodeJS for my Google Cloud Function
if I get a document from firestore like this
const result = await db.doc(`events/${eventID}`).get();
const myData = result.data();
then the type of myData is FirebaseFirestore.DocumentData like this
in other part of my code,
I need to check, if a variable has the type of FirebaseFirestore.DocumentData or not using the code below
if ( myVariable instanceof FirebaseFirestore.DocumentData ) {
// do something
}
but I have an error like this
Property 'DocumentData' does not exist on type 'typeof
FirebaseFirestore'
so how to check if my variable is a type of Firestore Document Data?
I see this answer , and it needs to import something so I can access it. how to do something like that for FirebaseFirestore.DocumentData ?
As with your other question you are mixing up Typescript types and JavaScript objects.
FirebaseFirestore.DocumentData refers to a type that is used by the TypeScript compiler as a compile time check. TypeScript compiles down to JavaScript and JavaScript doesn't know about those types during runtime. So the only way to check if the returned data is the Document Data you have to check for the existence of fields or check for undefined.
I am using Sequelize where I fetch all the data from a table using findAll. Which is basically an Array of objects. What seems to be confusing is that the data I am showing as output nested under objects. (Sounds Confusing? Let me clarify)
So, Let's I have this Short Code
Here if I run this code, it will give me undefine because father lies in parent, for which I have to use user.parent.father, Right?
Okay, now on Fetching data from table in my code,
I console.log my first row, for which I get this.
Now here the values which I need lies in dataValues.
In my ejs file. I am using simple for-of loop
Now my Question is why am I not getting undefined for product.title , product.imageUrl and so on? It is supposed to get those data by product.dataValues.title. Because It lies in another object names dataValues.
Technically, when a value is initialized by Sequelize, your object's prototype is set to Model (the class is too long to copy-paste it here).
When you create your model, Sequelize calls init on it (line 424) and which in turn calls refreshAttributes.
This one calls Object.defineProperty to define both the getter and the setter for each property you have defined in the metadata (line 1238).
The getter and setter are set to get and set functions respectively (lines 1095 to 1103).
This actually means that
instance.field
is just a property wrapped over
instance.get('field')
This corresponds with their docs which says
Instance instances operate with the concept of a dataValues property, which stores the actual values represented by the instance. By default, the values from dataValues can also be accessed directly from the Instance, that is:
instance.field
is the same as
instance.get('field')
is the same as
instance.getDataValue('field')
I have a mongoose model with a method that checks a field and dynamically returns a value, and is called in an HTML template.
This gave me a problem when cleaning up routes to only select fields needed on the page.
After including the select parameter as in Model.find({}, 'select parameter', cb..., this schema method started failing, despite including in the select parameter the property this schema method checks.
Whats up with this, is there a way around it?
The schema method is defined inside thingSchema.methods: { ... ...
and looks for this.thing.length
which is included in the select parameter 'thing, otherThing, thingyThing, thingestThing'
and is called in the html template like thing.getThing(), which will throw error can't find prop length of undefined.
It turns out there is a problem with the string type for the select parameter.
Use the object type instead: {thing:1,otherThing:1,things:1}.
I am trying something like below:
require(['N/search'],
function(search)
{
var mySearch = search.create({
type : search.Type.FOLDER,
columns : ['internalid'],
filters : [ 'internalid', 'anyof', ID]
});
mySearch.run();
});
I get an error for search.Type.FOLDER that search.Type is undefined and so cannot find FOLDER of undefined
I was able to do a workaround by writing a type as 'folder' that worked, but, why is this enum not defined if it is documented in NetSuite's help.
I tried even logging all keys using Object.keys and the returned array does not contains Type key.
Has anyone tried this or if anyone can point out if something is wrong with my code?
I don't see anything wrong with your code, and I confirmed in my own instance that the module brought in by N/search does not include a Type enum. Including the N/record module does have correctly have the Type enum, so if you want to avoid the magic string 'folder', you could import N/record and use record.Type.FOLDER instead.
It's not ideal, as what you are doing should work, but it seems there must be a bug in the search module where they're not properly returning the Type enum.