thunder-client-support icon indicating copy to clipboard operation
thunder-client-support copied to clipboard

Request for mjs support for scripting

Open max-mykhailenko opened this issue 1 year ago • 14 comments

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 avatar Mar 21 '24 15:03 max-mykhailenko

image

max-mykhailenko avatar Mar 21 '24 16:03 max-mykhailenko

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.

max-mykhailenko avatar Mar 21 '24 16:03 max-mykhailenko

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

rangav avatar Mar 21 '24 16:03 rangav

  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);
  1. Could you show example of file for that?

max-mykhailenko avatar Mar 22 '24 07:03 max-mykhailenko

  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"
}

rangav avatar Mar 22 '24 08:03 rangav

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);
})

max-mykhailenko avatar Mar 22 '24 10:03 max-mykhailenko

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 avatar Mar 22 '24 10:03 rangav

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

rangav avatar Mar 22 '24 10:03 rangav

@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

max-mykhailenko avatar Mar 22 '24 11:03 max-mykhailenko

You can use those files across the project

Please share code what is the issue?

rangav avatar Mar 22 '24 11:03 rangav

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.

max-mykhailenko avatar Mar 22 '24 11:03 max-mykhailenko

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

rangav avatar Mar 22 '24 11:03 rangav

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?

max-mykhailenko avatar Mar 22 '24 17:03 max-mykhailenko

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

rangav avatar Mar 22 '24 17:03 rangav