Created
December 24, 2018 14:10
-
-
Save anchnk/6acfd4673183964b4050529d957990c4 to your computer and use it in GitHub Desktop.
failing example of restoring a manual mock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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