-
Notifications
You must be signed in to change notification settings - Fork 81
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
Proposal for integration/unit testing #36
Comments
My 2 cents:
Same. |
Yes, I'm interested in this, thanks for starting this. Now, this is all new to me, so I need to find my way into all this. I git cloned your repo, and first thing is, how do I launch tests?
I use right- I saw that the build script needs |
First install Node.js: curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs Then install the 591 required dependencies:
Then execute the test script:
That doesn't do anything on mine, I think that doesn't work for most people. |
I could create an alias of |
@jspenguin2017 I've used Puppeteer, it's great. I would consider it's best use case for more "end-to-end" tests. I've found it easier to have sort of a blank slate and mimic certain browser features. I will commit some more tests with further examples of mocking out the chrome object. Mocking it out is not perfect because you're hoping the apis do what they say they do, and then you get to the real world with a quirky api that doesn't quite work as you thought. Just curious, can you think of an example where mocking the chrome object was an issue? Here's my thought process as I was trying to figure out what this commit was fixing. Since it's just modifying the regex, it's not really necessary to run that in a full chrome environment, I'd rather isolate the specific function
and then fired up the tests with
With the original regex, that printed out something like:
Okay now I see what was trying to be filtered out. So I picked a few that shouldn't be there, replaced the regex, and it passed. Well, in the middle there I got lazy and I didn't want to have 6 of the same Now sure, I could've just studied the regex for a bit, but I'm not that smart and would rather see it in action. I found writing tests for someone else's code is the best way for me to learn the codebase. This is what I did for a project called chromeless, which is sort of a "competitor" to Puppeteer.
And then I usually add a npm script to run jest in watch mode, which just re-runs the tests as files change.
Let me know if you run into issues.
Yes, it's just preference of directory structure. Another popular structure would be to put all the tests in a directory:
Then you'd just need to change the Jest comes preconfigured to handle both structures out of the box. That line above in I have about 10 other files from Also, here is one of the better articles I've read on testing philosophy/strategy: http://blog.codepipes.com/testing/software-testing-antipatterns.html Edit: Sorry -- a bit long. |
@geoffdutton There's nothing wrong with your unit tests, but mocking so many APIs will eventually result in this: There need to be integration tests, running in a real browser on artificial (and maybe real) sites. I use Puppeteer because it can load extensions and (used to) be able to auto-config them, I'm not sure if The repo is here: https://github.com/NanoAdblocker/NanoTest |
@jspenguin2017 I see what you're saying now. Yes, you are right on the need to test the full product in a real browser in an automated way. Puppeteer would be great for that, but I think lower level unit tests can be complimentary. For example, looking at this, I would need to do a lot of set up to make sure incrementing by 2 is what I really want. But now say I wanted to test the correct tab was actually reloaded when the refresh button is clicked in your screen shot, that would be a disaster to test using something like jest. You'd have to mock out the entire chrome messaging system and such. Another great use case for Puppeteer is "visual regression" sort of testing such as testing css media queries on various view port sizes. I will take a look at your repo and see if I have any ideas. |
@geoffdutton My worry is that uBO isn't really built on functional programming; one functionality can span across multiple files. Ideally you want many small pure functions and a few big controllers to tie them together. This way you can optimize smaller functions without needing to worry about breaking stuff. But now it's too late to change, so we have to instead design tests around it. I checked your tests, I have to say they are not ideal. I don't have a better way but ideally you would run a function multiple times instead of Also for code like this, it just feel really fragile and hacky: chrome.runtime.getManifest.mockReturnValue({ version: '1.1.1.1' }); Ideally we don't want to mock browser APIs, instead, we should mock |
I guess I'd say that's why having unit tests in place to prove the "contract" of the functionality.
Well the only thing we care about is that it's 4 digits, but I can see what you're saying. Edit: to your point testing a file like this would be really fragile because there’s all the internal stuff going on that’s not exposed https://github.com/gorhill/uBlock/blob/master/src/js/assets.js |
On Windows, Alt + 2+3+0 on numpad types |
|
Reason for close? @gorhill not wanting unit tests or just that this particular issue is not needed anymore? |
@gorhill I'm not sure if this is something you're interested in, but here is my proposal for unit testing: https://github.com/geoffdutton/uBlock/tree/unit-tests-proposal
Here is the current diff: https://github.com/gorhill/uBlock/compare/master...geoffdutton:unit-tests-proposal?expand=1
I've written a few test cases, and would be happy to contribute more. I think it can help for two main reasons:
Number 2 is especially nice, such as when parsing new filter syntaxes or running extreme scenarios.
I'd be happy to go into more detail on how to write tests, when to mock functions, how to mimic browser features, etc. I chose
jest
because it's fast, and you can mimic any browser like here.Let me know if this is something you're interested in incorporating.
The text was updated successfully, but these errors were encountered: