Skip to content
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

npm build and npm test in functions directory both fail due to type errors #3

Closed
miridius opened this issue Apr 24, 2020 · 5 comments
Closed

Comments

@miridius
Copy link

When I try to run npm build or npm test in the functions directory, I get errors like these:

> functions@ pretest /mnt/c/Users/drolle/Repos/covid19risk/covidwatch-cloud-functions/functions
> tsc --build tsconfig.test.json

../../../../node_modules/@types/jest/index.d.ts:35:13 - error TS2403: Subsequent variable declarations must have the same type.  Variable 'beforeEach' must be of type 'HookFunction', but here has type 'Lifecycle'.

35 declare var beforeEach: jest.Lifecycle;
               ~~~~~~~~~~

  node_modules/@types/mocha/index.d.ts:2734:13
    2734 declare var beforeEach: Mocha.HookFunction;
                     ~~~~~~~~~~
    'beforeEach' was also declared here.

../../../../node_modules/@types/jest/index.d.ts:37:13 - error TS2403: Subsequent variable declarations must have the same type.  Variable 'afterEach' must be of type 'HookFunction', but here has type 'Lifecycle'.

37 declare var afterEach: jest.Lifecycle;
               ~~~~~~~~~

  node_modules/@types/mocha/index.d.ts:2752:13
    2752 declare var afterEach: Mocha.HookFunction;
                     ~~~~~~~~~
    'afterEach' was also declared here.

../../../../node_modules/@types/jest/index.d.ts:38:13 - error TS2403: Subsequent variable declarations must have the same type.  Variable 'describe' must be of type 'SuiteFunction', but here has type 'Describe'.

38 declare var describe: jest.Describe;
               ~~~~~~~~

  node_modules/@types/mocha/index.d.ts:2768:13
    2768 declare var describe: Mocha.SuiteFunction;
                     ~~~~~~~~
    'describe' was also declared here.

../../../../node_modules/@types/jest/index.d.ts:40:13 - error TS2403: Subsequent variable declarations must have the same type.  Variable 'xdescribe' must be of type 'PendingSuiteFunction', but here has type 'Describe'.

40 declare var xdescribe: jest.Describe;
               ~~~~~~~~~

  node_modules/@types/mocha/index.d.ts:2789:13
    2789 declare var xdescribe: Mocha.PendingSuiteFunction;
                     ~~~~~~~~~
    'xdescribe' was also declared here.

../../../../node_modules/@types/jest/index.d.ts:41:13 - error TS2403: Subsequent variable declarations must have the same type.  Variable 'it' must be of type 'TestFunction', but here has type 'It'.

41 declare var it: jest.It;
               ~~

  node_modules/@types/mocha/index.d.ts:2803:13
    2803 declare var it: Mocha.TestFunction;
                     ~~
    'it' was also declared here.

../../../../node_modules/@types/jest/index.d.ts:43:13 - error TS2403: Subsequent variable declarations must have the same type.  Variable 'xit' must be of type 'PendingTestFunction', but here has type 'It'.

43 declare var xit: jest.It;
               ~~~

  node_modules/@types/mocha/index.d.ts:2824:13
    2824 declare var xit: Mocha.PendingTestFunction;
                     ~~~
    'xit' was also declared here.

../../../../node_modules/@types/jest/index.d.ts:44:13 - error TS2403: Subsequent variable declarations must have the same type.  Variable 'test' must be of type 'TestFunction', but here has type 'It'.

44 declare var test: jest.It;
               ~~~~

  node_modules/@types/mocha/index.d.ts:2817:13
    2817 declare var test: Mocha.TestFunction;
                     ~~~~
    'test' was also declared here.


Found 7 errors.
@madhavajay
Copy link
Collaborator

I had to switch the build and test ts config files around due to the way the firebase deploy script expects the build to be tsconfig.json to be the main one, and then added some stuff to get it to transpile the .ts into .js via babel:

"ts-babel": "npx babel src --out-dir lib --extensions '.ts,.tsx'",
"build": "npm run tsc && npm run ts-babel",

So we will need something similar to run before testing which converts the .ts to .js since this is no longer being done by typescript due to the need for babel to get some "advanced features" like the decorators used by the sparkson library.

Also the tests will fail due to the security rules in firestore.rules as well but we can sort that out next once everyone has finished migrating to the REST end point.

@miridius
Copy link
Author

I don't think the issue is in .ts to .js conversion, it's that in your dependency tree you have multiple libraries (jest and mocha) specifying the same type name differently. I would suggest remove mocha & chai dependencies and just add jest since afaik jest includes its own equivalents of those libraries already.

@inmyth
Copy link
Collaborator

inmyth commented Apr 28, 2020

