Does Jest support ES6 import/export?

If I use import/export from ES6 then all my Jest tests fail with error: Unexpected reserved word I convert my object under test to use old school IIFE syntax and suddenly my tests pass. Or, take...

Jest setup "SyntaxError: Unexpected token export"

I'm implementing tests into an existing project that currently has no tests. My tests are failing to compile node_modules/ imports. /Users/me/myproject/node_modules/lodash-es/lodash.js:10 export {...

Jest spyOn function called

I'm trying to write a simple test for a simple React component, and I want to use Jest to confirm that a function has been called when I simulate a click with enzyme. According to the Jest docs, I...

How do I test axios in Jest?

I have this action in React: export function fetchPosts() { const request = axios.get(`${WORDPRESS_URL}`); return { type: FETCH_POSTS, payload: request } } How do I...

Using Jest to mock named imports

I have a 'notifications.js' module that looks a bit like this: import { Notifications, Permissions } from 'expo' export function setLocalNotification(storage = AsyncStorage) { return storage ...

Jest mocking default exports - require vs import

I have seen questions referring to the mocking of default exports with jest around here, but I don't think this has already been asked: When mocking the default export of a dependency of a module...

Jest Enzyme test a React component that returns null in render method

I have a component that returns null in render under certain conditions: render() { if (this.props.isHidden) { return null; } return <div>test</div>; } I want to check if the...

Testing MutationObserver with Jest

I wrote a script with the main purpose of adding new elements to some table's cells. The test is done with something like that: document.body.innerHTML = ` <body> <div...

Sonarqube coverage configuration for React and Jest

I have problem with configuration sonarqube to work properly with React + Jest. My...

How to use Jest to test functions using crypto or window.msCrypto

When running unit tests with Jest in react the window.crypto API is causing problems. I haven't found a way to incorporate crypto in Jest without installing other packages which is something I...

How to reset state beforeEach/afterEach jest test?

My components all needed to be wrapped by a state provider in order for them to work, so when I render each component for testing I wrap them in the state provider. Like so: <StateProvider> ...

Cannot find name 'it' in Jest TypeScript

I try to create an intial setup for Jest in React + TypeScript. I have completed the initial setup and try to check whether the test runs. When I run the test using the command npm test, I am...

VSCode: How to debug current Jest test file

How to launch jest in debug mode on the current opened file, *and only this one*, regardless of the OS (windows, linux, mac). *The problem*: When using vscode under windows, it is impossible to...

react-testing-library: some portion of debug's output is not visible

I am using react jest with react testing library to test my component. I am facing a weird issue. I am usng debug return by render from testing-library. test('component should work', async () =>...

Jest Testing React Native cannot use import statement outside of a module

I have been trying to find out how to fix the following error when trying to run my Jest test using React Native: FAIL tests/App-test.js ● Test suite failed to...

How to mock navigator.clipboard.writeText() in Jest?

I have tried the following 4 options after looking at Jest issues and SO answers, but I am either getting TypeScript errors or runtime errors. I would really like to get option 1 (spyOn)...

Svelte Component Testing with Jest, Unable to load Svelte files recursively

I am working on a Svelte Project with Typescript and want to use Jest to test UI components using the @testing-library/svelte module . I am not able to properly import all my svelte files like my...

How to mock ResizeObserver to work in unit tests using react testing library

If anyone can help, I have a custom hook that uses ResizeObserver to change the width of a component. My problem is that when I go to run my units test it breaks all my tests and looking at the...

Jest coverage in a node typescript project always returns empty

I am working on backend Typescript project when i am trying to get coverage report for unit test case,Jest returns empty coverage report in terminal as well as in html report stating nothing. i...

React build - not found: Error: Can't resolve 'buffer'

I am having an error when I build my application in react. I noticed this error only when I tried to build application. When I stopped dev server and ran it again, it showed the same error. It...

How to test a delayed event handler in jest?

I've got a function that should be get executed in near future and I want to test it with jest but have no idea how to do this properly. Actually, I'm using React + testing react library but that...

NodeJs using pg client - Jest has detected the following open handle potentially keeping Jest from exiting - TCPWRAP

In some integration tests in my Node.js, I use pg to perform some cleanup of the Postgres tests database after the test have run. I call this in the afterAll(): afterAll(() => { const { Pool }...

React state not being updated in Jest while working in the application

When I click the button it increments the value by the amount written in the input, it works when I do that in the app but when I try to use Jest and first fireEvent.change() the value of an input...

How to Test Next.js's getServerSideProps with jest

I would like to run tests with Jest and Enzyme on Next.js's getServerSideProps function. The function looks like the following: export const getServerSideProps: GetServerSideProps = async...

How in Jest testing to match a string which contains a "/" and gives a syntax error

In my React application, I'm writing a Jest testing to test if on screen I get a specific date, time, and timezone. The test looks as it is test('Local date time format - Europe no UK', () => { ...

Jest not exiting properly when using RTK-Query useLazyQuery with ReactNative

I'm trying to test some functionality I've written using RTK-Query. Ive created an api using createApi and have exported the useLazyQuery hook. I am calling this hook and then listening for...

Package subpath './src/ngtsc/reflection' is not defined by "exports" in /node_modules/@angular/compiler-cli/package.json

I am trying to run the ng test with jest and getting this following error Package subpath './src/ngtsc/reflection' is not defined by "exports" in...

Angular 13 + jest + Angular library not working

I'm trying to use jest in an Angular 13 project for testing. I use jest-preset-angular with @angular-builders/jest. I use ngx-pipes, which is a library that is not compiled for Angular 13. It...

Invariant Violation: `new NativeEventEmitter()` requires a non-null argument

I tried to run default test in react-native using jest and I found this problem: Test suite failed to run Invariant Violation: `new NativeEventEmitter()` requires a non-null argument. at...

Angular v13 Jest with nx test - SyntaxError: Cannot use import statement outside a module at Runtime.createScriptFromCode

I tried to follow every comment with a possible solution here to the letter. I relied on an example project on github as well which works perfectly. This also started to happen to me after...