首页 > 解决方案 > 测试时出现 CircleCI 错误(我认为与 MongoDB 有关)-无法读取 null 的属性“拆分”

问题描述

我正进入(状态:

TypeError:无法读取 null 的属性“拆分”

在来自 mongoose.connect 的错误对象中,在 CircleCI 中,以及后来的测试超时,第一个失败并出现 500 错误。似乎是 mongo/mongoose 错误,我是否在测试中做错了什么,我的意思是我确定我做错了,因为使用简单的非异步测试我没有收到此错误。顺便说一句,这在本地运行良好,没有错误,只是在 CircleCI 测试中失败。

app.ts 数据库(猫鼬)连接代码:

mongoose
  .connect(mongoUrl, {
    useNewUrlParser: true,
    useCreateIndex: true,
    useUnifiedTopology: true,
    useFindAndModify: false,
    dbName: 'expressing-space',
  })


  .then(() => {
    // console.log('MongoDB connected..');
  })
  .catch((err) => {
    console.log(
      'MongoDB connection error. Please make sure MongoDB is running. ' + err,
    );
  });

测试:

import request from 'supertest';
import mongoose from 'mongoose';
import app from '../src/app';

let token: string = '';
const email = 'x@x.com';
const password = '12345';

beforeAll((done) => {
  request(app)
    .post('/auth/login')
    .send({
      email,
      password,
    })
    .end((err, response) => {
      token = response.body.token;
      done();
    });
});

afterAll((done) => {
  // Closing the DB connection allows Jest to exit successfully.
  mongoose.connection.close();
  done();
});

describe('GET /likes/artists/', () => {
  // token not being sent - should respond with a 401
  it('should return require authorization - return 401', async (done) => {
    const res = await request(app)
      .get('/likes/artists/')
      .set('Content-Type', 'application/json');
    expect(res.status).toBe(401);
    done();
  });

  // send the token - should respond with a 200
  it('should respond with JSON data - 200', async (done) => {
    const res = await request(app)
      .get('/likes/artists/')
      .set('Authorization', `Bearer ${token}`)
      .set('Content-Type', 'application/json');
    expect(res.status).toBe(200);
    expect(res.type).toBe('application/json');
    done();
  });
});

describe('PUT /likes/artists/', () => {
  it('should return require authorization - return 401', async (done) => {
    const res = await request(app)
      .put('/likes/artists')
      .set('Content-Type', 'application/json');
    expect(res.status).toBe(401);
    done();
  });

  // #Todo: There is probalby better way to go about this. Re-check it!
  it('should return json data with either name prop for success or message prop for err', async (done) => {
    const name = 'dmx';
    const res = await request(app)
      .put('/likes/artists')
      .set('Authorization', `Bearer ${token}`)
      .set('Content-Type', 'application/json')
      .send({ name });

    if (res.status === 409) {
      expect(res.status).toBe(409);
      expect(res.body).toHaveProperty('message');
    } else if (res.status === 201) {
      expect(res.status).toBe(201);
      expect(res.body.artist).toHaveProperty('name');
    }
    done();
  });
});

// #Todo: There is probalby better way to go about this. Re-check it!
describe('DELETE /likes/artists/:artistId', () => {
  const artistId = '5e24d7350b68952acdcafc2e';
  it('should return require authorization - return 401', async (done) => {
    const res = await request(app).delete(`/likes/artists/${artistId}`);
    expect(res.status).toBe(401);
    done();
  });

  it('should delete userId from artist if it exists or return 404 if not', async (done) => {
    const res = await request(app)
      .delete(`/likes/artists/${artistId}`)
      .set('Authorization', `Bearer ${token}`);

    if (res.status === 404) {
      expect(res.status).toBe(404);
      expect(res.body).toHaveProperty('message');
    } else if (res.status === 202) {
      expect(res.status).toBe(202);
      expect(res.body).toHaveProperty('message');
    }
    done();
  });
});

标签: mongodbtestingmongoosecontinuous-integrationcircleci

解决方案


将带有 mongoose 的应用程序部署到 Heroku 时发生此错误。我已经用引号设置了环境变量,当我删除它们时问题就消失了。希望这可以帮助。


推荐阅读