kblincoe / VisualGit_SE701_2019_4

1 stars 0 forks source link

Fix error module nodegit not found on windows through mocking #182

Closed YichenTang97 closed 5 years ago

YichenTang97 commented 5 years ago

Resolves #146.

Reason for the problem

What to do on the mocks You can add mock functions in the mocked modules (nodegit.js and electron.js) in one place to make them behave in the way you want for testing other classes, these mocked functions will be reflected in every test where these modules are required.

YichenTang97 commented 5 years ago

Tests for UserService is being fixed in #172 so blocking for now. Nice finding the solution by the way! Will test on macOS soon. I wonder why electron and nodegit was being picked up (macOS only) in early commits when the tests were being added.

Fixed merge conflicts and now both tests pass (also made a small fix on authenticate.component.test.ts). Some error message was thrown because document is not defined as shown below, but that does not affect the test result (test cases not covering that part). I think it would be fine to leave it like this by now? image

r4inee commented 5 years ago

Tests for UserService is being fixed in #172 so blocking for now. Nice finding the solution by the way! Will test on macOS soon. I wonder why electron and nodegit was being picked up (macOS only) in early commits when the tests were being added.

Fixed merge conflicts and now both tests pass (also made a small fix on authenticate.component.test.ts). Some error message was thrown because document is not defined as shown below, but that does not affect the test result (test cases not covering that part). I think it would be fine to leave it like this by now? image

Tests not working on macOS, also cannot npm run compile, it throws a lot of Cannot find name x errors when trying to compile, might be related to the downgrading of reflect-metadata but I'm unsure.

image

image Note: There are a lot more errors that this.

YichenTang97 commented 5 years ago

@sloushsu So weird... I've just made two another changes:

Works fine on Windows 10, can you test again on MacOS? Thanks By the way, is there anyone using Linux?

r4inee commented 5 years ago

@sloushsu So weird... I've just made two another changes:

  • Change moduleNameMapper to map nodegit to the mocked nodegit.js instead of the actual module
  • Change the target config in tsconfig.ts from es5 to es6 (which should solve the "Cannot find name X" problem according to this post)

Works fine on Windows 10, can you test again on MacOS? Thanks By the way, is there anyone using Linux?

I get the error you are getting now. But the test passes.

YichenTang97 commented 5 years ago

Just adding some note about the long process (4 days with dozens of failed trails) I went through until reaching the real reason causing the problem:

  1. Searched for the exception and related issues directly (cannot find module nodegit) but there is no issue found having the same situation as ours and their solution (many of them do not have a real solution) cannot be applied. Especially, no one is related to the test suite jest where the problem occured - some examples: ex.1, ex.2, ex.3
  2. Given there is no direct answer, I changed the search focus to looking into similar problems but with different modules, and finally I found this issue, where only when running jest tests, some node modules cannot be found, but they work with electron. This is the same problem we have, so I looked deep into the suggested solutions.
  3. From the answers, a solution that rebuilds the module using electron-rebuild is given, and the reason is that electron runtime uses a different version of node, and require node modules to be rebuilt. If the rebuilt is not done properly, the problem we meet could happen.
  4. Thus, I tried using electron-rebuild to rebuild the module nodegit. However, it was a painful process, and with over a dozen different trails investigating into versions of electron, electron rebuilt, nodegit and node, and trying to solving different kinds of errors thrown during the rebuilding process, I still cannot make it rebuild successfully. Again there is no solution can be found on google to solve the errors thrown during the rebuild process, for example - ex.4 and ex.5. The rebuilding process and node module reinstallation/update process is extremely time consuming and it took me a really long time.
  5. When I was confused and don't know where to go, another issue come into my eyes where the nodegit module not found during runtime was successfully solved by configuring webpack to mark nodegit as an external module. I remembered that we are also using webpack for compiling and packaging modules so I had a look at our webpack configuration, indeed nodegit (only) was configured as an external commonjs module.
  6. Given this clue, I looked into aspects about webpack, commonjs module, jest and how are they related to each other and finally reached a solution of mocking nodegit module using the method stated in this issue. The error cannot find module nodegit is finally gone.
  7. However, this is not the end, another error of module electron not found rise as stated in the initial comment above.
  8. My first assumption is that the problem occur because we are using electron-prebuilt instead of electron, thus, I tried installing electron instead and mock electron module, both did not work.
  9. Found in some issues that if running jest using electron run time, the problem can be solved, and two helping modules jest-runner-electron and @jest-runner/electron can be used to do that. However, again they failed due to the reason stated above in the initial comment.
  10. Another method of setting environment variable using the method in this issue with the help of cross-env module was also tried but again did not work.
  11. Finally, I found this issue with a solution of mocking electron and the tests can finally run on windows.

The whole process took me 4 days, but it finally made tests possible on windows pcs which most of us use.