简体   繁体   中英

I am trying to run my test script using mocha but there is an error of "Reference Error: beforeEach is not defined"

Mocha test Suite gives a reference error and it says "beforeEach is not defined"

I am trying to run my test script for my todo app in node.js using mocha. But there is a reference error and it says "beforeEach is not defined"

const {app} = require('./../server');
const {Todo} = require('./../models/todo');

beforeEach((done) => {
  Todo.remove({}).then(() => done());
});

describe('POST /todos', () => {
  it('should create a new todo', (done) => {
    var text = 'Test todo text';

request(app)
  .post('/todos')
  .send({text})
  .expect(200)
  .expect((res) => {
    expect(res.body.text).toBe(text);
  })
  .end((err, res) => {
    if (err) {
      return done(err);
    }

    Todo.find().then((todos) => {
      expect(todos.length).toBe(1);
      expect(todos[0].text).toBe(text);
      done();
        }).catch((e) => done(e));
      });
  });

  it('should not create todo with invalid body data', (done) => {
request(app)
  .post('/todos')
  .send({})
  .expect(400)
  .end((err, res) => {
    if (err) {
      return done(err);
    }

    Todo.find().then((todos) => {
      expect(todos.length).toBe(0);
      done();
    }).catch((e) => done(e));
  });
  });
});

Also, I have included all the necessary packages for my package.json file.

My Package.json file is given below

{
  "name": "todo-api",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
  "test": "mocha server/**/*.test.js",
    "test-watch": "nodemon --exec 'npm test' "
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bluebird": "^3.5.3",
    "body-parser": "^1.15.2",
    "express": "^4.14.0",
    "mongodb": "^2.2.5",
    "mongoose": "^4.5.9"
  },
  "devDependencies": {
    "expect": "^1.20.2",
    "mocha": "^3.0.2",
    "nodemon": "^1.10.2",
    "supertest": "^2.0.0"
  }
}

I've just tried to replicate your issue with the test with simple repository using your package.json file. My test file

const expect = require('expect');

beforeEach((done) => {
  done();  
});

describe('just a test', function() {
  it('test', function() {
    expect(true).toBe(true);
  })
});

Then, when running npm t , the test was executed successfully.

在此处输入图片说明

Perhaps there is a misconfiguration in your project.

It looks to me like you are trying to clear out your data in your MongoDB database before each new test of your todo app. So you want to find your collection of todo and drop everything in there before your next test, if so

beforeEach(() => {
 mongoose.connection.collections.todo
});

With the above you are directly referencing the collection of todo sitting inside your database and you call the drop() function on your collection of todo .

beforeEach(() => {
  mongoose.connection.collections.todo.drop();
});

Please let me know if I am correct in fulfilling your requirement. Keep in mind this is an asynchronous operation, so you need to ensure you pause the entire testing environment until the operation is complete, but you already know this, because you have already attempted to implement the done callback.

Also, drop() will accept a callback function like so:

beforeEach((done) => {
  mongoose.connection.collections.todo.drop(() => {});
});

The function will only be executed once you are done with the collection. So you also have to pass the done() callback after defining it like so:

beforeEach((done) => {
   mongoose.connection.collections.todo.drop(() => {
     done();
   });
});

Also, when you run a Mocha test, you do npm run test .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM