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

Explore unit Testing using Mocha #375

Closed
sophieeng opened this issue Oct 12, 2022 · 7 comments
Closed

Explore unit Testing using Mocha #375

sophieeng opened this issue Oct 12, 2022 · 7 comments
Assignees
Labels
testing Accuracy, performance, or other testing issue

Comments

@sophieeng
Copy link
Member

This is a separate branch for my Mocha tests as to not cross over the different testing frameworks. Mocha was the most popular Javascript testing framework before Jest. Using Mocha, I'm interested to see if my issues in Jest carry over, which will be a sign as to whether the extension or the testing frameworks are causing the errors I'm getting.

@sophieeng sophieeng added the long-term An issue that will be monitored but does not need immediate work label Oct 12, 2022
@sophieeng sophieeng self-assigned this Oct 12, 2022
@sophieeng sophieeng added the testing Accuracy, performance, or other testing issue label Oct 12, 2022
@SebastianZimmeck SebastianZimmeck removed the long-term An issue that will be monitored but does not need immediate work label Oct 18, 2022
@SebastianZimmeck SebastianZimmeck changed the title Unit Testing using Mocha Explore unit Testing using Mocha Oct 18, 2022
@SebastianZimmeck
Copy link
Member

As we discussed, @sophieeng will spend another three weeks or so on this issue and #365.

sophieeng added a commit that referenced this issue Nov 1, 2022
@sophieeng
Copy link
Member Author

sophieeng commented Nov 1, 2022

Steps to use Mocha:

  1. In terminal, npm install --save mocha chai
  2. Add "test": "mocha" in pacakge.json scripts.

Notes:

  • Had to change export function() to module.exports = function() in cookiesIAB.js, wondering if this will solve the Jest issue. Also need to check if this broke any functionality
  • Next step would be to create tests for async functions and functions from files that already have tests

@sophieeng
Copy link
Member Author

There are some large issues, which I believe will apply to all testing frameworks. As I mentioned above, I changed export function() to module.exports = function(). I then tested the extension and realized it was broken. Looking into it, I realized I changed the formatting of the js file from a ESM format to a CommonJS format. I believe all our modules are of the ESM format, though I did find a dependency in our codebase, babel/plugin-transform-modules-commonjs, that is apparently supposed to transform ECMAscript (ie ESM) to CommonJS, but I'm not sure what that actually transforms.

Testing frameworks don't seem to be very easily used with ESM. I am currently looking into configuring Mocha, using babel most likely, like this. Seems like a problem that has a large scope and we should consider abandoning testing if I can't get it to work soon, like Sebastian talked about only spending 3 more weeks on this.

@SebastianZimmeck
Copy link
Member

@sophieeng, maybe use a different test framework? Can you check whether the following work (in that order):

  1. Vitest ("Out-of-box ESM, TypeScript and JSX support powered by esbuild")
  2. AVA
  3. uvu
  4. Jasmine

I got these from a Stackoverflow post. The post also mentions that Mocha supports ESM but with some limitations. So, maybe using that we can also keep Mocha.

Whatever works better.

@sophieeng
Copy link
Member Author

The most recent is a working Mocha test, without modifying the code of the extension, with the exception of package.json. Therefore, Mocha does work with ESM but I need to look into the limitations and if this part of Mocha is still being up-kept.

The issue is that the webpack.config.js is written in CommonJS, so when I had to modify package.json to "type: module" it will not work with the formatting of CommonJS so the extension won't load and I can't test the functionality of the extension.

@SebastianZimmeck
Copy link
Member

In our meeting today, @sophieeng clarified the two tasks to perform:

  1. Build process: Our build process with webpack is the major obstacle of a pure ECMAscript solution for our extension. Our extension is based on ECMAscript except for webpack, which makes use of CommonJS. We would like to continue using ECMAscript as that is the future. Going back to CommonJS does not make much sense. So, the questions is whether we can change the build process with webpack such that it makes use of or is compatible with ECMAscript. If that is not possible, are there other build tools that we can use instead of webpack to move towards ECMAscript?

  2. Unit test framework: Assuming we are able to create a pure ECMAscript extension with related build process, what unit test framework can we use? As @sophieeng mentioned, we not all unit testing frameworks offer (yet) support for ECMAscript. Can we find one that does (at least, for our core functionality)?

@sophieeng
Copy link
Member Author

Example tests have been successfully merged and work correctly. Closing issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
testing Accuracy, performance, or other testing issue
Projects
None yet
Development

No branches or pull requests

2 participants