User Testing: How to look at Test DB


#1

Hello,

Query around testing. How do I look at what users exist in the test db?

Test Script

import { getUser, getUserName } from './userHelpers.js';

describe('userHelpers', function() {
  if (Meteor.isServer) {
    it ('getUser: it should retrive a user', function() {

      const testUser = {
        email: 'test@test.com',
        password: 'password',
        profile: {
          name: { first: 'Some FName', last: 'Some LName' },
        },
        roles: ['admin'],
      };

      // Create the test user here...
      const userId = Accounts.createUser(testUser);
      Roles.addUsersToRoles(userId, testUser.roles, 'q6a');

      // This is the actual test of the **getUser** function...
      const newUser = getUser(userId);
      chai.assert.typeOf(newUser, 'object');
    });

Function being Tested

export const getUser = (userId) => {
  return Meteor.users.findOne({ _id: userId });
};

The weird part is the error I get on running the test runner:

userHelpers
getUser: it should retrive a user ‣
Error: Email already exists. [403]
    at checkForCaseInsensitiveDuplicates (packages/accounts-password/password_server.js:205:13)
    at _createUser (packages/accounts-password/password_server.js:1001:3)
    at AccountsServer.Accounts.createUser (packages/accounts-password/password_server.js:1072:10)
    at Test.<anonymous> (imports/modules/userHelpers.test.js:36:31)
    at run (packages/practicalmeteor:mocha-core/server.js:34:29)

But how is this possible. The test db should be clean right? So if I rewrite my test like so…

describe('userHelpers', function() {
  if (Meteor.isServer) {
    it ('getUser: it should retrive a user', function() {
      const testUser = {
        email: 'test@test.com',
        password: 'password',
        profile: {
          name: { first: 'Some FName', last: 'Some LName' },
        },
        roles: ['admin'],
      };

      let user = Meteor.users.findOne({ 'emails.address': testUser.email }); // look for the user

      if (!user) { // if user does not exist...
        const userId = Accounts.createUser(testUser);
        Roles.addUsersToRoles(userId, testUser.roles, 'q6a');
        user = Meteor.users.findOne({ _id: userId }); // get newly create user object.
      }

      const newUser = getUser(user._id); // pass same object to getUser function being tested...

      chai.assert.typeOf(newUser, 'object');
    });

A. This works as expected. But why is my user with email address test@test.com persisting in the test db? Should this not be clean every time?
B. I also have a user fixture in my startup code to make life simpler. This is a user with email admin@admin.com. This is found in the test db, but with a different user _id to the normal. This is also persisting in the test db. Why?

it ('getUser: it should retrive admin user', function() {
      let user = Meteor.users.findOne({ 'emails.address': 'admin@admin.com' });
      const newUser = getUser(user._id);
      chai.assert.typeOf(newUser, 'object');
    });

C. How do I look at what is in the test db. It seems when i create the ‘Test User’, it persists. Is this normal?

Thanks so much.

Tat