question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cypress 3.2.0 exports mocha/chai globally

See original GitHub issue

Current behavior:

I can’t exactly pinpoint the cause of this, but I think after this change:

All third party @types have been moved from Cypress dependencies into devDependencies. 
This should fix situations where user's type dependencies were conflicting with Cypress's third 
party type definitions. Fixes #3371 and #1227.

Pull request was 3425

We are having duplicate conflicts between Cypress and our packages. In our case, we use Typescript with Mocha and Chai to write type-safe unit tests, and because they are exported globally by Mocha and Chai itself we get duplicate exports between Cypress and Chai/Mocha. Our linter reports the following:

ERROR in C:/Development/alloy-vue/node_modules/@types/chai/index.d.ts
40:17 Duplicate identifier 'Operator'.
    38 |     }
    39 | 
  > 40 |     export type Operator = string; // "==" | "===" | ">" | ">=" | "<" | "<=" | "!=" | "!==";
       |                 ^
    41 | 
    42 |     export type OperatorComparable = boolean | null | number | string | undefined | Date;
    43 | 

Version: typescript 3.3.4000, tslint 5.14.0

ERROR in C:/Development/alloy-vue/node_modules/@types/chai/index.d.ts
42:17 Duplicate identifier 'OperatorComparable'.
    40 |     export type Operator = string; // "==" | "===" | ">" | ">=" | "<" | "<=" | "!=" | "!==";
    41 | 
  > 42 |     export type OperatorComparable = boolean | null | number | string | undefined | Date;
       |                 ^
    43 | 
    44 |     interface ShouldAssertion {
    45 |         equal(value1: any, value2: any, message?: string): void;
ERROR in C:/Development/alloy-vue/node_modules/@types/chai/index.d.ts
1691:18 Duplicate identifier 'AssertionError'.
    1689 |     }
    1690 | 
  > 1691 |     export class AssertionError {
         |                  ^
    1692 |         constructor(message: string, _props?: any, ssf?: Function);
    1693 |         name: string;
    1694 |         message: string;
ERROR in C:/Development/alloy-vue/node_modules/@types/chai/index.d.ts
1700:15 Cannot redeclare block-scoped variable 'chai'.
    1698 | }
    1699 | 
  > 1700 | declare const chai: Chai.ChaiStatic;
         |               ^
    1701 | 
    1702 | declare module "chai" {
    1703 |     export = chai;
ERROR in C:/Development/alloy-vue/node_modules/@types/chai/index.d.ts
1703:14 Cannot redeclare block-scoped variable 'chai'.
    1701 | 
    1702 | declare module "chai" {
  > 1703 |     export = chai;
         |              ^
    1704 | }
    1705 | 
    1706 | interface Object {
ERROR in C:/Development/alloy-vue/node_modules/@types/mocha/index.d.ts
17:1 Definitions of the following identifiers conflict with those in another file: setup, teardown, suiteSetup, suiteTeardown, before, after, beforeEach, afterEach, mocha, MochaDone, ReporterConstructor, Mocha
    15 |  * @see https://mochajs.org/api/mocha
    16 |  */
  > 17 | declare class Mocha {
       | ^
    18 |     private _growl;
    19 |     private _reporter;
    20 |     private _ui;
ERROR in C:/Development/alloy-vue/node_modules/@types/mocha/index.d.ts
2837:11 Interface 'MochaSetupOptions' incorrectly extends interface 'MochaOptions'.
  Types of property 'ui' are incompatible.
    Type 'string | undefined' is not assignable to type '"bdd" | "tdd" | "qunit" | "exports" | undefined'.
      Type 'string' is not assignable to type '"bdd" | "tdd" | "qunit" | "exports" | undefined'.
    2835 |  * Options to pass to `mocha.setup` in the browser.
    2836 |  */
  > 2837 | interface MochaSetupOptions extends Mocha.MochaOptions {
         |           ^
    2838 |     /** @deprecated This is not used by Mocha. Use `files` instead. */
    2839 |     require?: string[];
    2840 |     fullTrace?: boolean;
ERROR in C:/Development/alloy-vue/node_modules/cypress/types/chai/index.d.ts
40:17 Duplicate identifier 'Operator'.
    38 |     }
    39 | 
  > 40 |     export type Operator = string; // "==" | "===" | ">" | ">=" | "<" | "<=" | "!=" | "!==";
       |                 ^
    41 | 
    42 |     export type OperatorComparable = boolean | null | number | string | undefined | Date;
    43 | 
ERROR in C:/Development/alloy-vue/node_modules/cypress/types/chai/index.d.ts
42:17 Duplicate identifier 'OperatorComparable'.
    40 |     export type Operator = string; // "==" | "===" | ">" | ">=" | "<" | "<=" | "!=" | "!==";
    41 | 
  > 42 |     export type OperatorComparable = boolean | null | number | string | undefined | Date;
       |                 ^
    43 | 
    44 |     interface ShouldAssertion {
    45 |         equal(value1: any, value2: any, message?: string): void;
ERROR in C:/Development/alloy-vue/node_modules/cypress/types/chai/index.d.ts
1607:18 Duplicate identifier 'AssertionError'.
    1605 |     }
    1606 | 
  > 1607 |     export class AssertionError {
         |                  ^
    1608 |         constructor(message: string, _props?: any, ssf?: Function);
    1609 |         name: string;
    1610 |         message: string;
ERROR in C:/Development/alloy-vue/node_modules/cypress/types/chai/index.d.ts
1616:15 Cannot redeclare block-scoped variable 'chai'.
    1614 | }
    1615 | 
  > 1616 | declare const chai: Chai.ChaiStatic;
         |               ^
    1617 | 
    1618 | declare module "chai" {
    1619 |     export = chai;
ERROR in C:/Development/alloy-vue/node_modules/cypress/types/chai/index.d.ts
1619:14 Cannot redeclare block-scoped variable 'chai'.
    1617 | 
    1618 | declare module "chai" {
  > 1619 |     export = chai;
         |              ^
    1620 | }
    1621 | 
    1622 | interface Object {
ERROR in C:/Development/alloy-vue/node_modules/cypress/types/mocha/index.d.ts
6:1 Definitions of the following identifiers conflict with those in another file: setup, teardown, suiteSetup, suiteTeardown, before, after, beforeEach, afterEach, mocha, MochaDone, ReporterConstructor, Mocha
    4 | // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
    5 | 
  > 6 | interface MochaSetupOptions {
      | ^
    7 |     //milliseconds to wait before considering a test slow
    8 |     slow?: number;
    9 | 
ERROR in C:/Development/alloy-vue/node_modules/cypress/types/mocha/index.d.ts
36:13 Subsequent variable declarations must have the same type.  Variable 'describe' must be of type 'SuiteFunction', but here has type 'IContextDefinition'.
    34 | 
    35 | declare var mocha: Mocha;
  > 36 | declare var describe: Mocha.IContextDefinition;
       |             ^
    37 | declare var xdescribe: Mocha.IContextDefinition;
    38 | // alias for `describe`
    39 | declare var context: Mocha.IContextDefinition;
ERROR in C:/Development/alloy-vue/node_modules/cypress/types/mocha/index.d.ts
37:13 Subsequent variable declarations must have the same type.  Variable 'xdescribe' must be of type 'PendingSuiteFunction', but here has type 'IContextDefinition'.
    35 | declare var mocha: Mocha;
    36 | declare var describe: Mocha.IContextDefinition;
  > 37 | declare var xdescribe: Mocha.IContextDefinition;
       |             ^
    38 | // alias for `describe`
    39 | declare var context: Mocha.IContextDefinition;
    40 | // alias for `describe`
ERROR in C:/Development/alloy-vue/node_modules/cypress/types/mocha/index.d.ts
39:13 Subsequent variable declarations must have the same type.  Variable 'context' must be of type 'SuiteFunction', but here has type 'IContextDefinition'.
    37 | declare var xdescribe: Mocha.IContextDefinition;
    38 | // alias for `describe`
  > 39 | declare var context: Mocha.IContextDefinition;
       |             ^
    40 | // alias for `describe`
    41 | declare var suite: Mocha.IContextDefinition;
    42 | declare var it: Mocha.ITestDefinition;
ERROR in C:/Development/alloy-vue/node_modules/cypress/types/mocha/index.d.ts
41:13 Subsequent variable declarations must have the same type.  Variable 'suite' must be of type 'SuiteFunction', but here has type 'IContextDefinition'.
    39 | declare var context: Mocha.IContextDefinition;
    40 | // alias for `describe`
  > 41 | declare var suite: Mocha.IContextDefinition;
       |             ^
    42 | declare var it: Mocha.ITestDefinition;
    43 | declare var xit: Mocha.ITestDefinition;
    44 | // alias for `it`
ERROR in C:/Development/alloy-vue/node_modules/cypress/types/mocha/index.d.ts
42:13 Subsequent variable declarations must have the same type.  Variable 'it' must be of type 'TestFunction', but here has type 'ITestDefinition'.
    40 | // alias for `describe`
    41 | declare var suite: Mocha.IContextDefinition;
  > 42 | declare var it: Mocha.ITestDefinition;
       |             ^
    43 | declare var xit: Mocha.ITestDefinition;
    44 | // alias for `it`
    45 | declare var test: Mocha.ITestDefinition;
ERROR in C:/Development/alloy-vue/node_modules/cypress/types/mocha/index.d.ts
43:13 Subsequent variable declarations must have the same type.  Variable 'xit' must be of type 'PendingTestFunction', but here has type 'ITestDefinition'.
    41 | declare var suite: Mocha.IContextDefinition;
    42 | declare var it: Mocha.ITestDefinition;
  > 43 | declare var xit: Mocha.ITestDefinition;
       |             ^
    44 | // alias for `it`
    45 | declare var test: Mocha.ITestDefinition;
    46 | declare var specify: Mocha.ITestDefinition;
ERROR in C:/Development/alloy-vue/node_modules/cypress/types/mocha/index.d.ts
45:13 Subsequent variable declarations must have the same type.  Variable 'test' must be of type 'TestFunction', but here has type 'ITestDefinition'.
    43 | declare var xit: Mocha.ITestDefinition;
    44 | // alias for `it`
  > 45 | declare var test: Mocha.ITestDefinition;
       |             ^
    46 | declare var specify: Mocha.ITestDefinition;
    47 | 
    48 | // Used with the --delay flag; see https://mochajs.org/#hooks
ERROR in C:/Development/alloy-vue/node_modules/cypress/types/mocha/index.d.ts
46:13 Subsequent variable declarations must have the same type.  Variable 'specify' must be of type 'TestFunction', but here has type 'ITestDefinition'.
    44 | // alias for `it`
    45 | declare var test: Mocha.ITestDefinition;
  > 46 | declare var specify: Mocha.ITestDefinition;
       |             ^
    47 | 
    48 | // Used with the --delay flag; see https://mochajs.org/#hooks
    49 | declare function run(): void;

So we can see that, for example Chai is exporting type definitions globally, but so is Cypress so they conflict with each other.

This is fixed on our side if we rollback to Cypress to 3.1.5 or if we remove Chai from our npm packages.

Desired behavior:

I don’t think Cypress should be exporting the packages globally, or at least keep it self contained within it’s own package.

Steps to reproduce: (app code and test code)

To exactly reproduce our case:

  1. Create a clean vue typescript project with vue-cli (https://vuejs.org/v2/guide/installation.html)
  2. Make sure to install Mocha, Chai and Cypress as part of it, all latest versions
  3. Setup TSLint if you want to get the same errors as above
  4. Run the vue-cli serve, wait for the linter to finish, notice the errors

Versions

Cypress version: 3.2.0 Operating system: Windows 10 Browser: Chrome Version 73.0.3683.86 (Official Build) (64-bit)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jennifer-shehanecommented, Mar 29, 2019

I created a new issue in our docs to document the global exports here: https://github.com/cypress-io/cypress-documentation/issues/1524. Our documentation is open source and contributions are welcome. 😄

0reactions
ghostcommented, Jun 8, 2020

@bahmutov In case when we need to trigger cypress tests from a mocha test, the two tsconfig workaround wont work. eg.

import chai, { expect } from "chai";
import cypress from "cypress";

...

describe("Running UI tests...", () => {
    it("should pass all UI tests", async () => {
        const result = await cypress.run({
            config: {
                baseUrl: "...",
            },
            env: {
                wp_username: "...",
                wp_password: "...",
            },
            spec: "src/cypress/integration/instance/*.test.js",
        });

        expect(result.runs.length).to.equal(1);
        expect(result.runs[0].stats.failures).to.equal(0);
    });
});

Didn’t see this until now, but you can circumvent this by using Cypress types for example:

cypress.expect(result.runs.length).to.equal(1);
cypress.expect(result.runs[0].stats.failures).to.equal(0);

So you won’t have to import Chai.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Changelog - Cypress Documentation
Next.js in Cypress is now allowed to import global styles. ... This fixes a regression introduced in 3.2.0 where responses with these status...
Read more >
cypress: Versions - Openbase
When running Cypress in global mode, an error will no longer be thrown when trying to ... error message when the plugins file...
Read more >
indexeddbshim - UNPKG
indexeddbshim/CHANGES.md ; 376, ## Version 3.2.0 ; 377 ; 378, - Fix: Deal with issue with Chrome/Node and `Number.MIN_VALUE` converting ; 379, to...
Read more >
cannot find module 'fs nodejs - JodiStory
Those run on linter2 will get process.cwd() if the global process object is ... doesn't contain module-level exports: Error: Cannot find module 'express' ......
Read more >
Mocha documentation — DevDocs
If a file loaded this way contains known Mocha-specific exports (e.g., ... Mocha runs global setup fixtures, if any; Starting with the “root”...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found