使用之前测试的 Mocha 和 Chai 消耗值进行测试

Testing with Mocha and Chai consuming values from previous tests

我有一些使用 mochachai 使用 TypeScript 创建的测试,它们实际上按预期工作。每个函数 returns 一个 Promise 运行一个 test.

我的问题是,是否可以在每个 test 上消耗前一个 test 返回的值,而不使用您在下面看到的嵌套。

我担心的是,如果我有更多 test 嵌套代码向右移动可能会很烦人

import * as request from 'supertest';
import app from '../src/app';
import { Promise } from 'bluebird';
import * as dateformat from 'dateformat';
import Commons from '../../utils/commons';
import { expect } from 'chai';

...
// all the functions used below are defined over here
...

registerNonExistingUser(email, pass, role).then(
    (jwtToken: string) => {
        authenticateUserCorrectJwt(jwtToken).then(
            (user) => {
                authenticateUserWrongJwt().then(
                    () => {
                        loginUserWrongCredentials().then(
                            () => {
                                loginUserCorrectCredentials(email, pass).then(
                                    (jwtToken: string) => {
                                        getLocalUserInfoCorrectJwt(jwtToken, email).then(
                                            (user) => {
                                                createTodoAsEditor(jwtToken, todoTitle).then(
                                                    (todos) => {
                                                        checkTodoExistsWithCorrectTitle(jwtToken, todoTitle).then(
                                                            (todo: any) => {
                                                                deleteTodoWithCorrectIdAsEditor(jwtToken, todo._id).then(
                                                                    (todo) => {
                                                                        unregisterExistingUser({
                                                                            'local.email': email,
                                                                        }).then(
                                                                            (user) => {
                                                                                // console.log(Commons.stringify(user));
                                                                            }
                                                                        );
                                                                    }
                                                                );
                                                            }
                                                        );
                                                    }
                                                );
                                            }
                                        );
                                    }
                                );
                            }
                        );
                    }
                );
            }
        );
    }
);

知道如何美化它吗?

[编辑 1]

@bhoo-day 建议成功了:

registerNonExistingUser(email, pass, role)
    .then((_jwtToken: string) => { 
        return authenticateUserCorrectJwt(_jwtToken);
    })
    .then((user) => {
        return authenticateUserWrongJwt();
    })
    ...

现在我想知道我是否可以转换链的开头,以便成为如下所示(我尝试过但不起作用)。我的目标是将每个功能放在同一级别,包括第一个功能:

Promise.resolve()
    .then(() => { 
        return registerNonExistingUser(email, pass, role);
    })
    .then((jwtToken: string) => { 
        return authenticateUserCorrectJwt(jwtToken);
    })
    .then((user) => {
        return authenticateUserWrongJwt();
    })
    ...

[编辑 2]

我尝试了以下方法并且有效。你知道如何简化它吗?,也许使用:Promise.resolve()...?

new Promise((resolve) => {
    it('dummy', (done) => { resolve(); return done(); });
})
.then(() => { 
    return registerNonExistingUser(email, pass, role);
})
.then((_jwtToken: string) => { 
    return authenticateUserCorrectJwt(_jwtToken);
})

谢谢!

如果您使用 promise,您可以避免这种回调地狱,因为这是 promise 的主要目的。

解决问题的一些想法。

// temp variable to store jwt token
let token;

registerNonExistingUser(email, pass, role)
  .then((jwtToken: string) => { 
    token = jwtToken; // assign it so other functions can use it
    return authenticateUserCorrectJwt(jwtToken)
  })
  .then((user) => authenticateUserWrongJwt())
  .then(() => loginUserWrongCredentials())
  .then(() => loginUserCorrectCredentials(email, pass))
  .then((jwtToken: string) => getLocalUserInfoCorrectJwt(jwtToken, email))
  .then((user) => createTodoAsEditor(token, todoTitle))
  .then((todos) => checkTodoExistsWithCorrectTitle(token, todoTitle))
  .then((todo: any) => deleteTodoWithCorrectIdAsEditor(token, todo._id))
  .then((todo) => unregisterExistingUser({ 'local.email': email }))
  .then((user) => {
    // console.log(Commons.stringify(user));
  });

或者如果您使用 node 7.6.0 或更高版本,则可以使用 async/await。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await

async function printUser() {    
    try { 
      let jwtToken = await registerNonExistingUser(email, pass, role);
      const user = await authenticateUserCorrectJwt(jwtToken);

      await loginUserWrongCredentials();
      jwtToken = await loginUserCorrectCredentials(email, pass);
      const user = await getLocalUserInfoCorrectJwt(jwtToken, email);
      const todos = await createTodoAsEditor(token, todoTitle);
      const todo = await checkTodoExistsWithCorrectTitle(token, todoTitle)

      const todo = await deleteTodoWithCorrectIdAsEditor(token, todo._id);
      const user = await unregisterExistingUser({ 'local.email': email });

      console.log(Commons.stringify(user));
    } catch (error) {
      // catch error here
    }
}