Jest + Typescript + Absolute paths (baseUrl) gives error: Cannot find module

I was struggling with the same problem and actually it turns out that a simple change seems to do the trick. I just updated the moduleDirectories field in jest.config.js. Before moduleDirectories: [‘node_modules’] After moduleDirectories: [‘node_modules’, ‘src’] Hope it helps.

How to test a className with the Jest and React testing library

You can easily do that with react-testing-library. First, you have to understand that container or the result of getByText etc. are merely DOM nodes. You can interact with them in the same way you would do in a browser. So, if you want to know what class is applied to container.firstChild you can just do … Read more

How can I exclude files from Jest watch?

From the documentation you need to add modulePathIgnorePatterns which is an array of string values which will be matched against modulePathIgnorePatterns [array<string>] # (default: []) An array of regexp pattern strings that are matched against all module paths before those paths are to be considered ‘visible’ to the module loader. If a given module’s path … Read more

Mock only one function from module but leave rest with original functionality

The highlight of this answer is jest.requireActual(), this is a very useful utility that says to jest that “Hey keep every original functionalities intact and import them”. jest.mock(‘./utilities.js’, () => ({ …jest.requireActual(‘./utilities.js’), speak: jest.fn(), })); Let’s take another common scenario, you’re using enzyme ShallowWrapper and it doesn’t goes well with useContext() hook, so what’re you … Read more

Jest test fails : TypeError: window.matchMedia is not a function

The Jest documentation now has an “official” workaround: Object.defineProperty(window, ‘matchMedia’, { writable: true, value: jest.fn().mockImplementation(query => ({ matches: false, media: query, onchange: null, addListener: jest.fn(), // Deprecated removeListener: jest.fn(), // Deprecated addEventListener: jest.fn(), removeEventListener: jest.fn(), dispatchEvent: jest.fn(), })), }); Mocking methods which are not implemented in JSDOM

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)