_userEvent.default.setup is not a function
@testing-library/user-eventversion: ^14.0.0-beta- DOM Environment: 8.11.1
- Node version: 14.17.4
Relevant code or config
const user = userEvent.setup()
What you did: Tests worked perfectly before updating, I just wanted the latest version for the pointer method.
Followed the latest documentation and tried multiple versions 14 and above.
Imported like so:
import userEvent from '@testing-library/user-event';
and initialised like so:
const user = userEvent.setup()
What happened:
Tests failed and receive following error:
TypeError: _userEvent.default.setup is not a function
_userEvent.default.setup suggests that the import statement is transpiled before being executed.
Could you please provide a reproduction?
I am also facing the same issue. Here is what I did:
import userEvent from "@testing-library/user-event";
test("Should be able to type an email", () => {
const user = userEvent.setup();
render(<App/>)
const emailInputElement = screen.getByRole("textbox", { name: /email/i})
user.type(emailInputElement, "[email protected]");
expect(emailInputElement.value).toBe("[email protected]");
})
Output: TypeError: _userEvent.default.setup is not a function
This is my jest and RTL versions: "@testing-library/jest-dom": "^5.16.2", "@testing-library/react": "^12.1.2", "@testing-library/user-event": "^13.5.0",
@bijay-ps userEvent.setup() was introduced in v14.
What build tools are you using (it would help to see your configs as well)?
The same thing happened to me. I'm using typescript with jest Node 16 lts.
"devDependencies": {
"@testing-library/jest-dom": "^5.16.2",
"@testing-library/react": "^12.1.3",
"@testing-library/user-event": "^14.0.0-beta",
"jest": "^27.5.1",
"ts-jest": "^27.1.3",
"ts-node": "^10.5.0"
}
{
"compilerOptions": {
"allowUmdGlobalAccess": true,
"noImplicitAny": true,
"strictNullChecks": true,
"noUnusedParameters": false,
"noUnusedLocals": true,
"skipLibCheck": true,
"moduleResolution": "node",
"removeComments": true,
"target": "ESNEXT",
"module": "ESNext",
"noEmit": true,
"esModuleInterop": true,
"jsx": "react"
},
"include": ["src"]
}
export default {
clearMocks: true,
collectCoverage: true,
coverageDirectory: "coverage",
coverageProvider: "v8",
preset: 'ts-jest/presets/default-esm',
testEnvironment: 'jest-environment-jsdom',
transform: {},
transformIgnorePatterns: [
"node_modules/(?!(ts-invariant)/)",
],
extensionsToTreatAsEsm: [
".jsx",
".ts",
".tsx"
],
globals: {
'ts-jest': {
useESM: true
},
},
setupFilesAfterEnv: ['./jest.setup.ts']
};
import userEvent from '@testing-library/user-event'
const user = userEvent.setup()
error: TypeError: userEvent.setup is not a function
If I run console.log(userEvent) I get { PointerEventsCheckLevel: [Getter], default: [Getter] }
I can access the methods if I access them through default but typescript thinks userEvent is already the default. Very weird.
Strangely enough, this issue is not present on my private CI runner I'm running on EC2. So it seems to be related to my local environment. I'm using .nvmrc so my node install should be the same. I'm not using different build/test tooling in the CI so I'm not sure what is different.
The import works just fine in node locally and on Stackblitz: https://stackblitz.com/edit/node-ndptdu?file=index.mjs
@mattxyzeth Could you check if this works in both of your environments outside of the test runner? We need to make sure this is not a misconfiguration of the transpiler.
Also please have a look at this: https://github.com/testing-library/user-event/issues/757#issuecomment-1044314542
I'll try to test this out soon. Just a quick note, the stackblitz you posted isn't using typescript. My problem is definitely a configuration issue but I can't seem to figure out what is configured wrong. No matter what I try my import is always coming from the .cjs file. Even though I've explicitly declared my project as a module and have all the configuration in jest and TS to compile as a module. I'm not convinced this is a testing-library issue but you guys should definitely know about it.
Could you create a reproduction please? FWIW, the latest version (previous version too) seems to be working in combination with TypeScript in the Angular Testing Library - https://github.com/testing-library/angular-testing-library/pull/283
I’ll be busy for a while working on a big release coming up in the next couple of months. I will be happy to help in June. Sorry for not being available sooner.
On Mar 17, 2022, at 9:01 AM, Tim Deschryver @.***> wrote:
Could you create a reproduction please? FWIW, the latest version (previous version too) seems to be working in combination with TypeScript in the Angular Testing Library - testing-library/angular-testing-library#283 https://github.com/testing-library/angular-testing-library/pull/283 — Reply to this email directly, view it on GitHub https://github.com/testing-library/user-event/issues/839#issuecomment-1070898692, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJDA3EAA6EV3N3K3HTASETVAMUJPANCNFSM5MMYUHUA. You are receiving this because you were mentioned.
import userEvent from '@testing-library/user-event'; imports the default export of user-event as userEvent. However, that package has no default export. Only named exports. What you probably want is
-import userEvent from '@testing-library/user-event';
+import * userEvent from '@testing-library/user-event';
This is the ESM export by esbuild: https://unpkg.com/browse/@testing-library/[email protected]/dist/index.mjs
2966 | var userEvent = __spreadProps(__spreadValues({}, directApi_exports), {
2967 | setup: setupMain
2968 | });
2969 | export {
2970 | PointerEventsCheckLevel,
2971 | userEvent as default
2972 | };
Oh we're talking about the beta. The latest stable didn't have a ESM entrypoint specified in the package.json so I was confused.
Can't say why this is happening in the beta without a repro.
Could this be related to https://github.com/facebook/jest/issues/12120? I am running typescript and ts-jest and the import is index.cjs?
Same issue, but with any of the userEvent methods
same issue getting _userEvent.default.click is not a function with jest
jsdom fourteen w/ jest react-scripts
"react": "^16.12.0",
"react-scripts": "^3.4.1",
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^12.1.3",
"@testing-library/react-hooks": "^7.0.2",
"@testing-library/user-event": "^14.0.4",
Any update on this?
This happened to me today with @testing-library/user-event 14.1.0 with Typescript 4.6.3, Jest 27.5.1 and TS Jest 27.1.4 (latest version of each).
What I found was that this happened in my project because the files in the dist folder of the package are produced with .cjs and .mjs extensions. When I renamed them to use .js, moved the ESM entry point into a separate esm directory, and then updated the package.json paths, everything worked again for me.
Here's the patch file I created locally:
diff --git a/node_modules/@testing-library/user-event/dist/index.mjs b/node_modules/@testing-library/user-event/dist/esm/index.js
similarity index 100%
rename from node_modules/@testing-library/user-event/dist/index.mjs
rename to node_modules/@testing-library/user-event/dist/esm/index.js
diff --git a/node_modules/@testing-library/user-event/dist/index.cjs b/node_modules/@testing-library/user-event/dist/index.js
similarity index 100%
rename from node_modules/@testing-library/user-event/dist/index.cjs
rename to node_modules/@testing-library/user-event/dist/index.js
diff --git a/node_modules/@testing-library/user-event/package.json b/node_modules/@testing-library/user-event/package.json
index 9d8bdc0..35134cb 100644
--- a/node_modules/@testing-library/user-event/package.json
+++ b/node_modules/@testing-library/user-event/package.json
@@ -25,12 +25,12 @@
"files": [
"dist"
],
- "main": "./dist/index.cjs",
- "module": "./dist/index.mjs",
+ "main": "./dist/index.js",
+ "module": "./dist/esm/index.js",
"exports": {
".": {
- "require": "./dist/index.cjs",
- "default": "./dist/index.mjs"
+ "require": "./dist/index.js",
+ "default": "./dist/esm/index.js"
}
},
"types": "./dist/types/index.d.ts",
I came across the same issue too.
The temporary solution for me is I added the support type cjs to the transform configs of Jest in the package.json
jest: {
...,
"transform": {
- "^.+\\.(js|jsx)$": "babel-jest",
+ "^.+\\.(js|jsx|cjs)$": "babel-jest",
}
}
For me works after add:
transformIgnorePatterns: [
'/node_modules/(?!(react-use)/)',
],