Skip to content

Instantly share code, notes, and snippets.

@josemigallas
Created January 16, 2019 12:31
Show Gist options
  • Save josemigallas/31f76e499e97e7362c683e1df2e26669 to your computer and use it in GitHub Desktop.
Save josemigallas/31f76e499e97e7362c683e1df2e26669 to your computer and use it in GitHub Desktop.
Gist exemplifying usage of Jest module mocks, with Flow.
// modules/__mocks__/i18n.js
const i18n = {
t: name => `Bye ${name}!`
}
export default i18n
// ./modules/i18n.js
const i18n = {
t: name => `Hello ${name}!`
}
export default i18n
// ./utils.js
import i18n from './modules/i18n'
export const functionThatUsesT = name => `${i18n.t(name)}`
// ./utils.spec.js
import { functionThatUsesT } from './utils'
jest.mock('./modules/i18n')
describe('functionThatUsesT', () => {
it('should mock i18n and say bye instead of hello', () => {
expect(functionThatUsesT('Spiderman')).toEqual('Bye Spiderman!')
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment