mock-in-bundle
v1.3.0
Published
Cypress utility for mocking a module inside a Webpack bundle
Downloads
470
Readme
mock-in-bundle
Cypress utility for mocking a module inside a Webpack bundle
Watch the video Mock ES6 Module From Cypress E2E Test
Install
$ 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'
API
Mock a single module
it('mocks named export Age', () => {
mockInBundle('src/Person.js', { Age: 20 })
})
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')
})
Mock multiple modules
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> },
})
})
Tip: you can pass the bundle name/pattern as the second argument
mockInBundle({
modules to mock
}, 'main.chunk.js')
Examples
But if you are going to use, at least check out the examples:
Small print
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
MIT License
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.