user-event icon indicating copy to clipboard operation
user-event copied to clipboard

_userEvent.default.setup is not a function

Open Tommoore96 opened this issue 4 years ago • 39 comments

  • @testing-library/user-event version: ^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

Tommoore96 avatar Jan 20 '22 13:01 Tommoore96

_userEvent.default.setup suggests that the import statement is transpiled before being executed.

Could you please provide a reproduction?

ph-fritsche avatar Jan 23 '22 19:01 ph-fritsche

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 avatar Feb 09 '22 17:02 bijay-ps

@bijay-ps userEvent.setup() was introduced in v14.

ph-fritsche avatar Feb 10 '22 11:02 ph-fritsche

What build tools are you using (it would help to see your configs as well)?

nickserv avatar Feb 16 '22 05:02 nickserv

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.

mattxyzeth avatar Feb 27 '22 19:02 mattxyzeth

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.

mattxyzeth avatar Feb 28 '22 16:02 mattxyzeth

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

ph-fritsche avatar Mar 01 '22 12:03 ph-fritsche

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.

mattxyzeth avatar Mar 01 '22 14:03 mattxyzeth

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

timdeschryver avatar Mar 17 '22 13:03 timdeschryver

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.

mattxyzeth avatar Mar 17 '22 15:03 mattxyzeth

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';

eps1lon avatar Mar 18 '22 08:03 eps1lon

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 | };

ph-fritsche avatar Mar 18 '22 09:03 ph-fritsche

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.

eps1lon avatar Mar 18 '22 12:03 eps1lon

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?

observstream avatar Mar 25 '22 19:03 observstream

Same issue, but with any of the userEvent methods

capndave avatar Apr 05 '22 21:04 capndave

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",

sherodtaylor avatar Apr 06 '22 16:04 sherodtaylor

Any update on this?

sherodtaylor avatar Apr 11 '22 14:04 sherodtaylor

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",

strmer15 avatar Apr 12 '22 22:04 strmer15

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",
  }
}

JimLin94 avatar Apr 22 '22 10:04 JimLin94

For me works after add:

transformIgnorePatterns: [
        '/node_modules/(?!(react-use)/)',
],

DominicTylor avatar May 06 '22 10:05 DominicTylor