Test an application with a test gRPC server
Use this example when an integration test needs to call the SDK without connecting to the broker. The application starts a local gRPC server, supplies its address to the SDK, and checks the response. The test depends only on public service definitions.
Add the server dependency
The SDK exports service contracts and the application creates the test server. Declare nice-grpc as a direct development dependency of the project under test, even if it is already a transitive dependency.
npm install --save-dev nice-grpc@^2.1.17
Run a test with SignalService
Register a local SignalService implementation with a predefined response and start the server on an available port with server.listen('127.0.0.1:0'). The example passes the actual address to the SDK, calls sdk.signals.getStrategies(), and checks that the SDK received exactly one strategy with the ID demo-strategy. The finally block closes the SDK and shuts down the server.
import { strict as assert } from 'node:assert';
import { createServer } from 'nice-grpc';
import {
SignalServiceDefinition,
StrategyType,
TInvestNodeSDK,
type SignalServiceImplementation
} from '@woodger/t-invest-node-sdk';
const signalService: SignalServiceImplementation = {
async getStrategies() {
return {
strategies: [
{
strategyId: 'demo-strategy',
strategyName: 'Demo strategy',
strategyType: StrategyType.STRATEGY_TYPE_TECHNICAL
}
]
};
},
async getSignals() {
return { signals: [] };
}
};
const server = createServer();
server.add(SignalServiceDefinition, signalService);
const port = await server.listen('127.0.0.1:0');
const sdk = new TInvestNodeSDK({
token: 'test-token',
endpoint: `127.0.0.1:${port}`,
useSsl: false
});
try {
const response = await sdk.signals.getStrategies({
strategyId: 'demo-strategy'
});
assert.equal(response.strategies.length, 1);
assert.equal(response.strategies[0]?.strategyId, 'demo-strategy');
}
finally {
sdk.close();
await server.shutdown();
}
Import public contracts
Import from the root of the package.
SignalServiceDefinitionsupplies the service definition forserver.add().SignalServiceImplementationdescribes the mock implementation type.- Import request and response types, enums, and their JSON converters from the same package root.
The SDK facade constructs the generated *ServiceClient internally. The package's exports field blocks unstable imports of internal files from dist/generated/**.
For other services, use the corresponding *ServiceDefinition and *ServiceImplementation. See the package's public exports for the full list.