Comparing version 1.2.0 to 2.0.0
@@ -6,3 +6,3 @@ Feature: Basic steps | ||
Scenario: I can call steps with and without a result | ||
Given I navigate to http://localhost:9000 PASSED | ||
Given I navigate to http://localhost:9999 PASSED | ||
And StepRegistry client SimpleStepPublisher is connected PASSED | ||
@@ -12,15 +12,31 @@ Check I can call a step with a result PASSED hello! | ||
Scenario: I can call steps which fail | ||
Given I navigate to http://localhost:9000 PASSED | ||
Given I navigate to http://localhost:9999 PASSED | ||
And StepRegistry client SimpleStepPublisher is connected PASSED | ||
Check I can call a step with a result PASSED hello! | ||
And I can call a step which fails FAILED (WebSocketClientStepInvoker:174)-StepFailedException Expected true to be false | ||
And I can call a step which fails FAILED (WebSocketClientStepInvoker:181)-StepFailedException Expected true to be false | ||
Scenario: I can call steps which are async | ||
Given I navigate to http://localhost:9000 PASSED | ||
Given I navigate to http://localhost:9999 PASSED | ||
And StepRegistry client SimpleStepPublisher is connected PASSED | ||
Check I can call a step which succeeds asynchronously PASSED true | ||
And I can call a step which times out FAILED (WebSocketClientStepInvoker:174)-StepFailedException Expected false to be true | ||
Check I can call a step which succeeds asynchronously PASSED true | ||
And I can call a step which times out FAILED (WebSocketClientStepInvoker:181)-StepFailedException Expected false to be true | ||
Scenario: I can read a variable from the Chorus Context | ||
Given I navigate to http://localhost:9999 PASSED | ||
And StepRegistry client SimpleStepPublisher is connected PASSED | ||
Given I create a variable outbound with the value one PASSED | ||
Then in chorus-js 'outbound' has the value 'one' PASSED | ||
Scenario: I can set a variable in the Chorus Context | ||
Given I navigate to http://localhost:9999 PASSED | ||
And StepRegistry client SimpleStepPublisher is connected PASSED | ||
When I set the 'inbound' variable to 'two' in chorus-js PASSED | ||
Then the variable inbound has the value two PASSED | ||
Scenario: I can overwrite a variable in the Chorus Context | ||
Given I navigate to http://localhost:9999 PASSED | ||
And StepRegistry client SimpleStepPublisher is connected PASSED | ||
Given I create a variable outbound with the value two PASSED | ||
When I set the 'outbound' variable to 'three' in chorus-js PASSED | ||
Then the variable outbound has the value three PASSED | ||
Features (total:1) (passed:0) (failed:1) | ||
Scenarios (total:3) (passed:1) (failed:2) | ||
Steps (total:14) (passed:12) (failed:2) (undefined:0) (pending:0) (skipped:0) | ||
Scenarios (total:6) (passed:4) (failed:2) | ||
Steps (total:27) (passed:25) (failed:2) (undefined:0) (pending:0) (skipped:0) |
@@ -6,7 +6,7 @@ Feature: I can use the counter | ||
Scenario: I can read the counter | ||
Given I navigate to http://localhost:9000 PASSED | ||
Given I navigate to http://localhost:9999 PASSED | ||
And StepRegistry client SimpleStepPublisher is connected PASSED | ||
Then the counter value is 0 PASSED 0 | ||
Scenario: I can increment the counter | ||
Given I navigate to http://localhost:9000 PASSED | ||
Given I navigate to http://localhost:9999 PASSED | ||
And StepRegistry client SimpleStepPublisher is connected PASSED | ||
@@ -17,3 +17,3 @@ Given the counter value is 0 PASSED 0 | ||
Scenario: I can decrement the counter | ||
Given I navigate to http://localhost:9000 PASSED | ||
Given I navigate to http://localhost:9999 PASSED | ||
And StepRegistry client SimpleStepPublisher is connected PASSED | ||
@@ -20,0 +20,0 @@ Given the counter value is 0 PASSED 0 |
@@ -36,7 +36,11 @@ 'use strict'; | ||
executionId = incomingMessage.executionId, | ||
args = incomingMessage.arguments, | ||
contextVariables = incomingMessage.contextVariables; | ||
var contextVars = contextVariables; | ||
var context = (0, _createContext2.default)(contextVars); | ||
try { | ||
var result = _callback.apply(undefined, _toConsumableArray(incomingMessage.arguments)); | ||
var result = _callback(args, context); | ||
var updatedContextVariables = context.toObject(); | ||
var message = { | ||
@@ -48,3 +52,3 @@ type: 'STEP_SUCCEEDED', | ||
result: result, | ||
contextVariables: contextVariables | ||
contextVariables: updatedContextVariables | ||
}; | ||
@@ -123,2 +127,6 @@ _sendMessage(message); | ||
var _createContext = require('./createContext'); | ||
var _createContext2 = _interopRequireDefault(_createContext); | ||
var _openWebSocket = require('./openWebSocket'); | ||
@@ -128,4 +136,2 @@ | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } |
{ | ||
"name": "chorus-js", | ||
"version": "1.2.0", | ||
"version": "2.0.0", | ||
"description": "A Chorus Javascript Client", | ||
@@ -8,3 +8,3 @@ "main": "lib/index.js", | ||
"start": "webpack-dev-server --progress --colors --inline --hot --quiet", | ||
"prebuild": "npm run clean", | ||
"prebuild": "npm run clean:lib", | ||
"build": "NODE_ENV=production babel ./src --out-dir ./lib", | ||
@@ -14,2 +14,3 @@ "test": "npm run lint && npm run typecheck && npm run test:unit && npm run test:e2e", | ||
"test:e2e": "./e2e", | ||
"posttest:e2e": "npm run clean:test", | ||
"preversion": "npm run test", | ||
@@ -20,3 +21,7 @@ "version": "npm run build", | ||
"typecheck": "flow", | ||
"clean": "rimraf ./chorus-interpreter && rimraf ./lib && rimraf ./tests/*.actual.* && rimraf *.log", | ||
"clean": "rimraf ./chorus-interpreter && npm run clean:test && npm run clean:lib && npm run clean:logs && npm run clean:modules", | ||
"clean:test": "rimraf ./tests/*.actual.*", | ||
"clean:lib": "rimraf ./lib", | ||
"clean:logs": "rimraf *.log", | ||
"clean:modules": "rimraf ./node_modules", | ||
"hook": "cp ./git-hooks/* ./.git/hooks" | ||
@@ -28,18 +33,18 @@ }, | ||
"devDependencies": { | ||
"babel-cli": "^6.18.0", | ||
"babel-cli": "^6.24.0", | ||
"babel-eslint": "^7.1.1", | ||
"babel-jest": "^19.0.0", | ||
"babel-loader": "^6.3.2", | ||
"babel-plugin-transform-flow-strip-types": "^6.21.0", | ||
"babel-loader": "^6.4.0", | ||
"babel-plugin-transform-flow-strip-types": "^6.22.0", | ||
"babel-plugin-transform-object-rest-spread": "^6.23.0", | ||
"babel-preset-es2015": "^6.18.0", | ||
"eslint": "^3.16.1", | ||
"eslint-config-airbnb-base": "^11.0.0", | ||
"eslint-plugin-flowtype": "^2.29.2", | ||
"babel-preset-es2015": "^6.24.0", | ||
"eslint": "^3.17.1", | ||
"eslint-config-airbnb-base": "^11.1.1", | ||
"eslint-plugin-flowtype": "^2.30.3", | ||
"eslint-plugin-import": "^2.2.0", | ||
"expect": "^1.20.2", | ||
"flow-bin": "^0.40.0", | ||
"flow-bin": "^0.41.0", | ||
"jest": "^19.0.2", | ||
"webpack": "^1.14.0", | ||
"webpack-dev-server": "^1.16.2" | ||
"webpack-dev-server": "^1.16.3" | ||
}, | ||
@@ -46,0 +51,0 @@ "repository": { |
@@ -0,1 +1,2 @@ | ||
// @flow | ||
const openWebSocketModule = jest.genMockFromModule('../openWebSocket'); | ||
@@ -22,3 +23,3 @@ | ||
close() {}, | ||
__simulateServerMessage() { | ||
__simulateServerMessage({ withContext = false } = {}) { | ||
setTimeout(() => { | ||
@@ -37,3 +38,5 @@ _listeners | ||
timeoutPeriodSeconds: 60, | ||
contextVariables: {}, | ||
contextVariables: withContext ? { | ||
projectName: 'chorus', | ||
} : {}, | ||
}), | ||
@@ -40,0 +43,0 @@ })); |
@@ -1,2 +0,3 @@ | ||
import createClient from '../createClient'; | ||
// @flow | ||
import createClient, { ChorusClient } from '../createClient'; | ||
import { SUCCESS_URL, FAILING_URL } from '../__mocks__/openWebSocket'; | ||
@@ -32,2 +33,9 @@ | ||
const client = createClient('clientId', 'clientDescription'); | ||
expect(client).toEqual(expect.objectContaining({ | ||
open: expect.any(Function), | ||
close: expect.any(Function), | ||
connect: expect.any(Function), | ||
publishStep: expect.any(Function), | ||
stepsAligned: expect.any(Function), | ||
})); | ||
expect(typeof client.open).toBe('function'); | ||
@@ -73,3 +81,3 @@ expect(typeof client.close).toBe('function'); | ||
const closeSpy = jest.fn(); | ||
webSocket.close = closeSpy; | ||
(webSocket: any).close = closeSpy; | ||
client.close(); | ||
@@ -88,3 +96,3 @@ expect(closeSpy).toHaveBeenCalled(); | ||
const sendSpy = jest.fn(); | ||
webSocket.send = sendSpy; | ||
(webSocket: any).send = sendSpy; | ||
client.connect(); | ||
@@ -105,3 +113,3 @@ const message = { | ||
it('calls `send` on the open websocket', () => { | ||
const client = createClient('clientId', 'clientDescription'); | ||
const client: ChorusClient = createClient('clientId', 'clientDescription'); | ||
@@ -111,8 +119,8 @@ return client.open(SUCCESS_URL).then(() => { | ||
const sendSpy = jest.fn(); | ||
webSocket.send = sendSpy; | ||
(webSocket: any).send = sendSpy; | ||
client.publishStep('pattern', () => {}, { | ||
pendingMessage: 'pendingMessage', | ||
technicalDescription: 'technicalDescription', | ||
retryDuration: 0, | ||
retryInterval: 0, | ||
retryDuration: 2000, | ||
retryInterval: 100, | ||
}); | ||
@@ -126,4 +134,4 @@ const message = { | ||
technicalDescription: 'technicalDescription', | ||
retryDuration: 0, | ||
retryInterval: 0, | ||
retryDuration: 2000, | ||
retryInterval: 100, | ||
}; | ||
@@ -144,3 +152,3 @@ expect(sendSpy).toHaveBeenCalledWith( | ||
const sendSpy = jest.fn(); | ||
webSocket.send = sendSpy; | ||
(webSocket: any).send = sendSpy; | ||
client.stepsAligned(); | ||
@@ -165,7 +173,7 @@ const message = { | ||
const stepCallbackSpy = jest.fn(); | ||
client.publishStep('pattern', stepCallbackSpy, 'technicalDescription', 'pendingMessage'); | ||
webSocket.__simulateServerMessage(); | ||
client.publishStep('pattern', stepCallbackSpy); | ||
(webSocket: any).__simulateServerMessage(); | ||
// simulate the wait for an async server message | ||
fixAsyncErrorHandling(done, () => { | ||
expect(stepCallbackSpy).toHaveBeenCalledWith('one', 'two', 'three'); | ||
expect(stepCallbackSpy).toHaveBeenCalled(); | ||
}); | ||
@@ -175,2 +183,22 @@ }); | ||
it('passes `argments` and `context` to the step callback', done => { | ||
const client = createClient('clientId', 'clientDescription'); | ||
return client.open(SUCCESS_URL).then(() => { | ||
const webSocket = client.getSocket(); | ||
const stepCallbackSpy = jest.fn(); | ||
client.publishStep('pattern', stepCallbackSpy); | ||
(webSocket: any).__simulateServerMessage(); | ||
// simulate the wait for an async server message | ||
fixAsyncErrorHandling(done, () => { | ||
expect(stepCallbackSpy.mock.calls[0][0]).toEqual(['one', 'two', 'three']); | ||
expect(stepCallbackSpy.mock.calls[0][1]).toEqual(expect.objectContaining({ | ||
get: expect.any(Function), | ||
set: expect.any(Function), | ||
toObject: expect.any(Function), | ||
})); | ||
}); | ||
}); | ||
}); | ||
it('reports a success', done => { | ||
@@ -182,5 +210,5 @@ const client = createClient('clientId', 'clientDescription'); | ||
const sendSpy = jest.fn(); | ||
webSocket.send = sendSpy; | ||
client.publishStep('pattern', () => 3, 'technicalDescription', 'pendingMessage'); | ||
webSocket.__simulateServerMessage(); | ||
(webSocket: any).send = sendSpy; | ||
client.publishStep('pattern', () => 3); | ||
(webSocket: any).__simulateServerMessage(); | ||
// simulate the wait for an async server message | ||
@@ -203,2 +231,33 @@ fixAsyncErrorHandling(done, () => { | ||
it('sends the context back correctly', done => { | ||
const client = createClient('clientId', 'clientDescription'); | ||
return client.open(SUCCESS_URL).then(() => { | ||
const webSocket = client.getSocket(); | ||
const sendSpy = jest.fn(); | ||
(webSocket: any).send = sendSpy; | ||
client.publishStep('pattern', (args, context) => { | ||
context.set('projectName', 'chorus-js'); | ||
return 3; | ||
}); | ||
(webSocket: any).__simulateServerMessage({ withContext: true }); | ||
// simulate the wait for an async server message | ||
fixAsyncErrorHandling(done, () => { | ||
const message = { | ||
type: 'STEP_SUCCEEDED', | ||
chorusClientId: 'clientId', | ||
stepId: 'uuid', | ||
executionId: 'executionId', | ||
result: 3, | ||
contextVariables: { | ||
projectName: 'chorus-js', | ||
}, | ||
}; | ||
expect(sendSpy).toHaveBeenCalledWith( | ||
JSON.stringify(message), | ||
); | ||
}); | ||
}); | ||
}); | ||
it('reports a failure', done => { | ||
@@ -210,5 +269,5 @@ const client = createClient('clientId', 'clientDescription'); | ||
const sendSpy = jest.fn(); | ||
webSocket.send = sendSpy; | ||
client.publishStep('pattern', () => { throw new Error('not ok'); }, 'technicalDescription', 'pendingMessage'); | ||
webSocket.__simulateServerMessage(); | ||
(webSocket: any).send = sendSpy; | ||
client.publishStep('pattern', () => { throw new Error('not ok'); }); | ||
(webSocket: any).__simulateServerMessage(); | ||
fixAsyncErrorHandling(done, () => { | ||
@@ -215,0 +274,0 @@ const message = { |
@@ -0,1 +1,2 @@ | ||
// @flow | ||
import * as index from '../index'; | ||
@@ -2,0 +3,0 @@ |
// @flow | ||
import uuid from 'uuid'; | ||
import type { | ||
StepCallbackReturn, | ||
ConnectMessage, | ||
@@ -11,3 +12,4 @@ PublishStepMessage, | ||
ExecuteStepMessage, | ||
} from './messages.type'; | ||
} from './shared-types'; | ||
import createContext, { type ContextVariables, type ChorusContext } from './createContext'; | ||
import openWebSocket from './openWebSocket'; | ||
@@ -23,2 +25,4 @@ | ||
type StepCallback = (Array<string>, ChorusContext) => StepCallbackReturn; | ||
export interface ChorusClient { | ||
@@ -31,3 +35,3 @@ getSocket(): WebSocket, | ||
pattern: string, | ||
callback: (...args: Array<string>) => number | string | void, | ||
callback: StepCallback, | ||
options?: PublishStepMessageOptions, | ||
@@ -44,3 +48,3 @@ ): void, | ||
let _socket: WebSocket; | ||
const _callbacks: {[stepId: string]: (...args: Array<string>) => number | string | void} = {}; | ||
const _callbacks: { [stepId: string]: StepCallback } = {}; | ||
@@ -62,6 +66,9 @@ function _sendMessage(message: OutgoingMessage): void { | ||
const callback = _callbacks[incomingMessage.stepId]; | ||
const { stepId, executionId, contextVariables } = incomingMessage; | ||
const { stepId, executionId, arguments: args, contextVariables } = incomingMessage; | ||
const contextVars: ContextVariables = contextVariables; | ||
const context: ChorusContext = createContext(contextVars); | ||
try { | ||
const result: number | string | void = callback(...incomingMessage.arguments); | ||
const result: StepCallbackReturn = callback(args, context); | ||
const updatedContextVariables = context.toObject(); | ||
const message: StepSucceededMessage = { | ||
@@ -73,3 +80,3 @@ type: 'STEP_SUCCEEDED', | ||
result, | ||
contextVariables, | ||
contextVariables: updatedContextVariables, | ||
}; | ||
@@ -76,0 +83,0 @@ _sendMessage(message); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
33003
21
719