Skip to content

Instantly share code, notes, and snippets.

@anchnk
Created December 24, 2018 14:10
Show Gist options
  • Save anchnk/6acfd4673183964b4050529d957990c4 to your computer and use it in GitHub Desktop.
Save anchnk/6acfd4673183964b4050529d957990c4 to your computer and use it in GitHub Desktop.
failing example of restoring a manual mock
'use strict';
let neatCsv = require('neat-csv');
let { parseResponseToJson } = require('./neat-csv.js');
jest.mock('neat-csv', () =>
jest.fn().mockRejectedValueOnce(new Error('Error while parsing'))
);
const csv = 'type;part\nunicorn;horn\nrainbow;pink';
const apiResponse = {
body: csv
};
const rejectionOf = (promise) =>
promise.then(
(value) => {
throw value;
},
(reason) => reason
);
test('mocked version', async () => {
const e = await rejectionOf(parseResponseToJson(apiResponse));
expect(e.message).toEqual('Error while parsing');
expect(neatCsv).toHaveBeenCalledTimes(1);
});
test('non mocked version', async () => {
jest.resetModules();
neatCsv = require('neat-csv');
({ parseResponseToJson } = require('./neat-csv.js'));
const result = await parseResponseToJson(apiResponse);
expect(JSON.stringify(result)).toEqual(
'[{"type":"unicorn","part":"horn"},{"type":"rainbow","part":"pink"}]'
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment