I've implemented a test case for my upload component which shows error if file.size
more than 1 mb.
Is there a way to monkeypatch file size with jest
or just js for getting test failing without creating the file like this?
const file = new File(
[new Blob(['1'.repeat(1024 * 1024 + 1)], { type: 'image/png' })],
'darthvader.png'
)
fake File: var blob = new Blob([""], { type: 'text/html' }); blob["lastModifiedDate"] = ""; blob["name"] = "filename"; var fakeF = blob; You can fill in the values as you see fit. You can fill the blob with whatever you need.
In Jest, Node. js modules are automatically mocked in your tests when you place the mock files in a __mocks__ folder that's next to the node_modules folder. For example, if you a file called __mock__/fs. js , then every time the fs module is called in your test, Jest will automatically use the mocks.
Mocking is a process used in unit testing when the unit being tested has external dependencies. The purpose of mocking is to isolate and focus on the code being tested and not on the behavior or state of external dependencies.
You can simply use Object.defineProperty
to override the original getter, it's configurable:
const file = new File([""], 'darthvader.png');
Object.defineProperty(file, 'size', { value: 1024 * 1024 + 1 })
console.log( file.size ); // 1048577
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With