Cypress utility for mocking a module inside a Webpack bundle
Watch the video Mock ES6 Module From Cypress E2E Test
$ npm i -D mock-in-bundle
# or using Yarn
$ yarn add -D mock-in-bundle
The import the function in your spec
import { mockInBundle } from 'mock-in-bundle'
This module supports mocking modules served by:
- Webpack v4 in dev mode (unminified)
it('mocks named export Age', () => {
mockInBundle('src/Person.js', { Age: 20 })
cy.visit('/')
})
You can optionally limit the JS bundles to inspect. For example, react-scripts put the application code into the main.chunk.js
. Pass it as th third argument.
it('mocks named export Age', () => {
mockInBundle('src/Person.js', { Age: 20 }, 'main.chunk.js')
cy.visit('/')
})
import { mockInBundle } from 'mock-in-bundle'
it('mocks the Config module', () => {
mockInBundle('src/Config.tsx', { default: { title: 'Mock Test' } })
cy.visit('/')
cy.contains('h1', 'Mock Test')
})
You can mock multiple modules at once by using an object with [module name]: mocks
format.
// import React so we can mock JSX components
import React from 'react'
it('mocks Person and Timer', () => {
mockInBundle({
'src/Person.js': { Age: 20 },
'src/Timer.js': { default: () => <div>Mock Time</div> },
})
cy.visit('/')
})
Tip: you can pass the bundle name/pattern as the second argument
mockInBundle({
modules to mock
}, 'main.chunk.js')
But if you are going to use, at least check out the examples:
- bahmutov/sudoku-mock-module
- bahmutov/haptics-and-micro-animations shows how to mock external library in a React Native project served using Expo
- bahmutov/stub-window-object-example
Author: Gleb Bahmutov © 2021
License: MIT - do anything with the code, but don't blame me if it does not work.
Spread the word: tweet, star on github, etc.
Support: if you find any problems with this module, email / tweet / open issue on Github
Copyright (c) 2021 Gleb Bahmutov
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.