-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Create an example that includes tests and how to run them. #122 #130
Conversation
* icons with mocha * initial web-extension * dependencies and npm configs
@hmelenok , nice work! @andymckay , do you know who might be qualified to review this? There's a lot of stuff in here I don't know, so while I could probably figure out whether it's working properly, I probably won't be able to tell whether it's good practice. |
@kumar303 might be able to help. |
}, | ||
"applications": { | ||
"gecko": { | ||
"id": "production@gemshelf.com", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We've been trying to remove the id out of example extensions, they can be through temporary install locally or pushed to AMO for signing. If there's no id, they won't all conflict with each other.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh sorry will remove today
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copy paste mistake
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please check it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for getting this started! I requested some changes and also asked some questions to help me understand it better.
"mocha": "^3.1.2", | ||
"should": "^11.1.1" | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is there a separate package.json
inside the addon dir? I was able to run the add-on and also run the tests with npm run test
without installing any packages within the addon dir. If it's not necessary we shouldn't include it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There two type of tests inside of addon i had add mocha client tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Outside of addon folder there phantomjs tests
@@ -0,0 +1,16 @@ | |||
/** | |||
* Created by hmelenok on 10/30/16. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like your editor (or something) added these automatically. Can you remove them all? They don't appear to be relevant to the example so they might be distracting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done removed
describe('ping', function() { | ||
it('should return pong in response', function() { | ||
Background.ping(false, false, function(response) { | ||
response.should.equal('pong'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
chai's prototype hack might be confusing to the casual JavaScript reader. Could you update this to use expect() (or something) instead? It would look like expect(response).to.equal('pong')
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, changed to expect 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see the change to make this expect()
. Do you have any unpushed commits maybe?
*/ | ||
describe('Background', function() { | ||
describe('ping', function() { | ||
it('should return pong in response', function(done) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how do I run this test? It isn't executed with npm run test
from the root directory. Is there a different way to run it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
npm run test
- runs phantomjs tests, there UI ob mocha which included as example if you install all dependencies, please read README.md before
##PhantomJs tests | ||
`npm run test` will run simple test `./addon/background.js` | ||
## Mocha browser test | ||
Already inside of addon |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean by this? I couldn't figure out how to run the tests in addon/tests/lib
. How do I run them and see results?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cd mocha-client-tests/addon && npm i
- it will install all node modules which required for running tests in addon window
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then just run web-ext run -s ./addon
with cli
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for fixing up the mocha test result view. I understand the two test suites better now. I ran into a few problems that need fixing.
describe('Array', function() { | ||
describe('#indexOf()', function() { | ||
it('should return -1 when the value is not present', function() { | ||
[1,2,3].indexOf(5).should.equal(-1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And inside of addon please run `npm i` | ||
|
||
##Run with web-ext cli | ||
If you have it already run `npm run web-ext` if not `npm i web-ext -g` and then first command (will work with FF dev edition), if you have error with web-ext cli please add path for FF binary file with `--firefox-binary /path/to/firefox-bin` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
npm run web-ext
doesn't work because package.json
wants it to be npm run web-ext-run
. I think you should rename the script
in package.json
to web-ext
.
And inside of addon please run `npm i` | ||
|
||
##Run with web-ext cli | ||
If you have it already run `npm run web-ext` if not `npm i web-ext -g` and then first command (will work with FF dev edition), if you have error with web-ext cli please add path for FF binary file with `--firefox-binary /path/to/firefox-bin` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
npm install -g web-ext
is not necessary because you've already put it in the devDependencies
. When you do npm run
it figures out the path to web-ext
automatically.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry you right
##Install dependency | ||
`npm install` and `bower install` | ||
|
||
And inside of addon please run `npm i` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be more helpful to spell out npm install
in all cases since not all readers will know about the i
alias.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
And inside of addon please run `npm i` | ||
|
||
##Run with web-ext cli | ||
If you have it already run `npm run web-ext` if not `npm i web-ext -g` and then first command (will work with FF dev edition), if you have error with web-ext cli please add path for FF binary file with `--firefox-binary /path/to/firefox-bin` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These instructions need a final sentence explaining that you need to click the mocha icon from the browser bar to run the tests
describe('ping', function() { | ||
it('should return pong in response', function(done) { | ||
chrome.runtime.sendMessage({action: 'ping'},function(response) { | ||
expect(response).to.equal('pong'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mochajs/mocha#1128 - looks like I'm not sure why it wait until fail
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, I see what the issue is now. The expect()
call throws an error but that is not passed to done(error)
so the mocha reporter can't show it. The way to fix this is to rewrite the code using promises:
it('should return pong in response', function() {
// Return a promise for Mocha using the Firefox browser API instead of chrome.
return browser.runtime.sendMessage({action: 'ping'})
.then(function(response) {
expect(response).to.equal('pong');
});
});
I think it would be good to convert this code into promises since the developer can always use a polyfill to make it compatible with Chrome.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for advice it works!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added in last commit
"default_title": "Mocha Test", | ||
"default_popup": "popup.html" | ||
}, | ||
"applications": { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove this section entirely. It's not needed for this example. It could be harmful if someone copy/pasted it because it makes the extension only compatible with Firefox, not Chrome.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
"background.js" | ||
] | ||
}, | ||
"permissions": [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like it has a bunch of permissions not needed for this example. Can you strip it down to only the relevant permissions? I think only notifications
is needed, right?
@@ -0,0 +1,3 @@ | |||
mocha.checkLeaks(); | |||
mocha.globals(['jQuery','AppView', 'ExtensionOptions', 'ExtensionView', 'WebView']); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
where are these globals used? These don't look like they are relevant to the example
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There a list of default globals in extension which from Mocha perspective looks like global leaks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, can you add a comment above this line of code explaining that?
"license": "MPL-2.0", | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"chrome-mock": "0.0.9", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
where is this used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
chrome-mock used for phantomJS as it not have WebExtensions (or Chrome Extensions) API inside of engine, so i'm using mock of this API's.
When you include to test runner original file(at my point background.js) it throws errors about chrome is not defined
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aha, I see
I see that you pushed some more changes (thanks!). Just let me know when it's ready for another review. |
Please check it again) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delay in getting back to you. This is really close.
|
||
When addon will start click on mocha icon in your browser bar to run client tests: | ||
|
||
![addon screenshot](screenshots/addon-button.png "Mocha test addon") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good idea to add a screenshot
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
describe('ping', function() { | ||
it('should return pong in response', function() { | ||
Background.ping(false, false, function(response) { | ||
response.should.equal('pong'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see the change to make this expect()
. Do you have any unpushed commits maybe?
@@ -0,0 +1,849 @@ | |||
/* webextension-polyfill - v0.1.0 - Wed Nov 09 2016 16:37:59 */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the built dist file, right? I don't know what to look for.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That what i get after build in webextension-polyfill
Thanks for all the fixups! |
Please check #122