javascript - Jest - 嵌套 promise 断言永远不会返回

标签 javascript jestjs

我目前正在尝试使用 Jest 进行实时 API 测试。也许有更好的工具,但我想这是另一个讨论。我遇到了一个问题,Jest 返回错误:期望调用一个断言,但收到零个断言调用。当断言位于第二个 Promise 内时。我认为 Jest 会支持 Promise 嵌套,但它似乎没有按预期运行。似乎断言没有被返回。此语法适用于单个 Promise。

我正在使用 Jest V22.4.3 和 Node V8.9.4。

new-ticket.test.js

const call = require('../resources/call');

test('Create a new, valid ticket.', () => {
    expect.assertions(1);

    return call.makePostRequest(~login-url~, {
        'username': 'xxxxx',
        'password': 'xxxxx',
        'version': 'xxxxx'
    }).then((response) => {
        call.makePostRequest(~ticket-url~, {
            'inInvType': 1,
            'inRetailOrClearance': 'R',
            'inAction': 'L',
            'inToken': response.token
        }).then((response) => {
            expect(response.retVal).toBe('0');
        });
    });
});

call.js

const https = require('https');

function makePostRequest(subURL, payload) {
    let options,
        request,
        body;

    // Convert our payload to JSON string.
    payload = JSON.stringify(payload);

    // Build our request options configuration.
    options = {
        hostname: ~base-url~,
        port: 8443,
        "rejectUnauthorized": false,
        path: subURL,
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Accept': '*/*'
        },
        observe: 'body',
        responseType: 'json',
        reportProgress: true,
        withCredentials: false
    };

    body = '';

    return new Promise((resolve) => {
        request = https.request(options, (response) => {

            // Collect our response data as it streams in.
            response.on('data', (data) => {
                body += data;
            });

            // Once ended, resolve with data.
            response.on('end', () => {
                body = JSON.parse(body);
                resolve(body);
            });
        });

        request.on('error', (err) => {
            resolve(err)
        });

        request.write(payload);
        request.end();
    });
}

module.exports.makePostRequest = makePostRequest;

最佳答案

测试如何知道您的测试用例何时完成?

在测试用例中返回 Promise 是正确的想法,但是当您请求票证 URL 时,您的 Promise 链会中断。尝试从该请求返回 Promise。

const call = require('../resources/call');

test('Create a new, valid ticket.', () => {
    expect.assertions(1);

// => Returning the promise is the right idea but ...
    return call.makePostRequest(~login-url~, {
        'username': 'xxxxx',
        'password': 'xxxxx',
        'version': 'xxxxx'
    }).then((response) =>
// ... the inner block doesn't return anything.
// Without a Promise to signal there's async code running,
// Jest won't run this block. Try returning this call (delete the {})
        call.makePostRequest(~ticket-url~, {
            'inInvType': 1,
            'inRetailOrClearance': 'R',
            'inAction': 'L',
            'inToken': response.token
        }).then((response) => {
            expect(response.retVal).toBe('0');
        });
    );
});

如果您对 async/await 感到满意,您也可以使用它:

const call = require('../resources/call');

test('Create a new, valid ticket.', async () => {
    expect.assertions(1);

    let response = await call.makePostRequest(~login-url~, {
        'username': 'xxxxx',
        'password': 'xxxxx',
        'version': 'xxxxx',
    });

    response = await call.makePostRequest(~ticket-url~, {
        'inInvType': 1,
        'inRetailOrClearance': 'R',
        'inAction': 'L',
        'inToken': response.token,
    });

    expect(response.retVal).toBe('0');
});

关于javascript - Jest - 嵌套 promise 断言永远不会返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50206176/

相关文章:

unit-testing - 如何使用 JSDom 对 Jest 中的自定义元素执行单元测试

javascript - 对于调用另一个异步函数的异步函数, Jest 测试失败

php - 显示ACF的中继字段

javascript - package.json 中包含的代理不起作用

javascript - 无法在 PHP 中捕获 POST 变量

javascript - InDesign ScriptUI 窗口出现后立即消失

javascript - Sequelize 关联函数不可用 - IntellIj 问题

vue.js - 使用 jest 和 vue-test-utils 进行 Vue 测试无法解析通过 app.component() 引入的组件

node.js - ReactJs Jest :`jsdom 4. x 及以上版本仅适用于 io.js,不适用于 Node.jsT:

node.js - 使用 Jest 和 Sinon 测试具有属性的函数