Skip to content

Commit

Permalink
fix: test for tx submit fail
Browse files Browse the repository at this point in the history
  • Loading branch information
slowbackspace committed Dec 1, 2021
1 parent f240765 commit 4e1fa4f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
76 changes: 75 additions & 1 deletion src/commands/transaction/__tests__/submit.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { BlockFrostAPI } from '@blockfrost/blockfrost-js';
import * as path from 'path';
import * as nock from 'nock';
import { stdout } from 'stdout-stderr';
import * as blockfrostService from '../../../services/blockfrost';
import { Submit } from '../submit';

describe('transaction submit', () => {
// TODO: mock txSubmit error
it('should read tx from file and submit it via client.txSubmit', async () => {
const files = ['txFile1', 'txFile2'];
for (let i = 0; i < files.length; i++) {
Expand Down Expand Up @@ -40,4 +41,77 @@ describe('transaction submit', () => {
jest.resetAllMocks();
}
});

it('should read tx from file and throw after pushing the tx', async () => {
jest.restoreAllMocks(); // restores createBlockfrostClient if mocked from other tests

jest
.spyOn(blockfrostService, 'createBlockfrostClient')
.mockImplementation((testnet?: boolean) => {
// omit check for missing env variable for project id
return new BlockFrostAPI({
projectId: 'testnet123',
isTestnet: Boolean(testnet),
});
});

nock('https://cardano-testnet.blockfrost.io:443', { encodedQueryParams: true })
.post('/api/v0/tx/submit')
.reply(
400,
{
error: 'Bad Request',
message:
'"transaction read error RawCborDecodeError [DecoderErrorDeserialiseFailure \\"Byron Tx\\" (DeserialiseFailure 0 \\"end of input\\"),DecoderErrorDeserialiseFailure \\"Shelley Tx\\" (DeserialiseFailure 0 \\"end of input\\"),DecoderErrorDeserialiseFailure \\"Shelley Tx\\" (DeserialiseFailure 0 \\"end of input\\"),DecoderErrorDeserialiseFailure \\"Shelley Tx\\" (DeserialiseFailure 0 \\"end of input\\"),DecoderErrorDeserialiseFailure \\"Shelley Tx\\" (DeserialiseFailure 0 \\"end of input\\")]"',
status_code: 400,
},
[
'Date',
'Wed, 01 Dec 2021 10:14:37 GMT',
'Content-Type',
'application/octet-stream',
'Transfer-Encoding',
'chunked',
'Connection',
'close',
'vary',
'Origin',
'access-control-allow-origin',
'*',
'cf-cache-status',
'DYNAMIC',
'expect-ct',
'max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"',
'report-to',
'{"endpoints":[{"url":"https:\\/\\/a.nel.cloudflare.com\\/report\\/v3?s=KqrAxNfliO9QMiBxu62YRXHh%2BoiZaqhhQgjC3dPWNE%2F9rKzEwAW%2FkIVIiDxU9YcxMVvq2LkSF88TS%2BBZSqF6lZPoNyw3WE9Q1ga4hOv0mjMThp1HA%2FMHPbyKf2D5gDIBH6HnSYFYqezUjGqFnBLxc7P6j6zQXg%3D%3D"}],"group":"cf-nel","max_age":604800}',
'nel',
'{"success_fraction":0,"report_to":"cf-nel","max_age":604800}',
'Server',
'cloudflare',
'CF-RAY',
'6b6b8cb13ba8278c-PRG',
],
);

// Mocking fs breaks test. for now using real files from __fixtures__ folder
// TypeError: The "code" argument must be of type string. Received undefined
// const readFileSyncMock = jest.spyOn(fs, 'readFileSync');
const filename = path.join(__dirname, `../__fixtures__/txFile1`);

stdout.start();
// nock.recorder.rec();
expect(
async () => await Submit.run(['--testnet', '--tx-file', filename]),
).rejects.toHaveProperty(
'message',
'Command failed: transaction read error RawCborDecodeError [DecoderErrorDeserialiseFailure \\"Byron Tx\\" (DeserialiseFailure 0 \\"end of input\\"),DecoderErrorDeserialiseFailure \\"Shelley Tx\\" (DeserialiseFailure 0 \\"end of input\\"),DecoderErrorDeserialiseFailure \\"Shelley Tx\\" (DeserialiseFailure 0 \\"end of input\\"),DecoderErrorDeserialiseFailure \\"Shelley Tx\\" (DeserialiseFailure 0 \\"end of input\\"),DecoderErrorDeserialiseFailure \\"Shelley Tx\\" (DeserialiseFailure 0 \\"end of input\\")]',
);
// nock.restore();
stdout.stop();

const output = stdout.output;
expect(output.includes('Transaction successfully submitted')).toBe(false);
// workaround ReferenceError: You are trying to `import` a file after the Jest environment has been torn down
await new Promise(resolve => setTimeout(() => resolve(true), 500));
});
});
2 changes: 1 addition & 1 deletion src/commands/transaction/submit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class Submit extends BaseCommand {
return response;
} catch (err) {
if (err instanceof BlockfrostServerError || err instanceof BlockfrostClientError) {
this.log(`Command failed: ${stripQuotes(err.message)}`); // matching cardano-cli
this.error(`Command failed: ${stripQuotes(err.message)}`); // matching cardano-cli (if we want really 1:1 output compatibility we should use this.log which doesn't add visuals in front of error message) and exit the process manually)
} else {
throw err;
}
Expand Down

0 comments on commit 4e1fa4f

Please sign in to comment.