-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Fix Stub creation for kafka and core - Add tests for multi-port stub and kafka stub
- Loading branch information
1 parent
5b8f5e2
commit 3963e78
Showing
5 changed files
with
213 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import path from 'path'; | ||
import { specmaticKafkaJarName } from '../../config'; | ||
import { ChildProcess, spawn } from 'child_process'; | ||
import { Readable } from 'stream'; | ||
import { mock as jestMock } from 'jest-mock-extended'; | ||
import * as specmatic from '../..'; | ||
import { KafkaStub } from '..'; | ||
|
||
jest.mock('child_process'); | ||
jest.mock('terminate'); | ||
|
||
const SPECMATIC_JAR_PATH = path.resolve(__dirname, '..', '..', '..', '..','specmatic-beta', 'kafka', specmaticKafkaJarName); | ||
const javaProcessMock = jestMock<ChildProcess>(); | ||
const readableMock = jestMock<Readable>(); | ||
javaProcessMock.stdout = readableMock; | ||
javaProcessMock.stderr = readableMock; | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
test('starts the specmatic kafka stub server', async () => { | ||
spawn.mockReturnValue(javaProcessMock); | ||
|
||
setTimeout(() => { | ||
const messageCallback = readableMock.on.mock.calls[0][1]; | ||
messageCallback("[Specmatic::Mock] Starting api server on port:29092"); | ||
messageCallback("[Specmatic::Mock] Kafka started on localhost:9092"); | ||
messageCallback("[Specmatic::Mock] Listening on topics: (product-queries)") | ||
}, 0); | ||
|
||
await expect(specmatic.startKafkaStub()).resolves.toStrictEqual( | ||
new KafkaStub(9092, 29092, javaProcessMock) | ||
); | ||
|
||
expect(spawn.mock.calls[0][1][1]).toBe(`"${path.resolve(SPECMATIC_JAR_PATH)}"`); | ||
expect(spawn.mock.calls[0][1][2]).toBe(""); | ||
}); | ||
|
||
test('takes additional pass through arguments', async () => { | ||
spawn.mockReturnValue(javaProcessMock); | ||
|
||
setTimeout(() => { | ||
const messageCallback = readableMock.on.mock.calls[0][1]; | ||
messageCallback("[Specmatic::Mock] Starting api server on port:29092"); | ||
messageCallback("[Specmatic::Mock] Kafka started on localhost:1234"); | ||
messageCallback("[Specmatic::Mock] Listening on topics: (product-queries)") | ||
}, 0); | ||
|
||
await expect(specmatic.startKafkaStub(1234, ['p1', 'p2'])).resolves.toStrictEqual( | ||
new KafkaStub(1234, 29092, javaProcessMock) | ||
); | ||
|
||
expect(spawn.mock.calls[0][1][1]).toBe(`"${path.resolve(SPECMATIC_JAR_PATH)}"`); | ||
expect(spawn.mock.calls[0][1][2]).toBe(" --port=1234 p1 p2"); | ||
}); | ||
|
||
test('stopStub method stops any running stub server', async () => { | ||
specmatic.stopKafkaMock(new KafkaStub(1234, 29092, javaProcessMock)); | ||
expect(readableMock.removeAllListeners).toHaveBeenCalledTimes(2); | ||
expect(javaProcessMock.removeAllListeners).toHaveBeenCalledTimes(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters