Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Mocha/Chai Promise Adapter -- Proof of Concept #47

Closed

Conversation

jamestalmage
Copy link

Fixes #35

I put this together fairly quickly, but I wanted to run it by the protractor team before I invested more time. It makes use of my promise-testing library to provide a Chai.js adapter for expectations on webdriver promises, very similar to the existing Jasmine implementation.

All the relevant files are in the mochachaiwd directory. onMocha.js and index.js are modified copies of files with the same name elsewhere in the project. I recommend using a file comparison tool to see what I actually changed (it's really only a few lines).

After cloning, switch to the mocha-proof-of-concept branch and then:

npm install
./bin/install_selenium_standalone
./bin/start_selenium_standalone

Once selenium is up and running, then:

npm run-script testMocha

This is not ready for primetime yet. To get there it needs:

  • Integration with the cli (some way for the cli to infer mocha vs jasmine)
  • Appropriate configuration options in the conf file
  • mochachaiwd/index.js is a hacked copy of jasminewd/index.js. There is plenty of duplicate code that could easily be eliminated.
  • promise-testing doesn't account for promises as expectations (expect(promiseA).to.equal(promiseB)). This will be harder to patch in Chai than it was Jasmine, since Chai allows such long promise chains (expect({a:10}).property('a').to.be.at.least(6)). I will add the feature there (possibly include a hook for customizing how promises are detected - which protractor could use to detect webdriver promises).

Thanks for for a great tool guys!

@@ -0,0 +1,104 @@
/**
* Adapts Jasmine-Node tests to work better with WebDriverJS. Borrows
Copy link
Member

Choose a reason for hiding this comment

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

Update comment :)

@juliemr
Copy link
Member

juliemr commented Aug 27, 2013

Cool! Nice start. Sorry for the slow response on this!

I think the biggest issue here will be to figure out how to reuse the mocha support that WebdriverJS already has build in - check out https://code.google.com/p/selenium/source/browse/javascript/node/selenium-webdriver/testing/index.js

That doesn't have any of the chai/promise support, but it does take care of wrapping the global functions.

@jamestalmage
Copy link
Author

@juliemr
The new commit addresses your line comments.

I looked at WebdriverJS code you linked to. They take the approach of not clobbering mochas global functions, and instead expect you to require a module that exports wrapped versions of those global functions.

Rather than this:

describe('My Module', function () {
  it('does something', function () {
    //expectations
  }
});

Code using their adapter looks like this:

var test = require('selenium-webdriver/testing');

test.describe('My Module', function() {
  test.it('does something', function() {
    //expectations
  }
});

If we want to overwrite the global functions, my adapter can be reduced to a series of statements like this:

var test = require('selenium-webdriver/testing');
global.describe = test.describe;
global.it = test.it;
//.... etc

As for expectations, I am going to continue working on the chai promise adapter to get it where I think it needs to be. I will keep you posted.

@teddyhwang
Copy link

Bump. Has there been any progress on this? Would love to see Mocha and Chai better integrated with Protractor.

@cyates81
Copy link

+1. Chai expect library is much better than jasmine's IMO. I want to be able to do expect(promise).to.have.property('foo', 'bar') where I can't seem to do this in jasmine without writing custom matchers.

@juliemr
Copy link
Member

juliemr commented Dec 27, 2013

See 478c00a

What's the status of the promise adapters for chai? Could we make them work with the new commit?

@juliemr
Copy link
Member

juliemr commented Dec 27, 2013

Also found this thing: http://chaijs.com/plugins/chai-as-promised

@jamestalmage
Copy link
Author

chai-as-promised was recently rewritten to address my one major issue with it (it broke integration with many other chai plugins). I haven't used the new version yet, but it's certainly worth checking out.

@juliemr
Copy link
Member

juliemr commented Dec 28, 2013

Chai As Promised seems to work, but I didn't try with any other plugins. Check out https://github.com/angular/protractor/blob/master/docs/using-mocha.md and the test it links to.

I think that takes care of everything from this PR - but let me know if there's any lingering issues or missing features!

@jamestalmage
Copy link
Author

Looks good to me. I'm closing the issue for now.

@jamestalmage jamestalmage deleted the mocha-proof-of-concept branch December 28, 2013 01:00
@jamestalmage jamestalmage restored the mocha-proof-of-concept branch December 28, 2013 01:00
heathkit added a commit to heathkit/protractor that referenced this pull request Jan 11, 2017
heathkit added a commit to heathkit/protractor that referenced this pull request Jan 18, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feature request: mocha adapter support
4 participants