Better way to disable console inside unit tests

For particular spec file, Andreas’s is good enough. Below setup will suppress console.log statements for all test suites, jest –silent (or) To customize warn, info and debug you can use below setup tests/setup.js or jest-preload.js configured in setupFilesAfterEnv global.console = { …console, // uncomment to ignore a specific log level log: jest.fn(), debug: jest.fn(), info: … Read more

How to change mock implementation on a per single test basis?

Use mockFn.mockImplementation(fn). import { funcToMock } from ‘./somewhere’; jest.mock(‘./somewhere’); beforeEach(() => { funcToMock.mockImplementation(() => { /* default implementation */ }); // (funcToMock as jest.Mock)… in TS }); test(‘case that needs a different implementation of funcToMock’, () => { funcToMock.mockImplementation(() => { /* implementation specific to this test */ }); // (funcToMock as jest.Mock)… in TS … Read more

How to resolve “Cannot use import statement outside a module” in jest

Also using Babel, Typescript and Jest. Had the same failure, driving me crazy for hours. Ended up creating a new babel.config.js file specifically for the tests. Had a large .babelrc that wasn’t getting picked up by jest no matter what i did to it. Main app still uses the .babelrc as this overrides babel.config.js files. … Read more

How do I deal with localStorage in jest tests?

Great solution from @chiedo However, we use ES2015 syntax and I felt it was a little cleaner to write it this way. class LocalStorageMock { constructor() { this.store = {}; } clear() { this.store = {}; } getItem(key) { return this.store[key] || null; } setItem(key, value) { this.store[key] = String(value); } removeItem(key) { delete this.store[key]; … Read more

How can I clear the Jest cache?

As of Jest 22.0.0+, you can use the –clearCache option: Deletes the Jest cache directory and then exits without running tests. Will delete cacheDirectory if the option is passed, or Jest’s default cache directory.

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