![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
specmatic
Advanced tools
This node module is a thin wrapper over the specmatic executable jar. All core capabilities are in the main Specmatic project. The purpose of this wrapper module is to act as a helper with below aspects.
java -jar
npm install specmatic
will install the specmatic locally to the node project.
Sample npm scripts to run specmatic, (Check Documentation for more information on cli commands and arguments.)
In 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.
Tests 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://github.com/znsio/specmatic-order-bff-nodejs
Specmatic JS library exposes some of the commands as methods that can be run programmatically from any javascript testing framework, during setup or test phases.
import {
startHttpStub,
setHttpStubExpectationJson,
setHttpStubExpectations,
stopHttpStub,
test,
showTestResults,
printJarVersion,
startKafkaMock,
setKafkaMockExpecations,
stopKafkaMock,
verifyKafkaMock
} from 'specmatic';
startHttpStub(host?: string, port?: number, args?: (string | number)[]): Promise<Stub>
Start the stub server. Argument args
values are passed directly to specmatic jar executable.
Note: This replaces startStub
method which is deprecated
setHttpStubExpectationJson(stubResponse: any, stubServerBaseUrl?: string): Promise<boolean>
Set stub expectation from a static JSON object. Stub should be running before invoking this method.
setHttpStubExpectations(stubPath: string, stubServerBaseUrl?: string): Promise<boolean>
Set stub expectation from a file. Stub should be running before invoking this method.
Note: This replaces setExpectations
method which is deprecated
stopHttpStub(stub: Stub)
Stop the stub server
Note: This replaces stopStub
method which is deprecated
test(host?: string, port?: string, contractPath?: string, args?: (string | number)[]): Promise<{ [k: string]: number } | undefined>
Run tests. Argument args
values are passed directly to specmatic jar executable.
showTestResults(testFn: (name: string, cb: () => void) => void)
View test results in any framework so that it shows up in IDE specific test results interface. Refer IDE Support below for details on how to use this feature.
printJarVersion()
method to print the version of specmatic.jar
enableApiCoverage(expressAppRef)
enable api coverage for express apps to know which apis and http verbs are covered in contract tests and which not
startKafkaMock(port?: number, args?: (string | number)[]): Promise<KafkaStub>
Start kafka stub. Requires an OpenAPI kafka spec in specmatic.json.
Note: This replaces startKafkaStub
method which is deprecated
setKafkaMockExpectations(stub: KafkaStub, expecations: any): Promise<void>
Set expected message count on Kafka for each topic. Expecations are of the format
[
{
"topic": "product-queries",
"count": 2
},
{
"topic": "test-topic",
"count": 2
}
]
Note: This replaces setKafkaStubExpectations
method which is deprecated
stopKafkaMock(stub: KafkaStub)
Stop a running kafka stub.
Note: This replaces stopKafkaStub
method which is deprecated
verifyKafkaMock(stub: KafkaStub): Promise<Boolean>
Verify all expecations set on Kafka.
Note: This replaces verifyKafkaStub
method which is deprecated
verifyKafkaMockMessage(stub: KafkaStub, topic: string, value: string): Promise<Boolean>
Verify 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..
Note: This replaces verifyKafkaStubMessage
method which is deprecated
Specmatic tests can be displayed in IDE specific test result view by using showTestResults
method coupled with test
method. Test framework specific steps are below.
Example: https://github.com/znsio/specmatic-order-bff-nodejs/blob/main/test/contract
test
method in a globalSetup
script. globalSetup
script path can be set either in the jest command line argument or in jest configuration file.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.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.
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.
Note 3: If your project uses jest projects support (--projects
), then configure globalSetup/globalTeardown
in the project specific jest config file
By default only warning and error messages are displayed. You can configure the loglevel in package.json as
"specmatic": {
"logLevel": "debug"
},
logLevel accepts all values supported by winston logger
Node 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
.
This happens due to an issue in Jest framework. The easiest solution is to import core-js
in the affected test file.
This happens if stub is not stopped in the same way it is started. There can be two possibilities in case of Jest framework
before*
methods in a test suite, then it should be stopped using stopStub
method in corresponding after*
methodglobalSetup
script, then it should be stopped in a globalTeardown
scriptNote: 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.
We 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 to set this up.
Any 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.
Please refer to this link
FAQs
Node wrapper for Specmatic
The npm package specmatic receives a total of 2,043 weekly downloads. As such, specmatic popularity was classified as popular.
We found that specmatic demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.