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

toThrowError with <iframe> reports incorrectly #1252

Closed
anseki opened this issue Jan 12, 2017 · 2 comments
Closed

toThrowError with <iframe> reports incorrectly #1252

anseki opened this issue Jan 12, 2017 · 2 comments

Comments

@anseki
Copy link
Contributor

anseki commented Jan 12, 2017

  • jasmine-standalone-2.5.2 in browser

Hi, thank you for the great framework.

When an app that runs in <iframe> is tested, toThrowError reports incorrectly. It says "Expected function to throw an Error, but it threw...".
Jasmine uses Error constructor in current window, but it was made by Error constructor in child window.

In this code:

if (!(thrown instanceof Error)) {

if (!(thrown instanceof Error)) {

It checks with the Error in current window always. But the thrown was made in different global (namespace).

For example:

  • app.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">

  <script>

    function methodA() {
      return 'foo';
    }

    function methodB() {
      throw new Error('bar');
    }

  </script>

</head>

<body>
  app.html
</body>
</html>
  • Spec
describe('App running in <iframe>', function() {

  function methodC() {
    throw new Error('baz');
  }

  var childWindow;

  beforeAll(function(done) {
    // Prepare <iframe>
    var iframe = document.body.insertBefore(document.createElement('iframe'), document.body.firstChild)
    iframe.addEventListener('load', function() {
      childWindow = iframe.contentWindow;
      // self.Error = childWindow.Error;
      done();
    });
    iframe.src = 'app.html';
  });

  it('should return foo', function() {
    expect(childWindow.methodA()).toBe('foo');
  });

  it('should throw an error', function() {
    expect(function() {
      childWindow.methodB();
    }).toThrowError('bar');
  });

  it('(test in current window)', function() {
    expect(function() {
      methodC();
    }).toThrowError('baz');
  });
});

The second spec fails.

I think, this line:

if (!(thrown instanceof Error)) {

should be:

if (!(thrown instanceof (thrown.constructor.constructor('return this')()).Error)) {

This gets Error from the same global as thrown.

@slackersoft
Copy link
Member

We would be happy to review a pull request that allows toThrowError to correctly detect Errors that come from different frames correctly.

@anseki
Copy link
Contributor Author

anseki commented Feb 10, 2017

@slackersoft,
Ok, I will send a PR later.

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

No branches or pull requests

2 participants