usebruno / bruno

Opensource IDE For Exploring and Testing Api's (lightweight alternative to postman/insomnia)
https://www.usebruno.com/
MIT License
27.82k stars 1.29k forks source link

Error saying tests are not defined on running request #3331

Open jonashilmersson opened 1 month ago

jonashilmersson commented 1 month ago

I have checked the following:

Describe the bug

I have downloaded the postman collection for commercetools and setup an environment. When I try to run the "Obtain an access token" I get the following error:

Error invoking remote method 'send-http-request': ReferenceError: tests is not defined

Looking in the Tests tab, there is code for tests. If I clear the test scripts tab, the request works.

The same environment/collection works fine in Postman.

.bru file to reproduce the bug

No response

Screenshots/Live demo link

image

helloanoop commented 1 month ago

@jonashilmersson It appears the test script needs to be updated to match Bruno's format of writing tests. We have some basic documentation here on the syntax of the test function - https://docs.usebruno.com/testing/introduction

If you could your share one of the sample tests, I should be able to share you how the Bruno equivalent looks like?

BTW, You can also checkout Bruno SDK of commercetools - https://docs.commercetools.com/sdk/bruno

jonashilmersson commented 1 month ago

It's the scripts that are included in the Postman collection from commercetools, for example this for the Obtain access token:

tests["Status code is 200"] = responseCode.code === 200;
var data = JSON.parse(responseBody);
if(data.access_token){
    pm.environment.set("ctp_access_token", data.access_token);
}
if (data.scope) {
    parts = data.scope.split(" ");
    parts = parts.filter(scope => scope.includes(":")).map(scope => scope.split(":"))
    if (parts.length > 0) {
        scopeParts = parts[0];
        pm.environment.set("project-key", scopeParts[1]);
        parts = parts.filter(scope => scope.length >= 3)
        if (parts.length > 0) {
            scopeParts = parts[0];
            pm.environment.set("store-key", scopeParts[2]);
        }
    }
}

But if there are Bruno-specific files I guess I can try these, thanks!

jonashilmersson commented 1 month ago

I cloned that repo and just tried to import the bru file at https://github.com/commercetools/commercetools-api-reference/blob/main/bruno/api/auth/clientCredentials.bru as a Bruno collection , but I get an error message saying the import failed.

I also created a new API client in commercetools and downloaded the keys as a Bruno collection, but can't import this either.

bhecquet commented 1 month ago

Hello,

I get the same issue importing a postman collection using the same test format

tests["Status code is 201"] = responseCode.code === 201;

The odd part is that Bruno considers this is a valid code as it's not commented. So I think the issue is more in the import process

helloanoop commented 1 month ago

@bhecquet @jonashilmersson

The equivalent of in Bruno is

test("Status code is 201", function() {
  expect(res.getStatus()).to.equal(200);
});

You could also declaratively write status check tests in bruno in Assertions

image
helloanoop commented 1 month ago

@jonashilmersson your error should go away once you manually update the line having tests["Status code is 200"] = responseCode.code === 200; using the bruno syntax I shared above.'

I cloned that repo and just tried to import the bru file at https://github.com/commercetools/commercetools-api-reference/blob/main/bruno/api/auth/clientCredentials.bru as a Bruno collection , but I get an error message saying the import failed.

You cannot import a single file. After you clone the repo, You need to click on Open Collection and open the folder at commercetools-api-reference/tree/main/bruno

helloanoop commented 1 month ago

@Pragadesh-45 please see if we can add a conversion step for lines like - tests["Status code is 201"] = responseCode.code === 201;

I am assuming we can translate test['test description'] = boolean_condition to

test("test description", function() {
  expect(boolean_condition).to.be.true;
});
Pragadesh-45 commented 1 month ago

@helloanoop Sure, I have started working on this issue.