thunder-client-support
thunder-client-support copied to clipboard
Request for mjs support for scripting
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.
May I ask you to suggest another possible solutions?
Paid version
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.
Hi @max-mykhailenko
-
You can import JS files https://github.com/rangav/thunder-client-support/blob/master/docs/filters.md#import-js-files
-
Test Results are found in Results Tab (near Response) and not in console.
-
Schema Validation: Please see docs https://github.com/rangav/thunder-client-support/blob/master/docs/testing.md#schema-validation
-
I can't import JS file if file contains
importstatement. Nodeimportstatement doesn't work outside the module. So I should change file extension tomjs, but TC gives bug when I trying to import.mjs -
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);
- Could you show example of file for that?
- Import statements will not work. please use
require(); - Your syntax is wrong for Tests - see docs
- 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"
}
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);
})
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
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 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
You can use those files across the project
Please share code what is the issue?
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.
The code will be executed at runtime and please test and then let us know if any issues.
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?
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