Does Jest support ES6 import/export?

From my answer to another question, this can be simpler: The only requirement is to configure your test environment to Babel, and add the ECMAScript 6 transform plugin: Step 1: Add your test environment to .babelrc in the root of your project: { “env”: { “test”: { “plugins”: [“@babel/plugin-transform-modules-commonjs”] } } } Step 2: Install the … Read more

How to properly make mock throw an error in Jest?

Change .mockReturnValue with .mockImplementation: yourMockInstance.mockImplementation(() => { throw new Error(); }); in case you want to assert test(‘the fetch fails with an error’, () => { return expect(fetchData()).rejects.toMatch(‘error’); }); If it’s a promise you can also to .rejects www.jestjs.io/docs/en/asynchronous#resolves–rejects

Jest SecurityError: localStorage is not available for opaque origins

In case, if you are accessing your application with a http://localhost prefix, you need to update your jest configuration (in your jest.config.js) as, “jest”: { “verbose”: true, “testURL”: “http://localhost/” } In case you do not already have any jest configuration, just include the configuration in your package.json. For example: { “name”: “…”, “description”: “…”, … … Read more

Jest: How to mock one specific method of a class

Using jest.spyOn() is the proper Jest way of mocking a single method and leaving the rest be. Actually there are two slightly different approaches to this. 1. Modify the method only in a single object import Person from “./Person”; test(‘Modify only instance’, () => { let person = new Person(‘Lorem’, ‘Ipsum’); let spy = jest.spyOn(person, … Read more

Cannot find name ‘describe’. Do you need to install type definitions for a test runner?

I’m using Visual Studio Code as my IDE and in my Angular project, and I had to comment-out/remove types in file tsconfig.json and add “jest” in types in tsconfig.spec.json. File tsconfig.json { “compilerOptions”: { // “types”: [] } } File tsconfig.spec.json { “compilerOptions”: { “types”: [“jest”, “node”] } }

How to mock imported named function in Jest when module is unmocked

Use jest.requireActual() inside jest.mock() jest.requireActual(moduleName) Returns the actual module instead of a mock, bypassing all checks on whether the module should receive a mock implementation or not. Example I prefer this concise usage where you require and spread within the returned object: // myModule.test.js import { otherFn } from ‘./myModule.js’ jest.mock(‘./myModule.js’, () => ({ …(jest.requireActual(‘./myModule.js’)), … Read more

Mock dependency in Jest with TypeScript

You can use type casting and your test.ts should look like this: import * as dep from ‘../dependency’; jest.mock(‘../dependency’); const mockedDependency = <jest.Mock<typeof dep.default>>dep.default; it(‘should do what I need’, () => { //this throws ts error // Property mockReturnValueOnce does not exist on type (name: string)…. mockedDependency.mockReturnValueOnce(‘return’); }); TS transpiler is not aware that jest.mock(‘../dependency’); … Read more

How can I mock the JavaScript ‘window’ object using Jest?

The following method worked for me. This approach allowed me to test some code that should work both in the browser and in Node.js, as it allowed me to set window to undefined. This was with Jest 24.8 (I believe): let windowSpy; beforeEach(() => { windowSpy = jest.spyOn(window, “window”, “get”); }); afterEach(() => { windowSpy.mockRestore(); … Read more

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