specmatic 0.65.2 → 0.65.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.babelrc CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
- "presets": [
3
- "@babel/typescript",
4
- [
5
- "@babel/preset-env",
6
- {
7
- "targets": {
8
- "esmodules": true
9
- }
10
- }
11
- ],
12
- ]
13
- }
2
+ "presets": [
3
+ "@babel/typescript",
4
+ [
5
+ "@babel/preset-env",
6
+ {
7
+ "targets": {
8
+ "esmodules": true
9
+ }
10
+ }
11
+ ],
12
+ ]
13
+ }
@@ -1,15 +1,17 @@
1
1
  # This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
2
2
  # For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages
3
3
 
4
- name: Node.js Package
4
+ name: publish
5
5
 
6
6
  on:
7
- push:
8
- branches:
9
- - master
7
+ workflow_run:
8
+ workflows: ["test"]
9
+ types:
10
+ - completed
10
11
 
11
12
  jobs:
12
13
  build-and-publish:
14
+ if: ${{ github.event.workflow_run.conclusion == 'success' }}
13
15
  runs-on: ubuntu-latest
14
16
  steps:
15
17
  - uses: actions/checkout@v2
@@ -17,8 +19,15 @@ jobs:
17
19
  with:
18
20
  node-version: 14
19
21
  - run: npm install
20
- - run: npm test
21
22
  - run: npm run build
23
+ - run: echo "previous_tag=$(git describe --tags --abbrev=0 2>/dev/null || echo '')" >> $GITHUB_ENV
24
+ - name: Create Tag
25
+ id: create_tag
26
+ uses: jaywcjlove/create-tag-action@main
27
+ if: env.previous_tag
28
+ with:
29
+ package-path: ./package.json
30
+ release: true
22
31
  - uses: JS-DevTools/npm-publish@v1
23
32
  with:
24
33
  check-version: true
@@ -0,0 +1,17 @@
1
+ name: test
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - master
7
+
8
+ jobs:
9
+ test:
10
+ runs-on: ubuntu-latest
11
+ steps:
12
+ - uses: actions/checkout@v2
13
+ - uses: actions/setup-node@v2
14
+ with:
15
+ node-version: 14
16
+ - run: npm install
17
+ - run: npm test
package/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # Specmatic Framework Node Module
2
2
 
3
+ ![tests](https://github.com/znsio/specmatic-node/actions/workflows/test.yml/badge.svg)
4
+ ![publish](https://github.com/znsio/specmatic-node/actions/workflows/npm-publish.yml/badge.svg)
5
+
3
6
  This node module is a thin wrapper over the [standalone executable jar](https://specmatic.in/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 convenience to help with below aspects.
4
7
  * Easy installation and upgrade of the jar file in node projects through npm
5
8
  * JS helper library which provides to do various setup steps like start, stop the specmatic stub server, installing specs etc. These helpers methods can be used inside a setup file inside a javascript project programmatically instead of using cli scripts.
@@ -21,6 +24,9 @@ Tests for Free – Specmatic parses your API Specification files and based on th
21
24
 
22
25
  Check [Documentation](https://specmatic.in/documentation.html) for more information on cli commands and arguments.
23
26
 
27
+ ## Sample Repo
28
+ https://github.com/znsio/specmatic-order-backend-nodejs
29
+
24
30
  ## Programmatic Access
25
31
 
26
32
  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.
package/dist/lib/index.js CHANGED
@@ -62,7 +62,7 @@ var stopStub = javaProcess => {
62
62
 
63
63
  exports.stopStub = stopStub;
64
64
 
65
- var test = (specs, host, port) => {
65
+ var test = (host, port, specs) => {
66
66
  var specsPath = _path.default.resolve(specs + '');
67
67
 
68
68
  var cmd = "java -jar ".concat(specmaticJarPath, " test");
@@ -88,10 +88,15 @@ var setExpectations = (stubPath, stubServerBaseUrl) => {
88
88
  var stubResponse = require(_path.default.resolve(stubPath));
89
89
 
90
90
  console.log('Setting expectations');
91
- (0, _nodeFetch.default)("".concat(stubServerBaseUrl ? stubServerBaseUrl : "http://localhost:9000/", "_specmatic/expectations"), {
92
- method: 'POST',
93
- body: JSON.stringify(stubResponse)
94
- }).then(json => console.log(json));
91
+ return new Promise((resolve, _reject) => {
92
+ (0, _nodeFetch.default)("".concat(stubServerBaseUrl ? stubServerBaseUrl : "http://localhost:9000/", "_specmatic/expectations"), {
93
+ method: 'POST',
94
+ body: JSON.stringify(stubResponse)
95
+ }).then(json => {
96
+ console.log(json);
97
+ resolve(true);
98
+ });
99
+ });
95
100
  };
96
101
 
97
102
  exports.setExpectations = setExpectations;
package/package.json CHANGED
@@ -1,14 +1,11 @@
1
1
  {
2
2
  "name": "specmatic",
3
- "version": "0.65.2",
3
+ "version": "0.65.3",
4
4
  "description": "Node wrapper for Specmatic",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
7
7
  "test": "jest",
8
- "build": "npm test && rimraf dist && ./node_modules/.bin/babel src --extensions '.ts,.js' --ignore '**/__tests__/*.ts' -d dist",
9
- "build-and-publish-patch": "npm run build && npm version patch && npm publish",
10
- "build-and-publish-minor": "npm run build && npm version minor && npm publish",
11
- "build-and-publish-major": "npm run build && npm version major && npm publish"
8
+ "build": "npm test && rimraf dist && ./node_modules/.bin/babel src --extensions '.ts,.js' --ignore '**/__tests__/*.ts' -d dist"
12
9
  },
13
10
  "repository": {
14
11
  "type": "git",
package/src/app.d.ts CHANGED
@@ -1 +1 @@
1
- declare module 'exec-sh';
1
+ declare module 'exec-sh';
@@ -6,8 +6,8 @@ import { specmaticJarPathLocal } from '../../config';
6
6
  jest.mock('exec-sh');
7
7
 
8
8
  test('arguments to be passed correctly to Specmatic lib', () => {
9
- const testArgs = ['node', 'index.js', 'stub', '*.specmatic', '--data', 'src/mocks', '--host', 'localhost', '--port', '8000'];
10
- startSpecmaticServer(testArgs);
11
- expect(execSh.mock.calls[0][0]).toBe(`java -jar ${path.resolve(specmaticJarPathLocal)} ${testArgs.slice(2).join(' ')}`);
12
- expect(execSh).toHaveBeenCalledTimes(1);
9
+ const testArgs = ['node', 'index.js', 'stub', '*.specmatic', '--data', 'src/mocks', '--host', 'localhost', '--port', '8000'];
10
+ startSpecmaticServer(testArgs);
11
+ expect(execSh.mock.calls[0][0]).toBe(`java -jar ${path.resolve(specmaticJarPathLocal)} ${testArgs.slice(2).join(' ')}`);
12
+ expect(execSh).toHaveBeenCalledTimes(1);
13
13
  });
package/src/bin/core.ts CHANGED
@@ -3,20 +3,16 @@ import path from 'path';
3
3
  import { specmaticJarPathLocal } from '../config';
4
4
 
5
5
  const callSpecmaticCli = (args?: string[]) => {
6
- const specmaticJarPath = path.resolve(specmaticJarPathLocal);
7
- const cliArgs = (args || process.argv).slice(2).join(' ');
6
+ const specmaticJarPath = path.resolve(specmaticJarPathLocal);
7
+ const cliArgs = (args || process.argv).slice(2).join(' ');
8
8
 
9
- console.log('starting specmatic server', cliArgs)
10
- execSh(
11
- `java -jar ${specmaticJarPath} ${cliArgs}`,
12
- {},
13
- (err: any) => {
14
- if (err) {
15
- console.log('Exit code: ', err.code);
16
- process.exit(err.code);
17
- }
18
- }
19
- );
9
+ console.log('starting specmatic server', cliArgs);
10
+ execSh(`java -jar ${specmaticJarPath} ${cliArgs}`, {}, (err: any) => {
11
+ if (err) {
12
+ console.log('Exit code: ', err.code);
13
+ process.exit(err.code);
14
+ }
15
+ });
20
16
  };
21
17
 
22
- export default callSpecmaticCli;
18
+ export default callSpecmaticCli;
package/src/bin/index.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  #!/usr/bin/env node
2
2
  import callSpecmaticCli from './core';
3
3
 
4
- callSpecmaticCli();
4
+ callSpecmaticCli();
package/src/config.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  export const specmaticJarPathLocal = './node_modules/specmatic/specmatic.jar';
2
2
  export const specmaticJarPathRemote = 'https://github.com/znsio/specmatic/releases/download/0.24.2/specmatic.jar';
3
- export const specmatic = 'specmatic.json'
3
+ export const specmatic = 'specmatic.json'
package/src/index.ts CHANGED
@@ -1,7 +1 @@
1
- export {
2
- startStub,
3
- stopStub,
4
- test,
5
- setExpectations,
6
- printJarVersion
7
- } from './lib';
1
+ export { startStub, stopStub, test, setExpectations, printJarVersion } from './lib';
@@ -3,120 +3,146 @@ import fetch from 'node-fetch';
3
3
  import path from 'path';
4
4
  import { ChildProcess } from 'child_process';
5
5
  import { mock as jestMock } from 'jest-mock-extended';
6
- import { Readable } from "stream";
6
+ import { Readable } from 'stream';
7
7
 
8
8
  import * as specmatic from '../';
9
9
  import { startStub, stopStub, printJarVersion, setExpectations } from '../';
10
10
  import { specmaticJarPathLocal } from '../../config';
11
- import mockStub from '../../../mockStub.json';
11
+ import mockStub from '../../../sample-mock-stub.json';
12
12
 
13
13
  jest.mock('exec-sh');
14
14
  jest.mock('node-fetch');
15
15
 
16
- const contractsPath = './contracts';
17
- const stubDataPath = './data';
18
- const host = 'localhost';
19
- const port = '8000';
16
+ const CONTRACT_YAML_FILE_PATH = './contracts';
17
+ const STUB_DIR_PATH = './data';
18
+ const HOST = 'localhost';
19
+ const PORT = '8000';
20
+
20
21
  const javaProcessMock = jestMock<ChildProcess>();
21
22
  const readableMock = jestMock<Readable>();
22
23
  javaProcessMock.stdout = readableMock;
23
24
  javaProcessMock.stderr = readableMock;
24
25
 
25
26
  beforeEach(() => {
26
- execSh.mockReset();
27
- fetch.mockReset();
27
+ execSh.mockReset();
28
+ fetch.mockReset();
28
29
  });
29
30
 
30
31
  test('startStub method starts the specmatic stub server', async () => {
31
- execSh.mockReturnValue(javaProcessMock);
32
+ execSh.mockReturnValue(javaProcessMock);
32
33
 
33
- startStub(host, port, stubDataPath).then(javaProcess => {
34
- expect(javaProcess).toBe(javaProcessMock);
35
- })
34
+ startStub(HOST, PORT, STUB_DIR_PATH).then(javaProcess => {
35
+ expect(javaProcess).toBe(javaProcessMock);
36
+ });
36
37
 
37
- readableMock.on.mock.calls[0][1]("Stub server is running");
38
+ readableMock.on.mock.calls[0][1]('Stub server is running');
38
39
 
39
- expect(execSh).toHaveBeenCalledTimes(1);
40
- expect(execSh.mock.calls[0][0])
41
- .toBe(`java -jar ${path.resolve(specmaticJarPathLocal)} stub --data=${path.resolve(stubDataPath)} --host=${host} --port=${port}`);
40
+ expect(execSh).toHaveBeenCalledTimes(1);
41
+ expect(execSh.mock.calls[0][0]).toBe(
42
+ `java -jar ${path.resolve(specmaticJarPathLocal)} stub --data=${path.resolve(STUB_DIR_PATH)} --host=${HOST} --port=${PORT}`
43
+ );
42
44
  });
43
45
 
44
46
  test('startStub method stubDir is optional', async () => {
45
- execSh.mockReturnValue(javaProcessMock);
47
+ execSh.mockReturnValue(javaProcessMock);
46
48
 
47
- startStub(host, port).then(javaProcess => {
48
- expect(javaProcess).toBe(javaProcessMock);
49
- })
49
+ startStub(HOST, PORT).then(javaProcess => {
50
+ expect(javaProcess).toBe(javaProcessMock);
51
+ });
50
52
 
51
- readableMock.on.mock.calls[0][1]("Stub server is running");
53
+ readableMock.on.mock.calls[0][1]('Stub server is running');
52
54
 
53
- expect(execSh).toHaveBeenCalledTimes(1);
54
- expect(execSh.mock.calls[0][0])
55
- .toBe(`java -jar ${path.resolve(specmaticJarPathLocal)} stub --host=${host} --port=${port}`);
55
+ expect(execSh).toHaveBeenCalledTimes(1);
56
+ expect(execSh.mock.calls[0][0]).toBe(`java -jar ${path.resolve(specmaticJarPathLocal)} stub --host=${HOST} --port=${PORT}`);
56
57
  });
57
58
 
58
59
  test('startStub method host and port are optional', async () => {
59
- execSh.mockReturnValue(javaProcessMock);
60
+ execSh.mockReturnValue(javaProcessMock);
60
61
 
61
- startStub().then(javaProcess => {
62
- expect(javaProcess).toBe(javaProcessMock);
63
- })
62
+ startStub().then(javaProcess => {
63
+ expect(javaProcess).toBe(javaProcessMock);
64
+ });
64
65
 
65
- readableMock.on.mock.calls[0][1]("Stub server is running");
66
+ readableMock.on.mock.calls[0][1]('Stub server is running');
66
67
 
67
- expect(execSh).toHaveBeenCalledTimes(1);
68
- expect(execSh.mock.calls[0][0])
69
- .toBe(`java -jar ${path.resolve(specmaticJarPathLocal)} stub`);
68
+ expect(execSh).toHaveBeenCalledTimes(1);
69
+ expect(execSh.mock.calls[0][0]).toBe(`java -jar ${path.resolve(specmaticJarPathLocal)} stub`);
70
70
  });
71
71
 
72
72
  test('stopStub method stops any running stub server', () => {
73
- stopStub(javaProcessMock);
73
+ stopStub(javaProcessMock);
74
74
 
75
- expect(readableMock.removeAllListeners).toHaveBeenCalledTimes(2);
76
- expect(javaProcessMock.removeAllListeners).toHaveBeenCalledTimes(1);
77
- expect(javaProcessMock.kill).toHaveBeenCalledTimes(1);
75
+ expect(readableMock.removeAllListeners).toHaveBeenCalledTimes(2);
76
+ expect(javaProcessMock.removeAllListeners).toHaveBeenCalledTimes(1);
77
+ expect(javaProcessMock.kill).toHaveBeenCalledTimes(1);
78
78
  });
79
79
 
80
80
  test('test runs the contract tests', function (done) {
81
- specmatic.test(contractsPath, host, port).then(result => {
82
- expect(result).toBeTruthy();
83
- done();
81
+ specmatic.test(HOST, PORT, CONTRACT_YAML_FILE_PATH).then(result => {
82
+ expect(result).toBeTruthy();
83
+ done();
84
+ });
85
+ execSh.mock.calls[0][2](); //Execute the callback
86
+ expect(execSh).toHaveBeenCalledTimes(1);
87
+ expect(execSh.mock.calls[0][0]).toBe(
88
+ `java -jar ${path.resolve(specmaticJarPathLocal)} test ${path.resolve(CONTRACT_YAML_FILE_PATH)} --host=${HOST} --port=${PORT}`
89
+ );
90
+ });
91
+
92
+ test('test runs the contract tests with host and port optional', function (done) {
93
+ specmatic.test().then(result => {
94
+ expect(result).toBeTruthy();
95
+ done();
84
96
  });
85
97
  execSh.mock.calls[0][2](); //Execute the callback
86
98
  expect(execSh).toHaveBeenCalledTimes(1);
87
- expect(execSh.mock.calls[0][0])
88
- .toBe(`java -jar ${path.resolve(specmaticJarPathLocal)} test ${path.resolve(contractsPath)} --host=${host} --port=${port}`);
89
- });
99
+ expect(execSh.mock.calls[0][0]).toBe(`java -jar ${path.resolve(specmaticJarPathLocal)} test`);
100
+ });
101
+
102
+ test('test runs the contract tests with contracts path optional', function (done) {
103
+ specmatic.test(HOST, PORT).then(result => {
104
+ expect(result).toBeTruthy();
105
+ done();
106
+ });
107
+ execSh.mock.calls[0][2](); //Execute the callback
108
+ expect(execSh).toHaveBeenCalledTimes(1);
109
+ expect(execSh.mock.calls[0][0]).toBe(`java -jar ${path.resolve(specmaticJarPathLocal)} test --host=${HOST} --port=${PORT}`);
110
+ });
90
111
 
91
112
  test('printJarVersion', () => {
92
- printJarVersion();
113
+ printJarVersion();
93
114
 
94
- expect(execSh).toHaveBeenCalledTimes(1);
95
- expect(execSh.mock.calls[0][0])
96
- .toBe(`java -jar ${path.resolve(specmaticJarPathLocal)} --version`);
115
+ expect(execSh).toHaveBeenCalledTimes(1);
116
+ expect(execSh.mock.calls[0][0]).toBe(`java -jar ${path.resolve(specmaticJarPathLocal)} --version`);
97
117
  });
98
118
 
99
- test('setExpectations with default baseUrl', () => {
100
- fetch.mockReturnValue(Promise.resolve("{}"));
101
- setExpectations(path.resolve('./mockStub.json'));
119
+ test('setExpectations with default baseUrl', done => {
120
+ fetch.mockReturnValue(Promise.resolve('{}'));
121
+ setExpectations(path.resolve('./sample-mock-stub.json')).then(result => {
122
+ expect(result).toBeTruthy();
123
+ done();
124
+ });
102
125
 
103
- expect(fetch).toHaveBeenCalledTimes(1);
104
- expect(fetch.mock.calls[0][0]).toBe('http://localhost:9000/_specmatic/expectations');
105
- expect(fetch.mock.calls[0][1]).toMatchObject({
106
- method: 'POST',
107
- body: JSON.stringify(mockStub)
108
- });
126
+ expect(fetch).toHaveBeenCalledTimes(1);
127
+ expect(fetch.mock.calls[0][0]).toBe('http://localhost:9000/_specmatic/expectations');
128
+ expect(fetch.mock.calls[0][1]).toMatchObject({
129
+ method: 'POST',
130
+ body: JSON.stringify(mockStub),
131
+ });
109
132
  });
110
133
 
111
- test('setExpectations with a different baseUrl for the stub server', () => {
112
- fetch.mockReturnValue(Promise.resolve("{}"));
113
- const stubServerBaseUrl = 'http://localhost:8000/'
114
- setExpectations(path.resolve('./mockStub.json'), stubServerBaseUrl);
115
-
116
- expect(fetch).toHaveBeenCalledTimes(1);
117
- expect(fetch.mock.calls[0][0]).toBe(`${stubServerBaseUrl}_specmatic/expectations`);
118
- expect(fetch.mock.calls[0][1]).toMatchObject({
119
- method: 'POST',
120
- body: JSON.stringify(mockStub)
121
- });
134
+ test('setExpectations with a different baseUrl for the stub server', done => {
135
+ fetch.mockReturnValue(Promise.resolve('{}'));
136
+ const stubServerBaseUrl = 'http://localhost:8000/';
137
+ setExpectations(path.resolve('./sample-mock-stub.json'), stubServerBaseUrl).then(result => {
138
+ expect(result).toBeTruthy();
139
+ done();
140
+ });
141
+
142
+ expect(fetch).toHaveBeenCalledTimes(1);
143
+ expect(fetch.mock.calls[0][0]).toBe(`${stubServerBaseUrl}_specmatic/expectations`);
144
+ expect(fetch.mock.calls[0][1]).toMatchObject({
145
+ method: 'POST',
146
+ body: JSON.stringify(mockStub),
147
+ });
122
148
  });
package/src/lib/index.ts CHANGED
@@ -6,7 +6,7 @@ import { ChildProcess } from 'child_process';
6
6
 
7
7
  const specmaticJarPath = path.resolve(specmaticJarPathLocal);
8
8
 
9
- const startStub = (host?: string, port?: string, stubDir?: string) : Promise<ChildProcess> => {
9
+ const startStub = (host?: string, port?: string, stubDir?: string): Promise<ChildProcess> => {
10
10
  const stubs = path.resolve(stubDir + '');
11
11
 
12
12
  var cmd = `java -jar ${specmaticJarPath} stub`;
@@ -25,7 +25,7 @@ const startStub = (host?: string, port?: string, stubDir?: string) : Promise<Chi
25
25
  javaProcess.stdout.on('data', function (data: String) {
26
26
  console.log('STDOUT: ' + data);
27
27
  if (data.indexOf('Stub server is running') > -1) {
28
- resolve(javaProcess);
28
+ resolve(javaProcess);
29
29
  }
30
30
  });
31
31
  javaProcess.stderr.on('data', function (data: String) {
@@ -42,7 +42,7 @@ const stopStub = (javaProcess: ChildProcess) => {
42
42
  javaProcess.kill();
43
43
  };
44
44
 
45
- const test = (specs?: string, host?: string, port?: string): Promise<boolean> => {
45
+ const test = (host?: string, port?: string, specs?: string): Promise<boolean> => {
46
46
  const specsPath = path.resolve(specs + '');
47
47
 
48
48
  var cmd = `java -jar ${specmaticJarPath} test`;
@@ -63,14 +63,20 @@ const test = (specs?: string, host?: string, port?: string): Promise<boolean> =>
63
63
  });
64
64
  };
65
65
 
66
- const setExpectations = (stubPath: string, stubServerBaseUrl?: string) => {
66
+ const setExpectations = (stubPath: string, stubServerBaseUrl?: string): Promise<boolean> => {
67
67
  const stubResponse = require(path.resolve(stubPath));
68
68
 
69
69
  console.log('Setting expectations');
70
- fetch(`${stubServerBaseUrl ? stubServerBaseUrl : `http://localhost:9000/`}_specmatic/expectations`, {
71
- method: 'POST',
72
- body: JSON.stringify(stubResponse),
73
- }).then(json => console.log(json));
70
+
71
+ return new Promise((resolve, _reject) => {
72
+ fetch(`${stubServerBaseUrl ? stubServerBaseUrl : `http://localhost:9000/`}_specmatic/expectations`, {
73
+ method: 'POST',
74
+ body: JSON.stringify(stubResponse),
75
+ }).then(json => {
76
+ console.log(json);
77
+ resolve(true);
78
+ });
79
+ });
74
80
  };
75
81
 
76
82
  const printJarVersion = () => {
File without changes
File without changes