{"id":21528245,"url":"https://github.com/znsio/specmatic-node","last_synced_at":"2025-04-09T23:42:45.880Z","repository":{"id":51287323,"uuid":"352910617","full_name":"znsio/specmatic-node","owner":"znsio","description":"Node wrapper for Specmatic","archived":false,"fork":false,"pushed_at":"2025-04-08T09:46:44.000Z","size":2687,"stargazers_count":6,"open_issues_count":19,"forks_count":4,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-04-08T10:32:08.878Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/znsio.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.MD","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-03-30T07:31:41.000Z","updated_at":"2025-04-08T09:46:54.000Z","dependencies_parsed_at":"2024-05-01T17:18:50.658Z","dependency_job_id":"b5180fd7-a1a0-4184-a58f-213703568f1f","html_url":"https://github.com/znsio/specmatic-node","commit_stats":{"total_commits":417,"total_committers":13,"mean_commits":32.07692307692308,"dds":"0.35011990407673865","last_synced_commit":"ae5239de12060d4aa8cabec5310f7dba4921bd69"},"previous_names":[],"tags_count":146,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/znsio%2Fspecmatic-node","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/znsio%2Fspecmatic-node/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/znsio%2Fspecmatic-node/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/znsio%2Fspecmatic-node/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/znsio","download_url":"https://codeload.github.com/znsio/specmatic-node/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247827894,"owners_count":21002967,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-24T01:52:20.900Z","updated_at":"2025-04-09T23:42:45.858Z","avatar_url":"https://github.com/znsio.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Specmatic Framework Node Module\n\n![tests](https://github.com/znsio/specmatic-node/actions/workflows/test.yml/badge.svg)\n[![publish](https://github.com/znsio/specmatic-node/actions/workflows/publish.yml/badge.svg)](https://www.npmjs.com/package/specmatic)\n[![release](https://img.shields.io/npm/v/specmatic)](https://github.com/znsio/specmatic-node/releases/latest)\n\nThis node module is a thin wrapper over the [specmatic executable jar](https://specmatic.io/getting_started.html#setup). All core capabilities are in the main [Specmatic project](https://github.com/znsio/specmatic). The purpose of this wrapper module is to act as a helper with below aspects.\n\n- Easy installation and upgrade of the jar file in node projects through npm\n- Global install using npm and easy run specmatic jar executable without having to download the jar file and having to run `java -jar`\n- Programmatic access to some of the specmatic options as api like start / stop the stub server, setting expecations, running tests. These helpers methods can be used in a javascript project programmatically instead of using cli scripts.\n\n## Table Of Contents\n  - [Quick Start](#quick-start)\n  - [Contract as Stub / Smart Mock (For API clients / consumers)](#contract-as-stub--smart-mock-for-api-clients--consumers)\n  - [Contract as Test (For API Providers / Service)](#contract-as-test-for-api-providers--service)\n  - [Sample Repo](#sample-repo)\n  - [Programmatic Access](#programmatic-access)\n    - [Core APIs](#core-apis)\n    - [Kafka APIs](#kafka-apis)\n  - [IDE Support](#ide-support)\n    - [Jest Framework](#jest-framework)\n  - [Logging](#logging)\n  - [Known Issues](#known-issues)\n    - [1. Node 17/18 - Connection Refused error when connecting to stub](#1-node-1718---connection-refused-error-when-connecting-to-stub)\n    - [2. Error \"ReferenceError: setImmediate is not defined\"](#2-error-referenceerror-setimmediate-is-not-defined)\n    - [3. Specmatic stub is not terminated after test execution](#3-specmatic-stub-is-not-terminated-after-test-execution)\n    - [4. Test results don't show up in IDE](#4-test-results-dont-show-up-in-ide)\n  - [Contribution](#contribution)\n\n## Quick Start\n\n`npm install specmatic` will install the specmatic locally to the node project.\n\nSample npm scripts to run specmatic, (Check [Documentation](https://specmatic.io/documentation.html) for more information on cli commands and arguments.)\n\n## Contract as Stub / Smart Mock (For API clients / consumers)\n\nIn stub mode, Specmatic emulates the Provider / API / Service based on the API Specification so that the consumer / client application can make independent progress. [Learn more](https://specmatic.io/#contract-as-stub).\n\n## Contract as Test (For API Providers / Service)\n\nTests for Free – Specmatic parses your API Specification files and based on this generates requests which are fired at your application. It then verifies if your application’s response is as per your API Specification. All this with a “No Code” approach.. [Learn More](https://specmatic.io/#contract-as-test)\n\n## Sample Repo\n\nhttps://github.com/znsio/specmatic-order-bff-nodejs\n\n## Programmatic Access\n\nSpecmatic JS library exposes some of the commands as methods that can be run programmatically from any javascript testing framework, during setup or test phases.\n\n```javascript\nimport {\n    startHttpStub,\n    setHttpStubExpectationJson,\n    setHttpStubExpectations,\n    stopHttpStub,\n    test,\n    showTestResults,\n    printJarVersion,\n    startKafkaMock,\n    setKafkaMockExpecations,\n    stopKafkaMock,\n    verifyKafkaMock\n} from 'specmatic';\n```\n### Core APIs\n\n`startHttpStub(host?: string, port?: number, args?: (string | number)[]): Promise\u003cStub\u003e` \u003cbr /\u003e\nStart the stub server. Argument `args` values are passed directly to specmatic jar executable.\u003cbr /\u003e\n*Note: This replaces `startStub` method which is deprecated*\n\n`setHttpStubExpectationJson(stubResponse: any, stubServerBaseUrl?: string): Promise\u003cboolean\u003e` \u003cbr /\u003e\nSet stub expectation from a static JSON object. Stub should be running before invoking this method.\u003cbr /\u003e\n\n`setHttpStubExpectations(stubPath: string, stubServerBaseUrl?: string): Promise\u003cboolean\u003e` \u003cbr /\u003e\nSet stub expectation from a file. Stub should be running before invoking this method.\u003cbr /\u003e\n*Note: This replaces `setExpectations` method which is deprecated*\n\n`stopHttpStub(stub: Stub)` \u003cbr /\u003e\nStop the stub server\u003cbr /\u003e\n*Note: This replaces `stopStub` method which is deprecated*\n\n`test(host?: string, port?: string, contractPath?: string, args?: (string | number)[]): Promise\u003c{ [k: string]: number } | undefined\u003e` \u003cbr /\u003e\nRun tests. Argument `args` values are passed directly to specmatic jar executable.\n\n`showTestResults(testFn: (name: string, cb: () =\u003e void) =\u003e void)` \u003cbr /\u003e\nView test results in any framework so that it shows up in IDE specific test results interface. Refer [IDE Support](#ide-support) below for details on how to use this feature.\n\n`printJarVersion()` \u003cbr /\u003e\nmethod to print the version of specmatic.jar\n\n`enableApiCoverage(expressAppRef) ` \u003cbr /\u003e\nenable api coverage for express apps to know which apis and http verbs are covered in contract tests and which not\n\n### Kafka APIs\n\n`startKafkaMock(port?: number, args?: (string | number)[]): Promise\u003cKafkaStub\u003e` \u003cbr /\u003e\nStart kafka stub. Requires an OpenAPI kafka spec in specmatic.json.\u003cbr /\u003e\n*Note: This replaces `startKafkaStub` method which is deprecated*\n\n`setKafkaMockExpectations(stub: KafkaStub, expecations: any): Promise\u003cvoid\u003e` \u003cbr /\u003e\nSet expected message count on Kafka for each topic. Expecations are of the format \n```\n[\n    {\n        \"topic\": \"product-queries\",\n        \"count\": 2\n    },\n    {\n        \"topic\": \"test-topic\",\n        \"count\": 2\n    }\n]\n```\n*Note: This replaces `setKafkaStubExpectations` method which is deprecated*\u003cbr /\u003e\n\n`stopKafkaMock(stub: KafkaStub)` \u003cbr /\u003e\nStop a running kafka stub.\u003cbr /\u003e\n*Note: This replaces `stopKafkaStub` method which is deprecated*\n\n`verifyKafkaMock(stub: KafkaStub): Promise\u003cBoolean\u003e` \u003cbr /\u003e\nVerify all expecations set on Kafka.\u003cbr /\u003e\n*Note: This replaces `verifyKafkaStub` method which is deprecated*\n\n`verifyKafkaMockMessage(stub: KafkaStub, topic: string, value: string): Promise\u003cBoolean\u003e` \u003cbr /\u003e\nVerify kafka message. This is invoked in tests to check on kafka side if a message expected to by pushed by a BFF api is  recieved by Kafka. The Kafka stub starts a verification end point for this purpose which is invoked internally by this api..\u003cbr /\u003e\n*Note: This replaces `verifyKafkaStubMessage` method which is deprecated*\n\n## IDE Support\n\nSpecmatic tests can be displayed in IDE specific test result view by using `showTestResults` method coupled with `test` method. Test framework specific steps are below.\n\n### Jest Framework\nExample: https://github.com/znsio/specmatic-order-bff-nodejs/blob/main/test/contract\n\n1. Call `test` method in a [`globalSetup`](https://jestjs.io/docs/configuration#globalsetup-string) script. `globalSetup` script path can be set either in the jest command line argument or in jest configuration file.\n2. Call `showTestResults` in the root of your test file anywhere. You can pass `test` method of Jest as its argument and it works out of the box.\n\n*Note 1:* Since you are running test method in a `globalSetup` script, any pre-test setup like starting a stub server, app server and any dependent processes like redis server has to be done in the globalSetup script in required sequence before `test` method is called.\n\n*Note 2:* If your project already has a jest globalSetup and or globalTeardown scripts then reuse them but include the necessary code to make IDE integration work.\n\n*Note 3:* If your project uses jest projects support ([`--projects`](https://jestjs.io/docs/configuration#projects-arraystring--projectconfig)), then configure `globalSetup/globalTeardown` in the project specific jest config file\n\n## Logging\n\nBy default only warning and error messages are displayed. You can configure the loglevel in package.json as\n\n```json\n\"specmatic\": {\n    \"logLevel\": \"debug\"\n},\n```\n\nlogLevel accepts all values supported by [winston logger](https://github.com/winstonjs/winston#logging-levels)\n\n## Known Issues\n\n### 1. Node 17/18 - Connection Refused error when connecting to stub\n\nNode 18 apparently shifted to IPv6 as first choice for resolving hostname when both IPv4 and IPv6 addresses are available. This means `localhost` most likely resolves to `::1` rather than `127.0.0.1` or `0.0.0.0`. Now specmatic node wrapper does not start the stub server but the java program under the hood does it and java still resolves to IPv4 address by default. Thus localhost on node v18 and java might resolve to a different address and any connection from node to the running stub will fail. To resolve this, until we have a permanent solution, we request to disable any IPv6 address mapping to a named host in your DNS resolver or `/etc/hosts`.\n\n### 2. Error \"ReferenceError: setImmediate is not defined\"\n\nThis happens due to an issue in Jest framework. The easiest solution is to import `core-js` in the affected test file.\n\n### 3. Specmatic stub is not terminated after test execution\n\nThis happens if stub is not stopped in the same way it is started. There can be two possibilities in case of *Jest* framework\n1. If started from `before*` methods in a test suite, then it should be stopped using `stopStub` method in corresponding `after*` method\n2. If started using `globalSetup` script, then it should be stopped in a `globalTeardown` script\n\n*Note*: If `bail` is set to true in jest config, then any test failure will abort further execution of tests including `after*` methods and `globalTeardown` script. This will prevent stopping your stubs and other processes leaving them hanging and causing port conflicts when tests are run again next.\n\n### 4. Test results don't show up in IDE\n\nWe have tested IDE integration with webstorm and jest framework combination. Visual Studio Code seems to work on and off with Jest. Please follow the instructions mentioned in [IDE Support](#ide-support) to set this up.\n\nAny other test framework can easily be also configured to display test results in IDE test results view by passing a convertor function to the `showTestResults` api.\n\n## Contribution\n\nPlease refer to this [link](CONTRIBUTING.MD)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fznsio%2Fspecmatic-node","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fznsio%2Fspecmatic-node","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fznsio%2Fspecmatic-node/lists"}