Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jest: window.print not implemented

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?

like image 560
SeyyedKhandon Avatar asked Oct 28 '25 09:10

SeyyedKhandon


2 Answers

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;
    });
});
like image 101
liborkozak Avatar answered Oct 30 '25 00:10

liborkozak


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

like image 32
SeyyedKhandon Avatar answered Oct 29 '25 23:10

SeyyedKhandon