Explore Library
Code Quiz

Async Stub Test False Positive

Spot why a Mocha/Sinon test for an async Page Object method passes even when it should fail.

Codejavascript
// LoginPage.js
class LoginPage {
  constructor(driver) { this.driver = driver; }
  async submit(user, pass) {
    await this.driver.type('#user', user);
    await this.driver.type('#pass', pass);
    return this.driver.click('#login');
  }
}

// login.test.js
const sinon = require('sinon');
const { expect } = require('chai');

it('clicks the login button', () => {
  const driver = {
    type: sinon.stub().resolves(),
    click: sinon.stub().resolves()
  };
  const page = new LoginPage(driver);

  page.submit('alice', 'secret').then(() => {
    expect(driver.click.calledWith('#login')).to.be.true;
  });
});

What is the bug in this test?