thunderclient / thunder-client-support

Thunder Client is a lightweight Rest API Client Extension for VS Code.
https://www.thunderclient.com
Other
3.61k stars 126 forks source link

Request for mjs support for scripting #1508

Open max-mykhailenko opened 5 months ago

max-mykhailenko commented 5 months ago

Goal — have same validation rules for backend and for frontend.

I tried to use Zod schemas for response validation and I can't because I can't import .ts or .mjs or .js (with import inside) files. I'm trying to achieve DRY and have automatic test for the API.

Tests / Scripting

 const zodSchema = require('./app/schema.mjs'); // or .ts or .js (doesn't work because import inside)
 zodSchema.parse(tc.response.json.data);

As an alternative I can create schemas in openAPI format and convert it to zod schema automatically and at the same time use them as argument for test.

image

May I ask you to suggest another possible solutions?

Paid version

max-mykhailenko commented 5 months ago
image
max-mykhailenko commented 5 months ago

One more question about this topic. Where I can find test's results? I saw them only in console, but this one doesn't look right? Test was broken and better to see that after every request.

rangav commented 5 months ago

Hi @max-mykhailenko

  1. You can import JS files https://github.com/rangav/thunder-client-support/blob/master/docs/filters.md#import-js-files

  2. Test Results are found in Results Tab (near Response) and not in console.

  3. Schema Validation: Please see docs https://github.com/rangav/thunder-client-support/blob/master/docs/testing.md#schema-validation

max-mykhailenko commented 5 months ago
  1. I can't import JS file if file contains import statement. Node import statement doesn't work outside the module. So I should change file extension to mjs, but TC gives bug when I trying to import .mjs

  2. Tests results remains empty.

    
    const z = await tc.loadModule('zod');

const schema = z.object({ id: z.string().nullable(), });

const result = schema.strict().safeParse(tc.response.json.data); expect(result.success).to.equal(true);



3. Could you show example of file for that?
rangav commented 5 months ago
  1. Import statements will not work. please use require();
  2. Your syntax is wrong for Tests - see docs
  3. Example Schema file
    {
    "type": "object",
    "additionalProperties": false,
    "properties": {
        "fraction": {
            "type": "number"
        },
        "balance": {
            "type": "number"
        },
        "bignumber": {
            "type": "integer"
        },
        "isNumber": {
            "type": "null"
        }
    },
    "required": [
        "balance",
        "bignumber",
        "fraction",
        "isNumber"
    ],
    "title": "Welcome4"
    }
max-mykhailenko commented 5 months ago

So I have file test-schema.js

const z = require('zod');
console.log({ z });

const deliveryAddressesCitySchema = z.object({
  id: z.string().nullable(),
});

module.exports = {
  deliveryAddressesCitySchema,
};

In console I got

Script Log: {
  "z": 0 // why do I have zero here? Is it possible to `require` file with another `require`?
}

In test I have

const { deliveryAddressesCitySchema } = require('./app/test-schema.js');

tc.test("Doesn't match schema", function () {
  const result = deliveryAddressesCitySchema.strict().safeParse(tc.response.json.data);
  expect(result.success).to.equal(true);
})
rangav commented 5 months ago

You need to import third party node modules using below syntax

const z = await tc.loadModule("zod");

See docs here https://github.com/rangav/thunder-client-support/blob/master/docs/filters.md#import-node-module

rangav commented 5 months ago

Please read the full scripting docs carefully to avoid issues https://github.com/rangav/thunder-client-support/blob/master/docs/filters.md#inline-scripting

max-mykhailenko commented 5 months ago

@rangav but in this case I will be unable to use that files across the project. I want to have the same schemas for using in code and for tests in TC

rangav commented 5 months ago

You can use those files across the project

Please share code what is the issue?

max-mykhailenko commented 5 months ago

Please share code what is the issue?

How I can use that across the project If I replace const z = require('zod'); to const z = tc.loadModule('zod'); ?

That validation schemas should be used in JS runtime.

rangav commented 5 months ago

The code will be executed at runtime and please test and then let us know if any issues.

max-mykhailenko commented 5 months ago

The code will be executed at runtime and please test and then let us know if any issues.

You don't understand. How my typescript environment will find your local tc variable?

rangav commented 5 months ago

Typescript is not supported, you have to create javascript files and import them into TC scripts

See docs here https://github.com/rangav/thunder-client-support/blob/master/docs/filters.md#import-js-files