简体   繁体   中英

testcafe requestLogger only logs the first test in the fixture

So I'm getting better with testcafe and one feature I'd like to learn is its RequestLogger.

So I've created an instance of it

    import { RequestLogger } from 'testcafe';

const logger = RequestLogger(/some reg exp/, {
    logRequestHeaders: true,
    logRequestBody: true
});

export default logger;

and then tried to use it on a sample test fixture:

fixture `REQUEST LOGGER TEST`
.requestHooks(logger);

test('should contain 204 as a status code 1', async t => {
    await t.useRole(role);
    await model.doSmth(t);
    await model.doSmthElse(t);

    console.log(logger.requests);

    await t
        .expect(logger.requests[0].response.statusCode)
        .eql(204);

    await t
        .expect(logger.requests[1].response.statusCode)
        .eql(200);
});

While the first test works just fine, the second one, even if it's the same, will output an empty array once I'll try to console.log(logger.requests)

Any idea how to go about it?

I had the same problem because you have to wait for the Smart Assertion Query Mechanism of Testcafe before doing assertions on Logger.request array.

The documentation tells us that using count or contains predicates make Testcafe use the Smart Assertion Query mechanism

Suggestion from Marion may do the job if your request returns a 200 statusCode :

await t.expect(logger.contains(record => record.response.statusCode === 200)).ok();

I found simpler to do this instead, which does not depend on the returned http status

await t.expect(logger.count(() => true)).eql(1);

replace eql(1) with the number of requests you expect to be logged before doing your assertions

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