Closed soullivaneuh closed 3 years ago
Looks to be related to: https://github.com/testing-library/user-event/issues/426#issuecomment-671832307
Hacking your code with this:
exports.prefersDark = window.matchMedia ? window.matchMedia('(prefers-color-scheme: dark)') : false;
"Solves" the issue.
Is that acceptable for a PR?
After some research, it's look like the error comes from jsdom that does not support all the window
functionalities.
I first try to use beforeParse
JSDom function to mock the function
using testEnvironmentOptions
jest option like described here but without any success.
Finally, I followed the Jest recommendation about
mocking methods which are not implemented in JSDOM with a setupFilesAfterEnv
configuration:
// jest.config.ts
import type { Config } from '@jest/types';
const config: Config.InitialOptions = {
setupFilesAfterEnv: [
'@testing-library/jest-dom/extend-expect',
'<rootDir>/jest.mocks.ts',
],
// Rest of the config
};
export default config;
// jest.mocks.ts
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation(query => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(),
removeListener: jest.fn(),
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
})),
});
export default null;
I'm quite surprised there is no other way like a mock library for that case, but it works and I don't think there is anything else to do on your project.
Except maybe, adding a hint to your documentation.
If you want to make a or adding a note I'll merge it!
Storyshots testing are not working anymore since I installed your addons.
Here is the storyshot test configuration:
Failing with the following stacktrace:
Related file: