{"id":15846,"library":"swagger-test-templates","title":"Swagger Test Code Generator","description":"swagger-test-templates is a Node.js library designed to generate API test code directly from a Swagger 2.0 specification. It currently stands at version 1.6.0, with its most recent listed releases (1.5.0, 1.4.0) indicating ongoing maintenance rather than rapid feature development. The library differentiates itself by allowing users to specify the assertion format (e.g., 'should', 'expect', 'assert') and the HTTP request module (e.g., 'supertest', 'request') for the generated tests. It also includes capabilities for generating load tests and supports custom Handlebars templates for advanced customization of the test output. A key limitation is its exclusive focus on Swagger 2.0, meaning it does not natively support newer OpenAPI 3.x specifications.","status":"maintenance","version":"1.6.0","language":"javascript","source_language":"en","source_url":"ssh://git@github.com/apigee-127/swagger-test-templates","tags":["javascript","Swagger","API","Test"],"install":[{"cmd":"npm install swagger-test-templates","lang":"bash","label":"npm"},{"cmd":"yarn add swagger-test-templates","lang":"bash","label":"yarn"},{"cmd":"pnpm add swagger-test-templates","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The library primarily exports a single default function. While `require` is shown in examples, ESM import syntax is typically `import stt from 'package-name'` for default exports.","wrong":"import { testGen } from 'swagger-test-templates';","symbol":"stt","correct":"import stt from 'swagger-test-templates';"},{"note":"This is the CommonJS import pattern shown in the official documentation and example usage, suitable for Node.js environments.","symbol":"require('swagger-test-templates')","correct":"const stt = require('swagger-test-templates');"},{"note":"The `swagger-test-templates` module exports its primary function directly as the default export, so you call the imported module reference directly like a function, not a property of it.","wrong":"const tests = stt.testGen(swagger, config);","symbol":"testGen","correct":"const tests = stt(swagger, config);"}],"quickstart":{"code":"const stt = require('swagger-test-templates');\nconst fs = require('fs');\n\n// In a real scenario, replace this with a path to your actual Swagger 2.0 JSON file\nconst swaggerSpec = {\n  \"swagger\": \"2.0\",\n  \"info\": {\"title\": \"Test API\", \"version\": \"1.0.0\"},\n  \"paths\": {\n    \"/user\": {\n      \"get\": {\n        \"responses\": {\n          \"200\": {\"description\": \"OK\"}\n        }\n      }\n    },\n    \"/user/{id}\": {\n      \"parameters\": [{\n        \"name\": \"id\",\n        \"in\": \"path\",\n        \"required\": true,\n        \"type\": \"string\"\n      }],\n      \"get\": {\n        \"responses\": {\n          \"200\": {\"description\": \"OK\"}\n        }\n      }\n    }\n  }\n};\n\nconst config = {\n  assertionFormat: 'should',\n  testModule: 'supertest',\n  pathName: ['/user', '/user/{id}'],\n  pathParams: {\n    \"id\": \"0123\"\n  },\n  // Example of a load test configuration for a specific path/operation\n  loadTest: [{\n    pathName: '/user',\n    operation: 'get',\n    load: {requests: 100, concurrent: 10}\n  }]\n};\n\n// Generates an array of objects containing the test file content and name\nconst tests = stt(swaggerSpec, config);\n\n// Iterate through generated tests and print them (or write to files)\ntests.forEach(testFile => {\n  console.log(`--- Test File: ${testFile.name} ---\\n`);\n  console.log(testFile.test);\n  // Example: Writing to a file\n  // fs.writeFileSync(`${testFile.name}`, testFile.test);\n});\n","lang":"javascript","description":"This quickstart demonstrates how to initialize `swagger-test-templates` with a mock Swagger 2.0 spec and configuration, generating test files for specified paths and including basic load test setup."},"warnings":[{"fix":"Ensure your API specification is in Swagger 2.0 format. For OpenAPI 3.x, consider alternative test generation tools or manual conversion.","message":"This library explicitly supports Swagger Specification version 2.0. It does not support OpenAPI Specification versions 3.x or later. Attempting to use a 3.x spec will lead to generation failures or incorrect tests.","severity":"breaking","affected_versions":"All versions"},{"fix":"If using `templatesPath`, ensure your custom directory contains all necessary Handlebars templates (e.g., base-test.js.handlebars, request-test.js.handlebars, load-test.js.handlebars) or your generated tests may be incomplete.","message":"When providing a `templatesPath` for custom Handlebars templates, it functions as an 'all-or-nothing' path. You must copy all default templates to your custom directory; the library does not merge custom templates with its built-in ones.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Carefully review the `requestData` structure in the documentation, ensuring keys for path, operation, and response code, along with an array of objects containing `body` (for HTTP body) or parameter keys (for path/query params).","message":"The `requestData` option requires a precise nested object structure matching `/endpoint -> operation -> responseCode -> array of data objects`. Incorrect structuring will result in generated tests sending empty or malformed request bodies/parameters.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Explicitly list the paths you wish to test in the `pathName` array. If you truly want all paths, document this choice clearly in your configuration.","message":"The `pathName` configuration option expects an array of specific paths. If an empty array `[]` is provided, the library will generate tests for *all* paths defined in the Swagger specification, which might be unintended for large APIs.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"For CommonJS, use `const stt = require('swagger-test-templates');`. For ESM, use `import stt from 'swagger-test-templates';`. Then call `stt(swagger, config);`.","cause":"The module was imported incorrectly, likely using named import syntax for a default export, or attempting to call a property instead of the root function.","error":"TypeError: stt is not a function"},{"fix":"Add a valid `assertionFormat` property (e.g., 'should', 'expect', or 'assert') to your configuration object: `config: { assertionFormat: 'should', ... }`.","cause":"The `assertionFormat` property was either missing or undefined in the configuration object passed to `stt`.","error":"Error: 'assertionFormat' is required"},{"fix":"Add a valid `testModule` property (e.g., 'supertest' or 'request') to your configuration object: `config: { testModule: 'supertest', ... }`.","cause":"The `testModule` property was either missing or undefined in the configuration object.","error":"Error: 'testModule' is required"},{"fix":"Verify that your `requestData` object precisely follows the `/endpoint -> operation -> responseCode -> array of data` format, and that the data objects themselves contain the correct keys for parameters or the `body` property for request bodies.","cause":"The `requestData` object provided in the configuration does not match the expected nested structure or the Swagger schema for the operation.","error":"Generated test files contain 'undefined' for request bodies/parameters."}],"ecosystem":"npm"}