node.js - 将 ChaiHttp 与 beforeEach 或 before 方法一起使用

标签 node.js typescript mocha.js chai

我有一个 NodeJS Express 应用程序,我想对其使用 cookie 进行单元测试。所以我想使用 beforeEach 或 before 来创建 Cookie。

没有任何问题的代码(但没有 before 方法):

import * as chai from 'chai';
import { expect } from 'chai'
import chaiHttp = require('chai-http');


import { app } from '../../server';
describe('Relaties', () => {
    describe('Ophalen alle relaties met: GET /api/ehrm-klantnr/relatie', () => {
        it('should get alle relaties', (done) => {

            let agent = chai.request.agent(app)
            agent
                .put('/api/ehrm-klantnr/medewerker/login')
                .send({ email: 'admin@sfgtest.com', wachtwoord: '<secret>' })
                .then(function (res) {
                    expect(res).to.have.cookie('SESSIONID');
                    // The `agent` now has the sessionid cookie saved, and will send it
                    // back to the server in the next request:
                    return agent.get('/api/ehrm-klantnr/relatie')
                        .set('meta','1')
                        .then(function (res) {
                            expect(res).to.have.status(200);
                            expect(res.body.data[0].vestiging).to.equal('Slib Hoofdkantoor');
                            done();
                        });
                });
        });
    });
});

不运行的是:

import * as chai from 'chai';
import { expect } from 'chai'
import chaiHttp = require('chai-http');

import { app } from '../../server';
 describe('Relaties', () => {
    let agent = chai.request.agent(app);

    describe('First this one', function () {
        beforeEach(function () {
            console.log('outer describe - beforeEach');

            agent
                .put('/api/ehrm-klantnr/medewerker/login')
                .send({ email: 'admin@sfgtest.com', wachtwoord: '<secret>' })
                .then(function (res) {
                    expect(res).to.have.cookie('SESSIONID');
                });
        });
    });

    describe('Ophalen alle relaties met: GET /api/ehrm-klantnr/relatie', () => {
        it('should get alle relaties', (done) => {

            return agent.get('/api/ehrm-klantnr/relatie')
                .set('meta', '1')
                .then(function (res) {
                    expect(res).to.have.status(200);
                    expect(res.body.data[0].vestiging).to.equal('Slib Hoofdkantoor');
                    done();
                });
        });
    });
});

它完全忽略了我的 before 或 beforeEach (这两种方法都不起作用)。 也许 chai-http 没有 before 或 beforeEach 支持? 我做错了什么?

重组后。

describe('Relaties', () => {
    const agent = chai.request.agent(app);

        beforeEach(function (done) {
            console.log('outer describe - beforeEach');

            agent
                .put('/api/ehrm-klantnr/medewerker/login')
                .send({ email: 'admin@sfgtest.com', wachtwoord: '<secret>' })
                .then(function (res) {
                    expect(res).to.have.cookie('SESSIONID');
                    done();
                });
        });


    describe('Ophalen alle relaties met: GET /api/ehrm-klantnr/relatie', () => {
        it('should get alle relaties', (done) => {

            return agent.get('/api/ehrm-klantnr/relatie')
                .set('meta', '1')
                .then(function (res) {
                    expect(res).to.have.status(200);
                    expect(res).to.be.an('object');
                    expect(res.body.data).to.be.an('array');
                    expect(res.body.data[0]).to.be.an('object');
                    expect(res.body.data[0].id).to.equal(1);
                    done();
                });
        });
    });
});

我仍然收到有关 promise 的错误。

最佳答案

如果这对某人有用,这是最终的解决方案:


 describe('Relaties', () => {
    const agent = chai.request.agent(app);

        beforeEach(function (done) {
            console.log('outer describe - beforeEach');

            agent
                .put('/api/ehrm-klantnr/medewerker/login')
                .send({ email: 'admin@sfgtest.com', wachtwoord: '<secret>' })
                .then(function (res) {
                    expect(res).to.have.cookie('SESSIONID');
                    done();
                });
        });


     describe('Ophalen alle relaties met: GET /api/ehrm-klantnr/relatie', () => {
        it('should get alle relaties', (done) => {

            agent.get('/api/ehrm-klantnr/relatie')
                .set('meta', '1')
                .then(function (res) {
                    expect(res).to.have.status(200);
                    done();
                });
        });
    }); 
});

关于node.js - 将 ChaiHttp 与 beforeEach 或 before 方法一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56382865/

相关文章:

javascript - 如何从 javascript 获取 Meteor 的 MongoDB URL

javascript - reactjs typescript 事件未定义

javascript - 如何在 typescript 开关中正确使用枚举

node.js - 使用 mocha-phantomjs 自动化功能测试

node.js - 服务客户端 Jade 模板

javascript - Socket.io 错误请求,响应为 {"code":0 ,"message" :"Transport unknown"}?

javascript - 在没有路径参数值和查询参数的情况下,如何在express中间件中获取我定义的确切URL路径?

javascript - 找不到 Webpack inject.preload.js 文件

javascript - 测试 Promise 链以 .catch 结尾(按 promise 使用 Mocha/Chai)

node.js - Mocha 无法与 Nightwatch 一起运行