1

Can't run test, with jasmine-karma on node.js.

i have installed, karma, jasmine, browserify,node and mongoose and i trying run my test but i get error "Uncaught TypeError: mongoose.model is not a function" and "... user.js:10:0".

karma.conf.js

// Karma configuration
// Generated on Fri Sep 16 2016 15:23:39 GMT+0200 (Środkowoeuropejski czas letni)

module.exports = function(config) {
  config.set({

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '',


    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['browserify','jasmine'],


    // list of files / patterns to load in the browser
    files: [
      'test-main.js',
      'spec/server/**/*.js',
      'server/scripts/auth.js'

    ],

    browserify: {
      debug: true,
      transform: [ 'brfs' ]
    },

    // list of files to exclude
    exclude: [
    ],


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
      'server/scripts/auth.js': [ 'browserify' ]
    },


    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['progress', 'jasmine-spec-runner'],
    jasmineSpecRunnerReporter: {
        jasmineCoreDir: 'jasmine-core'
    },


    // web server port
    port: 9876,


    // enable / disable colors in the output (reporters and logs)
    colors: true,


    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_INFO,


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,


    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: ['Chrome'],


    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: false,

    // Concurrency level
    // how many browser should be started simultaneous
    concurrency: Infinity}) }

authSpec.js

describe("Test auth", function () {
    describe("passwordVerification", function () {
        var Auth;

        beforeEach(function () {
            Auth = new auth();
        });
        it("is password same", function () {
            expect(Auth.isAlphaNumeric("asd111")).toBeTruthy();
        });
    });
});

auth.js

"use strict";
var mongoose = require('mongoose');
var session = require('express-session');
var User = require('../models/user.js');

var Auth = {
//...
    isAlphaNumeric(val) {
        if (val.match(/^[a-zA-Z0-9]+$/)) {
            return true;
        }
        else {
            return false;
        }
    }
}

module.exports = Auth;

user.js

'use strict';
var mongoose = require('mongoose');

var UserSchema = mongoose.Schema({
    email              : String,
    password           : String,
    isFacebookAccount  : Boolean
});

UserModel = mongoose.model("Users", UserSchema, "users");

module.exports = UserModel;
user2510058
  • 518
  • 1
  • 5
  • 13

2 Answers2

0

You are using model function with three args which is not a valid declaration.

Try to write this

user.js

'use strict';
var mongoose = require('mongoose');

var UserSchema = mongoose.Schema({
    email              : String,
    password           : String,
    isFacebookAccount  : Boolean
});

UserModel = mongoose.model("Users", UserSchema);

module.exports = UserModel;
abdulbarik
  • 6,101
  • 5
  • 38
  • 59
  • Your change don't helped , still same problem. About three args in model function i get this tip from here http://stackoverflow.com/a/7722490/2510058 and it working properly for my. So this is not a reason at 100% – user2510058 Sep 16 '16 at 16:42
  • What is your mongoose version? – abdulbarik Sep 16 '16 at 17:08
  • My mongoose version is 3.10.5 – user2510058 Sep 16 '16 at 17:41
  • @user2510058 what did you end up doing? I'm having the same problem. http://stackoverflow.com/questions/41004659/using-webpack-with-mongoose-mongoose-model-is-not-a-function – Rohit Tigga Dec 06 '16 at 21:19
0

You just have to connect database while running the program then this error will not occur.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 06 '23 at 05:08