@miridius Do you still have this issue ? So on my side I can build and run the test ok. It was just the test script in package.json should point to .mocha/test as it's where the test files are generated.
I included the fix in my PR. Also I wrote my test in mocha but in case there is need to change the test framework we should settle it first.

@miridius
Copy link
Author

@inmyth I still have the exact same issues.

Steps taken:

  1. Checkout your PR
  2. cd functions
  3. rm -rf node_modules
  4. npm install
  5. npm run build
  6. npm test

Results:

» npm run build

> functions@ build /mnt/c/Users/drolle/Repos/covid19risk/covidwatch-cloud-functions/functions
> npm run tsc && npm run ts-babel


> functions@ tsc /mnt/c/Users/drolle/Repos/covid19risk/covidwatch-cloud-functions/functions
> tsc --build tsconfig.json

../../../../node_modules/@types/jest/index.d.ts:35:13 - error TS2403: Subsequent variable declarations must have the same type.  Variable 'beforeEach' must be of type 'HookFunction', but here has type 'Lifecycle'.

35 declare var beforeEach: jest.Lifecycle;
               ~~~~~~~~~~

  node_modules/@types/mocha/index.d.ts:2734:13
    2734 declare var beforeEach: Mocha.HookFunction;
                     ~~~~~~~~~~
    'beforeEach' was also declared here.

../../../../node_modules/@types/jest/index.d.ts:37:13 - error TS2403: Subsequent variable declarations must have the same type.  Variable 'afterEach' must be of type 'HookFunction', but here has type 'Lifecycle'.

37 declare var afterEach: jest.Lifecycle;
               ~~~~~~~~~

  node_modules/@types/mocha/index.d.ts:2752:13
    2752 declare var afterEach: Mocha.HookFunction;
                     ~~~~~~~~~
    'afterEach' was also declared here.

../../../../node_modules/@types/jest/index.d.ts:38:13 - error TS2403: Subsequent variable declarations must have the same type.  Variable 'describe' must be of type 'SuiteFunction', but here has type 'Describe'.

38 declare var describe: jest.Describe;
               ~~~~~~~~

  node_modules/@types/mocha/index.d.ts:2768:13
    2768 declare var describe: Mocha.SuiteFunction;
                     ~~~~~~~~
    'describe' was also declared here.

../../../../node_modules/@types/jest/index.d.ts:40:13 - error TS2403: Subsequent variable declarations must have the same type.  Variable 'xdescribe' must be of type 'PendingSuiteFunction', but here has type 'Describe'.

40 declare var xdescribe: jest.Describe;
               ~~~~~~~~~

  node_modules/@types/mocha/index.d.ts:2789:13
    2789 declare var xdescribe: Mocha.PendingSuiteFunction;
                     ~~~~~~~~~
    'xdescribe' was also declared here.

../../../../node_modules/@types/jest/index.d.ts:41:13 - error TS2403: Subsequent variable declarations must have the same type.  Variable 'it' must be of type 'TestFunction', but here has type 'It'.

41 declare var it: jest.It;
               ~~

  node_modules/@types/mocha/index.d.ts:2803:13
    2803 declare var it: Mocha.TestFunction;
                     ~~
    'it' was also declared here.

../../../../node_modules/@types/jest/index.d.ts:43:13 - error TS2403: Subsequent variable declarations must have the same type.  Variable 'xit' must be of type 'PendingTestFunction', but here has type 'It'.

43 declare var xit: jest.It;
               ~~~

  node_modules/@types/mocha/index.d.ts:2824:13
    2824 declare var xit: Mocha.PendingTestFunction;
                     ~~~
    'xit' was also declared here.

../../../../node_modules/@types/jest/index.d.ts:44:13 - error TS2403: Subsequent variable declarations must have the same type.  Variable 'test' must be of type 'TestFunction', but here has type 'It'.

44 declare var test: jest.It;
               ~~~~

  node_modules/@types/mocha/index.d.ts:2817:13
    2817 declare var test: Mocha.TestFunction;
                     ~~~~
    'test' was also declared here.


Found 7 errors.

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! functions@ tsc: `tsc --build tsconfig.json`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the functions@ tsc script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/drolle/.npm/_logs/2020-04-29T09_56_07_312Z-debug.log
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! functions@ build: `npm run tsc && npm run ts-babel`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the functions@ build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/drolle/.npm/_logs/2020-04-29T09_56_07_382Z-debug.log
» npm test                                                                                       1 ↵

> functions@ pretest /mnt/c/Users/drolle/Repos/covid19risk/covidwatch-cloud-functions/functions
> tsc --build tsconfig.test.json

../../../../node_modules/@types/jest/index.d.ts:35:13 - error TS2403: Subsequent variable declarations must have the same type.  Variable 'beforeEach' must be of type 'HookFunction', but here has type 'Lifecycle'.

35 declare var beforeEach: jest.Lifecycle;
               ~~~~~~~~~~

  node_modules/@types/mocha/index.d.ts:2734:13
    2734 declare var beforeEach: Mocha.HookFunction;
                     ~~~~~~~~~~
    'beforeEach' was also declared here.

../../../../node_modules/@types/jest/index.d.ts:37:13 - error TS2403: Subsequent variable declarations must have the same type.  Variable 'afterEach' must be of type 'HookFunction', but here has type 'Lifecycle'.

37 declare var afterEach: jest.Lifecycle;
               ~~~~~~~~~

  node_modules/@types/mocha/index.d.ts:2752:13
    2752 declare var afterEach: Mocha.HookFunction;
                     ~~~~~~~~~
    'afterEach' was also declared here.

../../../../node_modules/@types/jest/index.d.ts:38:13 - error TS2403: Subsequent variable declarations must have the same type.  Variable 'describe' must be of type 'SuiteFunction', but here has type 'Describe'.

38 declare var describe: jest.Describe;
               ~~~~~~~~

  node_modules/@types/mocha/index.d.ts:2768:13
    2768 declare var describe: Mocha.SuiteFunction;
                     ~~~~~~~~
    'describe' was also declared here.

../../../../node_modules/@types/jest/index.d.ts:40:13 - error TS2403: Subsequent variable declarations must have the same type.  Variable 'xdescribe' must be of type 'PendingSuiteFunction', but here has type 'Describe'.

40 declare var xdescribe: jest.Describe;
               ~~~~~~~~~

  node_modules/@types/mocha/index.d.ts:2789:13
    2789 declare var xdescribe: Mocha.PendingSuiteFunction;
                     ~~~~~~~~~
    'xdescribe' was also declared here.

../../../../node_modules/@types/jest/index.d.ts:41:13 - error TS2403: Subsequent variable declarations must have the same type.  Variable 'it' must be of type 'TestFunction', but here has type 'It'.

41 declare var it: jest.It;
               ~~

  node_modules/@types/mocha/index.d.ts:2803:13
    2803 declare var it: Mocha.TestFunction;
                     ~~
    'it' was also declared here.

../../../../node_modules/@types/jest/index.d.ts:43:13 - error TS2403: Subsequent variable declarations must have the same type.  Variable 'xit' must be of type 'PendingTestFunction', but here has type 'It'.

43 declare var xit: jest.It;
               ~~~

  node_modules/@types/mocha/index.d.ts:2824:13
    2824 declare var xit: Mocha.PendingTestFunction;
                     ~~~
    'xit' was also declared here.

../../../../node_modules/@types/jest/index.d.ts:44:13 - error TS2403: Subsequent variable declarations must have the same type.  Variable 'test' must be of type 'TestFunction', but here has type 'It'.

44 declare var test: jest.It;
               ~~~~

  node_modules/@types/mocha/index.d.ts:2817:13
    2817 declare var test: Mocha.TestFunction;
                     ~~~~
    'test' was also declared here.


Found 7 errors.

npm ERR! Test failed.  See above for more details.

Note: I can get the build working by uninstalling @types/mocha but then of course the tests fail even worse because they need those types. There must be some transitive dependency which includes @types/jest and that is clashing with @types/mocha

@miridius
Copy link
Author

miridius commented Apr 29, 2020

Never mind I figured it out... @types/jest was in my user node_modules directory... I guess I installed something globally by accident. rm -rf ~/node_modules solved my problem. Sorry! 🤦‍♂️ 😀

AndrewBlackledge added a commit to anicolao/todo that referenced this issue Jul 5, 2023
Work around having mocha and jest installed at the same time.

This description is from
https://stackoverflow.com/questions/55680391/typescript-error-ts2403-subsequent-variable-declarations-must-have-the-same-typ

  You can't put together mocha (and other test runners that use mocha,
  such as web-test-runner) together in the same module.

  Types can be defined only once, and mocha and jest declare a series of
  globals (needed so that they can be used directly without importing)
  which are incompatible with each other. You need to commit to one or
  the other, or if you use things such as web-test-runner or
  electron-mocha just choose another runner (cypress will do, for
  instance).

The fix is taken from
covidwatchorg/covidwatch-cloud-functions#3

  Add the following property to the tsconfig file

  "compilerOptions": {
      "skipLibCheck": true
  },

  This tells TypeScript that we want to skip the type checking of
  libraries in the node_modules folder. This saves compilation time and
  stops conflicting types in node modules from breaking the build.

  See https://www.typescriptlang.org/tsconfig#skipLibCheck
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants