Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request for mjs support for scripting #1508

Open
max-mykhailenko opened this issue Mar 21, 2024 · 14 comments
Open

Request for mjs support for scripting #1508

max-mykhailenko opened this issue Mar 21, 2024 · 14 comments
Labels
feature request New feature or request

Comments

@max-mykhailenko
Copy link

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 max-mykhailenko added the feature request New feature or request label Mar 21, 2024
@max-mykhailenko
Copy link
Author

image

@max-mykhailenko
Copy link
Author

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
Copy link
Collaborator

rangav commented Mar 21, 2024

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
Copy link
Author

  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?

@rangav
Copy link
Collaborator

rangav commented Mar 22, 2024

  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
Copy link
Author

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
Copy link
Collaborator

rangav commented Mar 22, 2024

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
Copy link
Collaborator

rangav commented Mar 22, 2024

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
Copy link
Author

@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
Copy link
Collaborator

rangav commented Mar 22, 2024

You can use those files across the project

Please share code what is the issue?

@max-mykhailenko
Copy link
Author

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
Copy link
Collaborator

rangav commented Mar 22, 2024

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

@max-mykhailenko
Copy link
Author

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
Copy link
Collaborator

rangav commented Mar 22, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants