every thing i do in jest, the error is not going to fix and keeps showing below :
console.error node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/virtual-console.js:29
Error: Not implemented: window.alert
at module.exports (D:\Docs\Projects\EMM\emm_next\node_modules\jest-environment-jsdom\node_modules\jsdom\lib\jsdom\browser\not-implemented.js:9:17)
at D:\Docs\Projects\EMM\emm_next\node_modules\jest-environment-jsdom\node_modules\jsdom\lib\jsdom\browser\Window.js:594:7
at util_ajvSchemaValidator (D:\Docs\Projects\EMM\emm_next\src\utils\commonUtility.ts:1557:5)
at Object.<anonymous> (D:\Docs\Projects\EMM\emm_next\tests\unit\utils\commonUtility.test.ts:78:30)
at Object.asyncJestTest (D:\Docs\Projects\EMM\emm_next\node_modules\jest-jasmine2\build\jasmine_async.js:108:37)
at D:\Docs\Projects\EMM\emm_next\node_modules\jest-jasmine2\build\queue_runner.js:56:12
at new Promise (<anonymous>)
at mapper (D:\Docs\Projects\EMM\emm_next\node_modules\jest-jasmine2\build\queue_runner.js:43:19)
at D:\Docs\Projects\EMM\emm_next\node_modules\jest-jasmine2\build\queue_runner.js:87:41
at processTicksAndRejections (internal/process/task_queues.js:94:5) undefined
console.error node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/virtual-console.js:29
Error: Not implemented: window.print
at module.exports (D:\Docs\Projects\EMM\emm_next\node_modules\jest-environment-jsdom\node_modules\jsdom\lib\jsdom\browser\not-implemented.js:9:17)
at D:\Docs\Projects\EMM\emm_next\node_modules\jest-environment-jsdom\node_modules\jsdom\lib\jsdom\browser\Window.js:594:7
at Object.<anonymous> (D:\Docs\Projects\EMM\emm_next\tests\unit\utils\commonUtility.test.ts:127:5)
at Object.asyncJestTest (D:\Docs\Projects\EMM\emm_next\node_modules\jest-jasmine2\build\jasmine_async.js:108:37)
FAIL tests/unit/utils/commonUtility.test.ts (6.407s)jest-jasmine2\build\queue_runner.js:56:12
util_sum
i even used npm i jsdom, but it doesnt fix the problem. even add "testEnvironment": "jsdom" to package.json, but the problem isnt fixed. is there any proper way to fix this issue?
I had the same problem, and I fixed it this way:
In test for every component which using window.print I added this:
describe('Test.spec.js', () => {
let jsdomPrint;
beforeEach(() => {
jsdomPrint = window.print;
jest.spyOn(window, 'print').mockImplementation(() => {});
});
...
afterEach(() => {
window.print = jsdomPrint;
});
});
Since window.alert, print and several other browser-specific side effects needs to be stubbed manually. This should preferably be done with Jest, so a spy could be tracked and cleaned up:
jest.spyOn(window, 'alert').mockReturnValue();
jest.spyOn(window, 'print').mockReturnValue();
function printTheWebPage(){
window.print();
}
describe("print", () => {
it('should print', function () {
printTheWebPage();
expect(window.print).toHaveBeenCalled();
});
});
for more info see below links:
https://stackoverflow.com/a/60155951/12666332
https://jestjs.io/docs/en/mock-function-api#mockfnmockreturnvaluevalue
https://jestjs.io/docs/en/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom
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