Created
January 16, 2019 12:31
-
-
Save josemigallas/31f76e499e97e7362c683e1df2e26669 to your computer and use it in GitHub Desktop.
Gist exemplifying usage of Jest module mocks, with Flow.
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
// modules/__mocks__/i18n.js | |
const i18n = { | |
t: name => `Bye ${name}!` | |
} | |
export default i18n |
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
// ./modules/i18n.js | |
const i18n = { | |
t: name => `Hello ${name}!` | |
} | |
export default i18n |
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
// ./utils.js | |
import i18n from './modules/i18n' | |
export const functionThatUsesT = name => `${i18n.t(name)}` |
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
// ./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