Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9130605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:59:32+00:00 2026-06-17T07:59:32+00:00

This probably has to do with asynchronous code, but I’m not sure what. The

  • 0

This probably has to do with asynchronous code, but I’m not sure what. The both pass when I run them apart from each other: mocha test/models.coffee and mocha test/login.coffee

But describe 'saving another user with same username', -> fails when I run the tests together with npm test. I imagine the database gets cleared as it’s running that step so it saves instead of producing an error because it’s supposed to be a unique value.

ALSO: I am extremely new at this software testing stuff, if anyone has any tips, or wants to criticize my blatant mistakes, feel free to email me (check my profile)

Thanks!!

Here are my two test files (coffeescript):

/test/models.coffee

should = require 'should'
{User} = require '../models'

# function to find our test user, John Egbert
findJohn = (cb) ->
  User.findOne {'public.username': 'john'}, (err, john) ->
    throw err if err
    cb(john)

before (done) ->

  # Drop all users from database
  User.collection.drop()

  # Create our test user, his username is John Egbert
  User.create
    password: 'test123'
    public: {username: 'john'}, done

describe 'create user', ->
  it 'should create a document that can be found', (done) ->
    findJohn (user) ->
      should.exist user
      done()

describe 'user password', ->
  it 'should NOT be stored in plaintext', (done) ->
    findJohn (user) ->
      user.password.should.not.eql 'test123'
      done()

  it 'should return true for matching password', (done) ->
    findJohn (user) ->
      user.comparePassword 'test123', (err, isMatch) ->
        isMatch.should.eql true
        done()

  it 'should return false for non-matching password', (done) ->
    findJohn (user) ->
      user.comparePassword 'wrong_password', (err, isMatch) ->
        isMatch.should.not.eql true
        done()

describe 'saving another user with same username', ->
  it 'should produce an error', (done) ->
    User.create public: {username: 'john'}, (err) ->
      should.exist err
      done()

/test/login.coffee:

should = require 'should'
{User} = require '../models'
login = require '../services/login'

before (done) ->

  # Drop all users from database
  User.collection.drop()

  # Create our test user, his username is John Egbert
  User.create
    password: 'test123'
    public: {username: 'john'}, done

describe 'login', ->
  it 'should return true for an existing username/password combo', (done) ->
    login username: 'john', password: 'test123', (err, loggedIn) ->
      should.not.exist(err)
      loggedIn.should.be.true
      done()

  it 'should return false for a bad username/password combo', (done) ->
    login username: 'john', password: 'wrong_pass', (err, loggedIn) ->
      should.not.exist(err)
      loggedIn.should.be.false
      done()

And /models.coffee

fs = require 'fs'
path = require 'path'
mongoose = require 'mongoose'

#Connect to mongodb
#TODO: env variable to choose production/development/testing databases
mongoose.connect 'localhost', 'siglr'

models = {}

for file in fs.readdirSync './schemas'
  if path.extname(file) is '.coffee'
    modelName = path.basename file, '.coffee'
    schema = require "./schemas/#{modelName}"
    models[modelName] = mongoose.model modelName, schema

# key is model name, value is actual mongoose model
module.exports = models
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T07:59:32+00:00Added an answer on June 17, 2026 at 7:59 am

    I found that for some reason the public.username index is removed when running the tests together with npm test. But was maintained when running each test alone.

    I changed the following in the test/models.coffee:

      # Create our test user, his username is John Egbert
      User.create
        password: 'test123'
        public: {username: 'john'}, done
    

    To the following:

      # Create our test user, his username is John Egbert
      User.create
        password: 'test123'
        public: {username: 'john'}, ->
          User.collection.ensureIndex { 'public.username': 1 }, { unique: true }, (err) ->
            throw err if err
            done()
    

    …which calls ensureIndex an internal mongoose function which is called when the model is compiled initially, but is removed for some reason during the test’s life-cycle.

    Using Mongoose 3.5.4

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I realize this question has probably been asked numerous times, but I have not
This probably has a simple answer, but I must not have had enough coffee
I know this probably has been asked before but I am having issues with
This has probably something to do with my transformations, but right now I can't
This has probably been answer already but I am trying to return the primary
I know this has probably been asked before but for my problem I cannot
this question has probably been asked before, but i'm stuck and i've tried a
I know this has been asked probably a thousand times, but I've been biting
This probably has been asked before but all I can find are questions regarding
This probably has probably been asked before, but I couldn't find anything relevant. Would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.