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

Runtime error on Angular 13.1.0 #519

Closed
ChaseOxide opened this issue Dec 11, 2021 · 5 comments · Fixed by #522
Closed

Runtime error on Angular 13.1.0 #519

ChaseOxide opened this issue Dec 11, 2021 · 5 comments · Fixed by #522

Comments

@ChaseOxide
Copy link

Is this a regression?

No

Description

I've just upgraded from Angular 13.0.3 to 13.1.0 and started receiving this error.
I've tried starting a new Angular project with ng new, get spectator installed and have @ngneat/spectator imported in the spec file. The test runs without runtime error until I have it imported

Please provide a link to a minimal reproduction of the bug

https://github.com/ChaseOxide/spectator-crash

Please provide the exception or error you saw

An error was thrown in afterAll
  Uncaught ReferenceError: global is not defined
  ReferenceError: global is not defined
      at Object.74757 (http://localhost:9876/_karma_webpack_/webpack:/node_modules/pretty-format/build/plugins/AsymmetricMatcher.js:10:1)

Please provide the environment you discovered this bug in

Angular 13.1.0
Spectator 9.0.0
Jasmine 3.10.0

Anything else?

Output from npm ls pretty-format:

`-- @ngneat/spectator@9.0.0
  `-- @testing-library/dom@7.26.5
    `-- pretty-format@26.6.2 

Do you want to create a pull request?

No

@molily
Copy link

molily commented Dec 15, 2021

Bumping the @testing-library/dom dependency to 8.x should fix this. See testing-library/dom-testing-library#756

I quickly tried this and had to fix some typing errors in dom-selectors.ts. The Matcher interface has changed, it’s now

export type MatcherFunction = (
  content: string,
  element: Element | null,
) => boolean
export type Matcher = MatcherFunction | RegExp | number | string

This is how the byTextContent function could look:

export const byTextContent = (matcher: Matcher, options: MandatorySelectorMatchingOptions): DOMSelector => {
  let textContentMatcher: Matcher;
  const normalizer: NormalizerFn = options?.normalizer || getDefaultNormalizer(options);
  const getTextContent = (elem: Element): string => normalizer(elem.textContent ?? '');

  if (typeof matcher === 'string' || typeof matcher === 'number') {
    textContentMatcher = (_, elem) => {
      if (elem === null) {
        return false;
      }
      if (options?.exact === false) {
        return (
          getTextContent(elem)
            .toLowerCase()
            .indexOf(matcher.toString().toLowerCase()) >= 0
        );
      }

      return getTextContent(elem) === matcher;
    };
  } else if (matcher instanceof RegExp) {
    textContentMatcher = (_, elem) => {
      if (elem === null) {
        return false;
      }
      return matcher.test(getTextContent(elem))
    };
  } else {
    textContentMatcher = (_, elem) => {
      if (elem === null) {
        return false;
      }
      return matcher(getTextContent(elem), elem);
    }
  }

  return new DOMSelector(el => DOMQueries.queryAllByText(el, textContentMatcher, options));
};

Then the tests pass with "@testing-library/dom": "8.11.1". (Don’t know if there are other reason against 8.x.)

@NetanelBasal
Copy link
Member

@molily do you want to create a PR?

@johncrim
Copy link
Contributor

johncrim commented Dec 19, 2021

I'm seeing the same problem. I would say that this is a regression (it starts occurring after updating to ng 13 and spectator 9), but not a regression due to changes in this lib. As described in @testing-library #756, it's due to this line in pretty-format:

AsymmetricMatcher.js line 10:

var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
const asymmetricMatcher =
  typeof Symbol === 'function' && Symbol.for
    ? Symbol.for('jest.asymmetricMatcher')
    : 0x1357a5;
const SPACE = ' ';

pretty-format@26.6.2 is referenced by @testing-library/dom@7.26.5 which is referenced by @ngneat/spectator@9.0.0

I'm not sure whether this was an issue before ng 13 with spectator 9, since I updated everything together.

I'll create a PR ASAP.

johncrim added a commit to johncrim/spectator that referenced this issue Dec 19, 2021
API change while updating to new @testing-library/dom

fixes: ngneat#519
johncrim added a commit to johncrim/spectator that referenced this issue Dec 22, 2021
fixes: ngneat#519

BREAKING CHANGE: This change will result in a compile error for any `MatcherFunction` that uses `HTMLElement` properties or functions.
`MatcherFunction` in `@testing-library/dom` now receives a parameter of type `Element`,
where it previously was `HTMLElement` - so users will have to test/cast to `HTMLElement`
if they need `HTMLElement` properties or methods in their matcher function.
@sykaeh
Copy link

sykaeh commented Dec 24, 2021

Thank you to everyone involved for the fast fix!

@johncrim
Copy link
Contributor

Fixed in version 10.0.0 :

https://github.com/ngneat/spectator/blob/master/CHANGELOG.md

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 a pull request may close this issue.

5 participants