1

I have 2 files as below

admin.js

const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const Schema = new Schema({
    User: { type: String, unique: true },
    Pass: { type: String },
});

var Admin = mongoose.model("admin", Schema);
module.exports = Admin;

admin.test.js

const Admin = require('./admin.js');

test("verify username is present and datatype is correct", () => {
 if (typeof Admin.Admin !== "undefined") {
        // this statement will not execute
        console.log('username is DEFINED');
    } else {
        console.log('username is not DEFINED');
    }

});

I need to validate that the Username is present and validate datatype is a string. I am not able to call the Username from another file.

1

Your admin.js is :

const mongoose = require("mongoose");
let Schema = mongoose.Schema;
Schema = new Schema({
    User: { type: String, unique: true },
    Pass: { type: String },
});

var Admin = mongoose.model("admin", Schema);
module.exports = Admin;

here you are exporting the identifier "Admin" . So when you import admin.js using require in any other module "Admin" will be exposed .

const Admin = require('./admin.js');

console.log(Admin)

You don't have to call Admin.Admin , it will return undefined always as "Admin" variable has a model and doesn't have any property called "Admin"

1
  • is there a way that I can test the datatype defined as expected for the unit test?
    – meado
    Apr 5 '21 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.