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

jest-matchers#addMatchers #1276

Merged
merged 1 commit into from
Jul 14, 2016

Conversation

aaronabramov
Copy link
Contributor

No description provided.

@ghost ghost added the CLA Signed ✔️ label Jul 9, 2016

const matchers = require('./matchers');
const GLOBAL_MATCHERS_OBJECT_KEY = '_JEST_MATCHERS';
global[GLOBAL_MATCHERS_OBJECT_KEY] = {};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this will not work until #1275 is merged.
because right now jest-matchers is executed in outside environment

Copy link
Member

Choose a reason for hiding this comment

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

I don't understand why we need this global object? Can it not just be a module-local variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i've had a few issues with closure variables before and try to avoid it now.
a few advantages are:

  1. Better semantics. because it's global it is stored on global objects
  2. No weirdness when resetting the module cache and using custom require functions
  3. Accessible from the outside if ever needs to be read/modified/debugged

Copy link
Member

Choose a reason for hiding this comment

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

Can you explain how you are hoping to use addMatchers in Jest? I'm quite a bit against adding new globals to Jest.

I'd prefer to re-use jest.addMatchers by making it compatible with both Jasmine2 and the new matcher system. We don't use jest.addMatchers at FB, so it is a good chance to start using it. It's ok if we are breaking the API a bit, all your matchers work requires us to go to 14.0 soon anyway and this can easily be codemodded.

Regarding the global, I am also hesitant on storing it on global. Number 3 is a strict downside. I don't want anyone to modify or read this and opening it up to this will just make someone frown in five years when they have to clean up a gigantic mess at FB because some people started abusing this. I think your #2 point is valid, however I first need to see how addMatchers is being used. If it's added from the outside context in (through jest.addMatchers), the module registry will not be an issue.

Finally, if we do decide this must absolutely be global for some reason, then I would like us to use

Object.defineProperty(global, Symbol.for('$$jest-add-matchers'), {
  // these are all defaults:
  // configurable: false,
  // enumerable: false,
  // writable: false,
  value: {}
})

so that it is unlikely people will mess with 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.

my thoughts were to use jest.addMatchers({toBeSomething: () => {...}}); yeah.

then in jest-jasmine-adapter we define a jasmine.addMatchers = (obj) => {jest.addAddmatchers(obj)});` function to make it compatible with jasmine too. (or making jasmine compatible with jest)

so no direct accessing of the global property.

I really like the idea of using a symbol though

@aaronabramov aaronabramov force-pushed the jest-matchers-add-matchers branch from 4a8b525 to 382ff25 Compare July 12, 2016 20:04

function expect(actual: any) {
if (!global[GLOBAL_MATCHERS_OBJECT_SYMBOL]) {
Object.defineProperty(global, GLOBAL_MATCHERS_OBJECT_SYMBOL, {value: {}});
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@cpojer how about that?
we can always do a closure var too, it's not really a big difference :)

Copy link
Member

Choose a reason for hiding this comment

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

It should use Object.create(null) as value. We don't want the prototype chain as part of the matchers.

I can live with this, even though I'd prefer it not to be global but I take the module registry reset argument. I'm happy to change this later when we inject jest.addMatchers properly.

@ghost ghost added the CLA Signed ✔️ label Jul 12, 2016
@aaronabramov aaronabramov force-pushed the jest-matchers-add-matchers branch from 382ff25 to 52d6ac9 Compare July 12, 2016 22:12
@ghost ghost added the CLA Signed ✔️ label Jul 13, 2016
@aaronabramov aaronabramov force-pushed the jest-matchers-add-matchers branch from 52d6ac9 to 1f976d3 Compare July 13, 2016 22:56
@ghost ghost added the CLA Signed ✔️ label Jul 14, 2016
@aaronabramov aaronabramov merged commit 8e4a658 into jestjs:master Jul 14, 2016
@aaronabramov aaronabramov deleted the jest-matchers-add-matchers branch July 14, 2016 16:13
@aaronabramov aaronabramov restored the jest-matchers-add-matchers branch July 14, 2016 16:13
@aaronabramov aaronabramov deleted the jest-matchers-add-matchers branch July 14, 2016 16:13
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants