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

Converted Q Unit tests to Mocha Tests #1301

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

THEBOSS0369
Copy link
Collaborator

@THEBOSS0369 THEBOSS0369 commented Jan 14, 2025

This PR Fixes #1187

Description

In this PR i have converted Q Unit tests to Mocha Tests.

Tests Converted

All the tests from unit/spec/test.js file are converted into mocha tests. It can be found in unit/spec/mocha.test.js

Note:
In this PR i haven't deleted the test.js file as once other files are approved we can continue with deleting that file

Commands to Run the test:

npm run test-unit : This command runs the unit tests for the application to ensure that individual units of code (such as functions or methods) are working as expected.

npm run test-unit-watch : This command allows us to continuously run tests while we work on our code. If we change a test file or the code it tests, Mocha will automatically re-run the tests.

npm run test-unit-coverage : This command runs the unit tests with code coverage reporting using NYC, which is a tool for generating code coverage reports.

Test

I have done all the necessary test

  1. Checked in both "Restricted" and "ServiceWorker" Everything is working fine.
  2. Unit tests npm test no issue
  3. End-to-end (e2e) tests-e2e-firefox-> All tests Passed.
  4. extension versions with production code tested
  5. Browser Test -> Microsoft Edge.

Screenshot for converted tests Passing

image

@THEBOSS0369
Copy link
Collaborator Author

These tests are falling because of the changes done in init.js file, I am fixing that right now

@Jaifroid
Copy link
Member

@THEBOSS0369 Thank you very much for this, that's great! I'll take a look as soon as I can. Looks like a great improvement. @audiodude maybe you have some comments?

@Jaifroid Jaifroid added this to the v4.2 milestone Jan 15, 2025
@Jaifroid Jaifroid added build Code relating to building, publishing, or maintaining the app task and removed enhancement labels Jan 15, 2025
@Jaifroid
Copy link
Member

I've done some basic tests by switching to the PR/branch, running npm install, and npm run test-unit and test-unit-coverage. Congratulations, this is very impressive conversion work!

Just quickly, I noticed that running these commands create a bunch of temporary files in the Repo that have not been correctly ignored (see screenshot). If the coverage files are overwritten on each coverage run, then they can remain, but you should .gitignore the coverage folder. Regarding the .nyc_output folder, it should also be .gitignore'd, but these look like files with a PID, and so I imagine will be created every run and could become a nuisance in that folder. Ignoring the folder may not be enough. Please check if there is a way to prevent them being generated, or whether they get auto-cleaned up (which would be fine).

image

@Jaifroid
Copy link
Member

I haven't yet reviewed the code itself.

@THEBOSS0369
Copy link
Collaborator Author

@Jaifroid Sir the temp files that you are seeing is not from the code instead it is due to the command npm run test-unit-coverage which is present in package.json file. The json files are the report files. The report gets updated when we re run the command and there is a change in files.
I didn't thought of that it would install the files in the main repo itself. However if you want i can delete the command so that these files wouldn't get downloaded and once this pr passes all the ticks we can add the command in the docs so if anyone wants report they can directly copy paste the command. Let me know what you think

This is a blog for a reference https://zinserjan.github.io/mocha-webpack/docs/guides/code-coverage.html

@Jaifroid
Copy link
Member

Yes, I was aware that these reports were from running the command, but we don't want them being accidentally pushed to the server by a dev, so the folders containing them should at least be added to .gitignore. I don't think it's necessary to remove the command to invoke the code coverage test. I noticed the coverage hadn't been fully set up, it doesn't list any files and coverage currently.

@THEBOSS0369
Copy link
Collaborator Author

Yes, I was aware that these reports were from running the command, but we don't want them being accidentally pushed to the server by a dev, so the folders containing them should at least be added to .gitignore. I don't think it's necessary to remove the command to invoke the code coverage test. I noticed the coverage hadn't been fully set up, it doesn't list any files and coverage currently.

Got it sir i will do it, and yes right now it just shows the table's heading and empty value I wanted to confirm whether this feature is needed or not so i created as an example. Once conversion work of test is approved, I will make it working.

@Jaifroid Jaifroid self-requested a review January 26, 2025 11:34
@THEBOSS0369
Copy link
Collaborator Author

Hey @Jaifroid sir let me know what to do further in this pr, when you get some time.

@Jaifroid
Copy link
Member

Jaifroid commented Feb 3, 2025

Thanks! I noticed fallback errors in the test run due to Fetch not being supported in the test environment (see screenshot):

image

To avoid this, we should add a test for 'Fetch' support in the files xzdec_wrapper.js and zstddec_wrapper.js. It is sufficient to add this in each file where we test for WebAssembly support:

if ('WebAssembly' in self && 'Fetch' in self) { (the new code is && 'Fetch' in self).

Add this specifically on these lines:

Copy link
Member

@Jaifroid Jaifroid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@THEBOSS0369 It's looking good. Just some minor suggestions to clean up the output display.

});

describe('Environment', function () {
it('Mocha Test should be properly configured', function () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's otiose to write "should" in every test. More usual would be to write simply Configure Mocha Test. If it has a tick, then it passed, if it has a red cross, it failed.

expect('test').to.equal('test');
});

it('should have loaded archive files', function () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly here, simply Load archive files...

});

describe('Utils', function () {
it('should correctly read an IEEE_754 float from 4 bytes', function () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here: Correctly read an .....

expect(float).to.equal(-118.625);
});

it('should handle upper/lower case variations', function () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here Handle upper/lower case variation

Comment on lines +76 to +81
{ input: 'téléphone', expected: 'Téléphone', description: 'The first letter should be uppercase' },
{ input: 'Paris', expected: 'paris', description: 'The first letter should be lowercase' },
{ input: 'le Couvre-chef Est sur le porte-manteaux', expected: 'Le Couvre-Chef Est Sur Le Porte-Manteaux', description: 'The first letter of every word should be uppercase' },
{ input: 'épée', expected: 'Épée', description: 'The first letter should be uppercase (with accent)' },
{ input: '$¥€"«xριστός» †¡Ἀνέστη!"', expected: '$¥€"«Xριστός» †¡ἀνέστη!"', description: 'First non-punctuation/non-currency Unicode letter should be uppercase, second (with breath mark) lowercase' },
{ input: 'Καλά Νερά Μαγνησία žižek', expected: 'ΚΑΛΆ ΝΕΡΆ ΜΑΓΝΗΣΊΑ ŽIŽEK', options: 'full', description: 'All Unicode letters should be uppercase' }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In these cases, it's OK to keep "should".

});
});

it('should remove parameters from URLs', function () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just Remove parameters from URLs

});

describe('ZIM initialization', function () {
it('should set archive as ready', function () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this and all other "should" below, just change to, e.g., Set archive as ready (etc.).

@THEBOSS0369
Copy link
Collaborator Author

Thanks! I noticed fallback errors in the test run due to Fetch not being supported in the test environment (see screenshot):

image

To avoid this, we should add a test for 'Fetch' support in the files xzdec_wrapper.js and zstddec_wrapper.js. It is sufficient to add this in each file where we test for WebAssembly support:

if ('WebAssembly' in self && 'Fetch' in self) { (the new code is && 'Fetch' in self).

Add this specifically on these lines:

@Jaifroid Thanks you for this sir , this was such a help i was trying to fix this in files zstddec_asm.js and wasm and similar types of files and was just about to ask you about this. I have fixed it now and other typos as you suggested. please take a look

@THEBOSS0369
Copy link
Collaborator Author

Progress status: I'm currently trying to fix npm run test-unit-coverage isse. Its taking longer than i expected it to plus my winter's holiday has ended 2 weeks ago so i am not getting enough time to focus only on fixing it but i will try to fix it till this weekend, if i ain't able to make it, i will probably won't add the feature in this pr. would that be fine sir? @Jaifroid

@Jaifroid
Copy link
Member

Jaifroid commented Feb 5, 2025

Progress status: I'm currently trying to fix npm run test-unit-coverage isse. Its taking longer than i expected it to plus my winter's holiday has ended 2 weeks ago so i am not getting enough time to focus only on fixing it but i will try to fix it till this weekend, if i ain't able to make it, i will probably won't add the feature in this pr. would that be fine sir? @Jaifroid

Thanks for the update. See what you can do this weekend, and if it's too complicated, maybe you could just make it into a separate issue to "Add Unit test coverage".

Signed-off-by: THEBOSS0369 <[email protected]>
@THEBOSS0369
Copy link
Collaborator Author

Hello @Jaifroid sir i have fixed it but its creating so many problems in other workflow of the repo's.

First is that the main test are failing if i add the functionality it shows error message in files like urlutils.js , settingstore.js... and when i fix them it disturb the main workflow
second is the loaders which is used for testing will most probably be removed in future which will most probably disturb the workflow of repo, and when i use the instead option showing in below ss, it does not suite with other things.

example is this
image

coverage report are showing but with error messages
image

I am not very confident with this so i am just commiting the code to show you. I am just confirming again that i should remove this functionality from this pr and go ahead with the creating an issue or should i work on it more.

Its just keep getting complicated & more complicated and i don't want to mess with the codebase. Let me know what to do thanks.

@THEBOSS0369
Copy link
Collaborator Author

THEBOSS0369 commented Feb 8, 2025

I will revert the recent commit once you have taken a look

@@ -0,0 +1,26 @@
const { createInstrumenter } = require('istanbul-lib-instrument');
const { resolve } = require('path');
const { readFileSync } = require('fs');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@THEBOSS0369 We're not using CommonJS modules. You should only use the ES Module import statements.

.babelrc Outdated
@@ -0,0 +1,4 @@
{
"presets": ["@babel/preset-env"],
"plugins": ["@babel/plugin-transform-modules-commonjs"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@THEBOSS0369 Don't transform modules to CJS format, that will lead to loads of issues.

@Jaifroid
Copy link
Member

Jaifroid commented Feb 8, 2025

@THEBOSS0369 I didn't review the whole of the latest commit, but I immediately noticed you're using the wrong type of modules. We use ES6 Modules, with import statements, not CommonJS modules with require statements. If ever you are copying code from Stack Exchange that has require in it, then it won't work with our project. It took a lot of work to get rid of the old moduling system and modernize it to ES6!

Overall, my view is that you should revert the code coverage commit and make it into a separate issue. It's enough that you've converted our ancient tests to a modern format. We know the coverage is extremely poor currently, and we need to work on adding more tests for basic units of code. Being told on each run that our coverage is poor won't really help the situation, even though it's a good idea in principle! Many thanks!

@THEBOSS0369
Copy link
Collaborator Author

@THEBOSS0369 I didn't review the whole of the latest commit, but I immediately noticed you're using the wrong type of modules. We use ES6 Modules, with import statements, not CommonJS modules with require statements. If ever you are copying code from Stack Exchange that has require in it, then it won't work with our project. It took a lot of work to get rid of the old moduling system and modernize it to ES6!

Overall, my view is that you should revert the code coverage commit and make it into a separate issue. It's enough that you've converted our ancient tests to a modern format. We know the coverage is extremely poor currently, and we need to work on adding more tests for basic units of code. Being told on each run that our coverage is poor won't really help the situation, even though it's a good idea in principle! Many thanks!

Got it sir i have reverted the changes and yess i have been searching out the internet to make it work from last 2 weeks as i have never worked on something like this before 😅🫠.
I think the work is done now so do i need to do anything else on this pr? if not then i will move to different issue.

@Jaifroid
Copy link
Member

Jaifroid commented Feb 9, 2025

Thanks, @THEBOSS0369 🙏. I shall review ASAP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
build Code relating to building, publishing, or maintaining the app task tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Convert QUnit tests to mocha
2 participants