-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeScript + Fully-Specified ESM import fails in cy.task #20580
Comments
Workaround: precompile the TypeScript files to CJS (run |
ts-node 10.8.0 has been released with .js extension resolution. Would that resolve this for Cypress? I'm interested in a workaround that doesn't target CommonJS; my project is ESM and I'd like to have Cypress tasks import my app code. |
Yeah, it would be great to have ESM support in Cypress. I don't think new versions of |
Ah, looks like Cypress v10 has updated this cypress/packages/server/package.json Line 120 in a0a64cc
Maybe fully-specified ESM imports will work now... 🤔 |
Everything is working now with the recent "type": "module" fix by @lmiller1990 (released in To take advantage of this, make sure that you have:
import { defineConfig } from 'cypress';
-import * as users from './database/queries/users';
+import * as users from './database/queries/users.js';
export default defineConfig({
e2e: {
setupNodeEvents(on) {
on('task', {
async deleteUserByEmail(email: string) {
return (await users.deleteUserByEmail(email)) || null;
},
});
},
},
}); |
Alternatively, change your As an aside, you have no idea how good it feels to get a notification in an already closed issue that actually clarifies things are working and provides useful info for future users! Thank you for this! |
This is not true with TypeScript and fully-specified imports in ESM, check out microsoft/TypeScript#41887 (comment) The files associated with the
Great, really good to hear! 😃 I've been trying to open issues and post solutions to clear up some of the confusion around ESM + TS interop. |
Right, so, if I want to use native ESM in my TS files, I actually need to do I will need to play with this more. I've been using |
Yep, counterintuitive but true. |
Current behavior
Using
cy.task
fails when it is used along with TypeScript code which uses fully-specified ESM imports (imports using.js
extension, where there is no matching.js
file because all of the files are TypeScript files , see microsoft/TypeScript#41887 (comment)).For example:
Note the difference in file names above. As per microsoft/TypeScript#41887 (comment), this should work in TypeScript (already works when using
tsc
)Error message:
Desired behavior
Fully-specified ESM imports using
.js
file extensions should work in TypeScript codeTwo details:
ts-node
feat(register): resolve .js to .ts in ts-node/register TypeStrong/ts-node#13618.5.4
version ofts-node
is currently used in the macOS Cypress.app via theserver
package (apparently? I was surprised to find it there):cypress/packages/server/package.json
Line 115 in 024308b
Test code to reproduce
See above
Cypress Version
9.5.1
Other
It would be nice if Cypress also supported Node.js ESM code just generally, but I think there are other issues for that:
The text was updated successfully, but these errors were encountered: