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

Create an example that includes tests and how to run them. #122 #130

Merged
merged 19 commits into from
Nov 30, 2016

Conversation

hmelenok
Copy link
Contributor

Please check #122

@wbamberg
Copy link

wbamberg commented Nov 3, 2016

@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.

@andymckay
Copy link

@kumar303 might be able to help.

},
"applications": {
"gecko": {
"id": "production@gemshelf.com",

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.

Copy link
Contributor Author

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy paste mistake

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please check it

Copy link
Contributor

@kumar303 kumar303 left a 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"
}
}
Copy link
Contributor

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.

Copy link
Contributor Author

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

Copy link
Contributor Author

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.
Copy link
Contributor

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.

Copy link
Contributor Author

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');
Copy link
Contributor

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')

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, changed to expect 👍

Copy link
Contributor

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) {
Copy link
Contributor

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?

Copy link
Contributor Author

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
Copy link
Contributor

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?

Copy link
Contributor Author

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

Copy link
Contributor Author

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

@kumar303 kumar303 self-assigned this Nov 4, 2016
@hmelenok
Copy link
Contributor Author

hmelenok commented Nov 5, 2016

Please check last changes:

  • removed comments from js files
  • removed jquery and empty contentscript
  • changed should to expect
    screen shot 2016-11-05 at 19 42 53

Copy link
Contributor

@kumar303 kumar303 left a 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);
Copy link
Contributor

Choose a reason for hiding this comment

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

This fails with should is undefined; it needs to use expect() instead, right?

screenshot 2016-11-07 10 59 25

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`
Copy link
Contributor

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`
Copy link
Contributor

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.

Copy link
Contributor Author

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`
Copy link
Contributor

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.

Copy link
Contributor Author

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`
Copy link
Contributor

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');
Copy link
Contributor

Choose a reason for hiding this comment

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

when this assertion fails, it only gets logged to the browser console. Can you make it show up in the mocha test output instead? For example, if you change expect(response).to.equal('pong') to expect(response).to.equal('nope') then I see this confusing timeout instead of the assertion error:

screenshot 2016-11-07 11 25 33

Copy link
Contributor Author

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

Copy link
Contributor

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.

Copy link
Contributor Author

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!

Copy link
Contributor Author

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": {
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

"background.js"
]
},
"permissions": [
Copy link
Contributor

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']);
Copy link
Contributor

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

Copy link
Contributor Author

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

Copy link
Contributor

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",
Copy link
Contributor

Choose a reason for hiding this comment

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

where is this used?

Copy link
Contributor Author

@hmelenok hmelenok Nov 8, 2016

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

Copy link
Contributor

Choose a reason for hiding this comment

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

aha, I see

@kumar303
Copy link
Contributor

kumar303 commented Nov 8, 2016

I see that you pushed some more changes (thanks!). Just let me know when it's ready for another review.

@hmelenok
Copy link
Contributor Author

Please check it again)

Copy link
Contributor

@kumar303 kumar303 left a 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")
Copy link
Contributor

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

Copy link
Contributor Author

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');
Copy link
Contributor

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 */
Copy link
Contributor

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.

Copy link
Contributor Author

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

@kumar303
Copy link
Contributor

Thanks for all the fixups!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants