Cypress: plugins.js - async/await causes invalid import errors

Created on 14 Apr 2020  路  11Comments  路  Source: cypress-io/cypress

Current behavior:

Using async within a plugins.ts file results in a (non-informative) error.

**Message:** The plugins file is missing or invalid.

Your `pluginsFile` is set to `/srv/mossaino/common/asset_files/pwa/tests/plugins/index.ts`, but either the file is missing, it contains a syntax error, or threw an error when required. The `pluginsFile` must be a `.js` or `.coffee` file.

Or you might have renamed the extension of your `pluginsFile` to `.ts`. If that's the case, restart the test runner.

Please fix this, or set `pluginsFile` to `false` if a plugins file is not necessary for your project.

**Details:** /srv/mossaino/common/asset_files/pwa/tests/plugins/index.ts:13
import { __awaiter } from "tslib";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Module._compile (internal/modules/cjs/loader.js:896:18)
    at Module.m._compile (/home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/server/node_modules/ts-node/src/index.ts:536:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:986:10)
    at Object.require.extensions.<computed> [as .ts] (/home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/server/node_modules/ts-node/src/index.ts:539:12)
    at Module.load (internal/modules/cjs/loader.js:816:32)
    at Module._load (internal/modules/cjs/loader.js:728:14)
    at Module._load (electron/js2c/asar.js:717:26)
    at Function.Module._load (electron/js2c/asar.js:717:26)
    at Module.require (internal/modules/cjs/loader.js:853:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at module.exports (/home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/server/lib/plugins/child/run_plugins.js:206:15)
    at Object.<anonymous> (/home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/server/lib/plugins/child/index.js:9:25)
    at Module._compile (internal/modules/cjs/loader.js:968:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:986:10)
    at Module.load (internal/modules/cjs/loader.js:816:32)
    at Module._load (internal/modules/cjs/loader.js:728:14)

**Stack trace:**

[object Object]
    at Object.get (/home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/server/lib/errors.js:348:11)
    at EventEmitter.<anonymous> (/home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/server/lib/plugins/index.js:121:21)
    at EventEmitter.emit (events.js:210:5)
    at ChildProcess.<anonymous> (/home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/server/lib/plugins/util.js:25:29)
    at ChildProcess.emit (events.js:210:5)
    at emit (internal/child_process.js:876:12)
    at processTicksAndRejections (internal/process/task_queues.js:81:21)
From previous event:
    at fn (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:88741:14)
    at ipcBus (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:88760:10)
    at Object.ipc.<computed> [as openProject] (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:88801:69)
    at Object.openProject (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:91213:67)
    at Project.componentDidMount (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:90797:78)
    at ik (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:75383:360)
    at ../../node_modules/scheduler/cjs/scheduler.production.min.js.exports.unstable_runWithPriority (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:85486:437)
    at fg (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:75252:325)
    at Yj (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:75376:437)
    at Lj (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:75355:420)
    at file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:75253:115
    at ../../node_modules/scheduler/cjs/scheduler.production.min.js.exports.unstable_runWithPriority (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:85486:437)
    at fg (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:75252:325)
    at ig (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:75253:61)
    at jg (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:75252:496)
    at bk (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:75356:156)
    at reactionScheduler (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:48924:47)
    at runReactions (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:48900:5)
    at endBatch (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:48600:9)
From previous event:
    at App.componentDidMount (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:86770:77)
    at ik (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:75383:360)
    at ../../node_modules/scheduler/cjs/scheduler.production.min.js.exports.unstable_runWithPriority (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:85486:437)
    at fg (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:75252:325)
    at Yj (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:75376:437)
    at Lj (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:75355:420)
    at Rg (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:75346:137)
    at rk (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:75411:43)
    at file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:75414:301
    at ck (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:75356:210)
    at zk (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:75414:287)
    at render (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:75418:416)
    at Object.start (file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/app.js:89373:61)
    at file:///home/ebruines/.cache/Cypress/4.4.0/Cypress/resources/app/packages/desktop-gui/dist/index.html:29:11

Desired behavior:

No errors, as well as being able to use async.

Test code to reproduce

plugins.ts

module.exports = (on: Cypress.PluginEvents, config: Cypress.PluginConfig) => {
  on('task', {
    'hello-world': () => async () {
      return true
    }
  })
}

and alternatively

plugins.js

module.exports = (on, config) => {
  on('task', {
    'hello-world': () => async () {
      return true
    }
  })
}


Versions

Note that the plugins.js file mentioned, was valid code in Cypress 4.3.0.

Cypress: 4.4.0, 4.4.1, 4.5.0
OS: Linux
Browser: not applicable
plugins 鈿欙笍 typescript v4.4.0

Most helpful comment

I'm having similar problems when setting "importHelpers" to true in the tsconfig.json file, although I'm not 100% sure this is the same issue, given that my plugins file is still a ".js".
Setting "importHelpers" to false in cypress' tsconfig.json does not help.

I can prepare a minimal reproduction if you think it'd be helpful in any way.

All 11 comments

Also occurs if any of the imported/required files use async.

Reproducable code

helper.js

export async function sayWelcome() {}

plugins.js

const helper = require('./helper') 
// import helper from './helper' does not make a difference

module.exports = (on, config) => {
  on('task', {
    'hello-world': () => () {
      return helper.sayWelcome()
    }
  })
}

According to https://github.com/cypress-io/cypress/issues/7006 my tsconfig.json file is ignored anyways, so it should not be related.

I have a related problem problem but for another usage of tslib and I don't have async functions here. I don't even have typescript in those tests codebase and I don't have any plugin files.

[2020-04-13T22:44:57.007Z] Cannot use import statement outside a module

[2020-04-13T22:44:57.019Z] /home/jenkins/.cache/Cypress/4.4.0/Cypress/resources/app/packages/reporter/lib/resolve-dist.js:1

[2020-04-13T22:44:57.019Z] import { __read, __spread } from "tslib";

[2020-04-13T22:44:57.019Z] ^^^^^^

[2020-04-13T22:44:57.019Z] 

[2020-04-13T22:44:57.019Z] SyntaxError: Cannot use import statement outside a module

[2020-04-13T22:44:57.019Z]     at Module._compile (internal/modules/cjs/loader.js:896:18)

[2020-04-13T22:44:57.019Z]     at Module.m._compile (/home/jenkins/.cache/Cypress/4.4.0/Cypress/resources/app/packages/server/node_modules/ts-node/src/index.ts:536:23)

[2020-04-13T22:44:57.019Z]     at Module._extensions..js (internal/modules/cjs/loader.js:986:10)

[2020-04-13T22:44:57.019Z]     at Object.require.extensions.<computed> [as .js] (/home/jenkins/.cache/Cypress/4.4.0/Cypress/resources/app/packages/server/node_modules/ts-node/src/index.ts:539:12)

[2020-04-13T22:44:57.019Z]     at Module.load (internal/modules/cjs/loader.js:816:32)

[2020-04-13T22:44:57.019Z]     at Module._load (internal/modules/cjs/loader.js:728:14)

[2020-04-13T22:44:57.019Z]     at Module._load (electron/js2c/asar.js:717:26)

[2020-04-13T22:44:57.019Z]     at Function.Module._load (electron/js2c/asar.js:717:26)

[2020-04-13T22:44:57.019Z]     at Module.require (internal/modules/cjs/loader.js:853:19)

[2020-04-13T22:44:57.019Z]     at require (internal/modules/cjs/helpers.js:74:18)

[2020-04-13T22:44:57.019Z]     at Object.<anonymous> (/home/jenkins/.cache/Cypress/4.4.0/Cypress/resources/app/packages/server/lib/controllers/reporter.js:8:14)

[2020-04-13T22:44:57.019Z]     at Object.<anonymous> (/home/jenkins/.cache/Cypress/4.4.0/Cypress/resources/app/packages/server/lib/controllers/reporter.js:18:4)

[2020-04-13T22:44:57.019Z]     at Module._compile (internal/modules/cjs/loader.js:968:30)

[2020-04-13T22:44:57.019Z]     at Module.m._compile (/home/jenkins/.cache/Cypress/4.4.0/Cypress/resources/app/packages/server/node_modules/ts-node/src/index.ts:536:23)

[2020-04-13T22:44:57.019Z]     at Module._extensions..js (internal/modules/cjs/loader.js:986:10)

[2020-04-13T22:44:57.019Z]     at Object.require.extensions.<computed> [as .js] (/home/jenkins/.cache/Cypress/4.4.0/Cypress/resources/app/packages/server/node_modules/ts-node/src/index.ts:539:12)

[2020-04-13T22:44:57.019Z]     at Module.load (internal/modules/cjs/loader.js:816:32)

[2020-04-13T22:44:57.019Z]     at Module._load (internal/modules/cjs/loader.js:728:14)

[2020-04-13T22:44:57.019Z]     at Module._load (electron/js2c/asar.js:717:26)

[2020-04-13T22:44:57.019Z]     at Function.Module._load (electron/js2c/asar.js:717:26)

[2020-04-13T22:44:57.019Z]     at Module.require (internal/modules/cjs/loader.js:853:19)

[2020-04-13T22:44:57.019Z]     at require (internal/modules/cjs/helpers.js:74:18)

[2020-04-13T22:44:57.019Z]     at Object.<anonymous> (/home/jenkins/.cache/Cypress/4.4.0/Cypress/resources/app/packages/server/lib/routes.js:8:18)

[2020-04-13T22:44:57.019Z]     at Module._compile (internal/modules/cjs/loader.js:968:30)

[2020-04-13T22:44:57.019Z]     at Module.m._compile (/home/jenkins/.cache/Cypress/4.4.0/Cypress/resources/app/packages/server/node_modules/ts-node/src/index.ts:536:23)

[2020-04-13T22:44:57.019Z]     at Module._extensions..js (internal/modules/cjs/loader.js:986:10)

[2020-04-13T22:44:57.020Z]     at Object.require.extensions.<computed> [as .js] (/home/jenkins/.cache/Cypress/4.4.0/Cypress/resources/app/packages/server/node_modules/ts-node/src/index.ts:539:12)

[2020-04-13T22:44:57.020Z]     at Module.load (internal/modules/cjs/loader.js:816:32)

[2020-04-13T22:44:57.020Z]     at Module._load (internal/modules/cjs/loader.js:728:14)

[2020-04-13T22:44:57.020Z]     at Module._load (electron/js2c/asar.js:717:26)

[2020-04-13T22:44:57.020Z]     at Function.Module._load (electron/js2c/asar.js:717:26)

[2020-04-13T22:44:57.020Z]     at Module.require (internal/modules/cjs/loader.js:853:19)

[2020-04-13T22:44:57.020Z]     at require (internal/modules/cjs/helpers.js:74:18)

[2020-04-13T22:44:57.020Z]     at Server.createRoutes (/home/jenkins/.cache/Cypress/4.4.0/Cypress/resources/app/packages/server/lib/server.js:157:14)

[2020-04-13T22:44:57.020Z]     at /home/jenkins/.cache/Cypress/4.4.0/Cypress/resources/app/packages/server/lib/server.js:200:17

[2020-04-13T22:44:57.020Z]     at tryCatcher (/home/jenkins/.cache/Cypress/4.4.0/Cypress/resources/app/packages/server/node_modules/bluebird/js/release/util.js:16:23)

[2020-04-13T22:44:57.020Z]     at Function.Promise.attempt.Promise.try (/home/jenkins/.cache/Cypress/4.4.0/Cypress/resources/app/packages/server/node_modules/bluebird/js/release/method.js:39:29)

[2020-04-13T22:44:57.020Z]     at Server.open (/home/jenkins/.cache/Cypress/4.4.0/Cypress/resources/app/packages/server/lib/server.js:178:28)

[2020-04-13T22:44:57.020Z]     at /home/jenkins/.cache/Cypress/4.4.0/Cypress/resources/app/packages/server/lib/project.js:133:26

[2020-04-13T22:44:57.020Z]     at tryCatcher (/home/jenkins/.cache/Cypress/4.4.0/Cypress/resources/app/packages/server/node_modules/bluebird/js/release/util.js:16:23)

[2020-04-13T22:44:57.020Z]     at Promise._settlePromiseFromHandler (/home/jenkins/.cache/Cypress/4.4.0/Cypress/resources/app/packages/server/node_modules/bluebird/js/release/promise.js:547:31)

[2020-04-13T22:44:57.020Z]     at Promise._settlePromise (/home/jenkins/.cache/Cypress/4.4.0/Cypress/resources/app/packages/server/node_modules/bluebird/js/release/promise.js:604:18)

[2020-04-13T22:44:57.020Z]     at Promise._settlePromise0 (/home/jenkins/.cache/Cypress/4.4.0/Cypress/resources/app/packages/server/node_modules/bluebird/js/release/promise.js:649:10)

[2020-04-13T22:44:57.020Z]     at Promise._settlePromises (/home/jenkins/.cache/Cypress/4.4.0/Cypress/resources/app/packages/server/node_modules/bluebird/js/release/promise.js:729:18)

[2020-04-13T22:44:57.020Z]     at _drainQueueStep (/home/jenkins/.cache/Cypress/4.4.0/Cypress/resources/app/packages/server/node_modules/bluebird/js/release/async.js:93:12)

[2020-04-13T22:44:57.020Z]     at _drainQueue (/home/jenkins/.cache/Cypress/4.4.0/Cypress/resources/app/packages/server/node_modules/bluebird/js/release/async.js:86:9)

[2020-04-13T22:44:57.020Z]     at Async._drainQueues (/home/jenkins/.cache/Cypress/4.4.0/Cypress/resources/app/packages/server/node_modules/bluebird/js/release/async.js:102:5)

[2020-04-13T22:44:57.020Z]     at Immediate.Async.drainQueues [as _onImmediate] (/home/jenkins/.cache/Cypress/4.4.0/Cypress/resources/app/packages/server/node_modules/bluebird/js/release/async.js:15:14)

[2020-04-13T22:44:57.020Z]     at processImmediate (internal/timers.js:439:21)

[2020-04-13T22:44:57.069Z] 

script returned exit code 1

Possible same issue like #7005

@EtienneBruines can you try my workaround?

plugins.ts

function plugins(on: Cypress.PluginEvents, _config: Cypress.ConfigOptions): void {
  on('before:browser:launch', () => {
    console.log("hello");
  });
}

module.exports = plugins;

@CSchulz I thought about it (and really liked learning that on: Cypress.PluginEvents - so thank you for that :smile: ), but this issue really seems to be linked to async/await, so once I include it, the preprocessing wants to import some code it can't handle.

IIRC

function plugins(on: Cypress.PluginEvents, _config: Cypress.ConfigOptions): void {
  on('before:browser:launch', () => {
    console.log("hello");
  });
}

module.exports = plugins;

the above works, but this one does not:

function plugins(on: Cypress.PluginEvents, _config: Cypress.ConfigOptions): void {
  on('before:browser:launch', async () => {
    console.log("hello");
  });
}

module.exports = plugins;

I'm having similar problems when setting "importHelpers" to true in the tsconfig.json file, although I'm not 100% sure this is the same issue, given that my plugins file is still a ".js".
Setting "importHelpers" to false in cypress' tsconfig.json does not help.

I can prepare a minimal reproduction if you think it'd be helpful in any way.

@EtienneBruines could you paste your tsconfig.json file, please?

Can someone provide a reproducible example - I have placed async and import code in https://github.com/cypress-io/cypress-test-tiny/tree/async-problem-7011 and Cypress 4.5.0 skips loading plugins.ts BUT Cypress from develop branch already loads everything correctly. It would be nice to confirm it though.

{
  "compilerOptions": {
    "module": "esnext",
    "allowJs": true,
    "importHelpers": true
  }
}

Trying https://github.com/cypress-io/cypress/pull/7166 and it works.

Tested with the latest on develop, b515e036dd0c0147a6e699ab252d5571d4420002 at the moment.

I have narrowed it down to this minimal tsconfig.json:

{
  "compilerOptions": {
    "module": "esnext",
    "allowJs": true,
    "importHelpers": true
  }
}

Removing allowJs -> compiles. Removing importHelpers -> compiles. But those combined, causes issues.

import { __awaiter, __generator } from "tslib";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Module._compile (internal/modules/cjs/loader.js:896:18)
    at Module.m._compile (/home/ebruines/.cache/Cypress/4.5.1/Cypress/resources/app/packages/server/node_modules/ts-node/src/index.ts:536:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:986:10)
    at Object.require.extensions.<computed> [as .js] (/home/ebruines/.cache/Cypress/4.5.1/Cypress/resources/app/packages/server/node_modules/ts-node/src/index.ts:539:12)
    at Module.load (internal/modules/cjs/loader.js:816:32)
    at Module._load (internal/modules/cjs/loader.js:728:14)
    at Module._load (electron/js2c/asar.js:717:26)
    at Function.Module._load (electron/js2c/asar.js:717:26)
    at Module.require (internal/modules/cjs/loader.js:853:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at module.exports (/home/ebruines/.cache/Cypress/4.5.1/Cypress/resources/app/packages/server/lib/plugins/child/run_plugins.js:206:15)
    at Object.<anonymous> (/home/ebruines/.cache/Cypress/4.5.1/Cypress/resources/app/packages/server/lib/plugins/child/index.js:9:25)
    at Module._compile (internal/modules/cjs/loader.js:968:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:986:10)
    at Module.load (internal/modules/cjs/loader.js:816:32)
    at Module._load (internal/modules/cjs/loader.js:728:14)

Having no tsconfig.json at all, does make it compile correctly as well.

Full tsconfig.json - just for those that are interested

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "outDir": "./dist/",
    "sourceMap": true,
    "noImplicitAny": true,
    "module": "esnext",
    "target": "ES2016",
    "jsx": "react",
    "allowJs": true,
    "moduleResolution": "node",
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "importHelpers": true,
    "lib": [
      "dom",
      "webworker",
      "es2015.promise"
    ]
  },
  "include": [
    "**/*.ts",
    "**/*.vue"
  ],
  "exclude": [
    "node_modules"
  ],
  "files": [
    "ts-shim.d.ts"
  ]
}

Note that I'm testing this for plugin.js - a JavaScript file, not a TypeScript file. Not sure if it makes a difference

The code for this is done in cypress-io/cypress#7197, but has yet to be released.
We'll update this issue and reference the changelog when it's released.

~~Tested on Linux: not fixed by aae90bb

7197 did not fix the issue I was having.~~

Caching is the root of all evil ...

Nevermind, works as intended...

Thank you @bahmutov for the fix :+1:

Released in 4.6.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to
Cypress v4.6.0, please open a new issue.

Was this page helpful?
0 / 5 - 0 ratings