Open lukaw3d opened 2 years ago
We can provide a fake implementation of XMLHttpRequest via:
1) 3rd party lib, sample: https://github.com/oasisprotocol/oasis-wallet-web/commit/72894bc1df6d8fd19ddff7e2eef5e8c07db90089
2) Dummy mock https://github.com/oasisprotocol/oasis-wallet-web/commit/0690b0cb8711f83c7d88b161bd7a55fd0b3d3ae1
Both can be applied per test or globally via setupTests.ts
3) Write isolated/unit tests not integration tests, but that would require writing additional tests to cover actions and reducers. https://github.com/oasisprotocol/oasis-wallet-web/commit/8676dba280059b965dd7c94da399153a67632263
grpc-web also allows you to pass in a replacement for their "xhrIo" object when creating a grpc client. we can expose this in the typescript oasis sdk if you want
google has a mock for xhrIo: https://google.github.io/closure-library/api/goog.testing.net.XhrIo.html https://github.com/google/closure-library/blob/master/closure/goog/testing/net/xhrio.js#L37
Mocking at request level for gRPC isn't pretty, e.g. AAAAABKhZGRhdGFLoWhiYWxhbmNlc6A=gAAAAB5ncnBjLXN0YXR1czowDQpncnBjLW1lc3NhZ2U6DQo=
But maybe we only need a few encoded responses like that :shrug:
We currently sometimes:
getOasisNic
jest.mock('@oasisprotocol/client/dist/client')
and jest.mocked(NodeInternal).mockImplementation
I'd rather have a NodeInternal
that prints unmocked requests and is easy to mock specific methods. I got this to work:
/* setupTests.ts */
beforeEach(() => {
// @ts-expect-error Protected property
jest.spyOn(NodeInternal.prototype, 'callUnary')
jest.mocked(NodeInternal.prototype['callUnary']).mockImplementation(async (desc, request) => {
console.warn(
'Unmocked gRPC',
desc.getName(),
request,
"Mock it with e.g. jest.spyOn(NodeInternal.prototype, 'stakingAccount').mockResolvedValue()",
)
})
})
mocking the NodeInternal object or the callUnary should be fine. we have end to end tests in oasis-sdk to keep an eye on the grpc client layer
See https://github.com/oasisprotocol/oasis-wallet-web/runs/6108972874?check_suite_focus=true#step:6:22
By logging requests in node_modules/@oasisprotocol/client/dist/client.js, I get the following unmocked GRPC calls