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

Expose expect.extend and alias jasmine.addMatchers to it #1933

Merged
merged 4 commits into from
Oct 24, 2016

Conversation

blainekasten
Copy link
Contributor

Summary

This solves #1835.

It exposes expect.extend as the jest API for adding matchers and coerces jasmines addMatchers API.

Test plan

I've added jest-jasmine2/jasmine-uses-jests-extend-api-test.js which proves a few things:

  1. jasmine.addMatchers calls expect.extend
  2. jasmine.addMatchers properly gets the API coerced into expect.extend

You can test this by running:
npm run build && npm run jest jasmine-uses-jest

Copy link
Contributor Author

@blainekasten blainekasten left a comment

Choose a reason for hiding this comment

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

I still need to add proper flow tags to the code. I just wanted to get this up early to see if the approach was good. Looking for feedback!

it('properly alias to jests api', () => {
expect(1)._toBeValue(1);
expect(1).not._toBeValue(2);
});
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 should add a test that expect.extend properly works similar to testing jasmine.addMatchers

@@ -15,6 +15,7 @@ import type {TestResult} from 'types/TestResult';
import type Runtime from 'jest-runtime';

const JasmineReporter = require('./reporter');
const AddMatchersAdapter = require('./jasmineAddMatcherAdapter');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

need to remove this

@@ -0,0 +1,36 @@
/**
Copy link
Contributor Author

Choose a reason for hiding this comment

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

file should be renamed to jasmineAddMatchersAdapter.js


module.exports = {
addMatchers,
addMatchers: expect.extend, // TODO: remove!
Copy link
Contributor Author

Choose a reason for hiding this comment

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

TODO: My goal is to remove this export. Does that sound good? I just need to go throw and update any code that depends on it.

Copy link
Member

Choose a reason for hiding this comment

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

yep, sounds good.

@@ -10,9 +10,7 @@

'use strict';
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 rename this file as well? "extend-test.js"?

test('throws the error if tested function did not throw error', () => {
expect(() => {}).toThrowErrorMatchingSnapshot();
});

Copy link
Member

Choose a reason for hiding this comment

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

I'm not really show where this test is coming from and why it is here?


jasmine.addMatchers({});

expect(expect.extend).toBeCalled();
Copy link
Member

Choose a reason for hiding this comment

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

if this test fails by throwing, we won't reset expect.extend and we'll be in an odd state. Can you do afterAll(() => expect.extend = originalExtend)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

expect.extend = originalExtend;
});

it('properly alias to jests api', () => {
Copy link
Member

Choose a reason for hiding this comment

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

"Jest's".

@@ -18,11 +17,15 @@ const jasmineExpect = global.expect;

// extend jasmine matchers with `jest-matchers`
module.exports = () => {
addMatchers({toMatchSnapshot, toThrowErrorMatchingSnapshot});
jestExpect.extend({toMatchSnapshot, toThrowErrorMatchingSnapshot});

global.expect = actual => {
const jasmineMatchers = jasmineExpect(actual);
const jestMatchers = jestExpect(actual);
const not = Object.assign(jasmineMatchers.not, jestMatchers.not);
return Object.assign(jasmineMatchers, jestMatchers, {not});
};
Copy link
Member

Choose a reason for hiding this comment

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

I think with your change we should be able to kill this and replace it with:

global.expect = jestExpect;

because we won't need Jasmine's expect at all any longer.

global.expect = actual => {
const jasmineMatchers = jasmineExpect(actual);
const jestMatchers = jestExpect(actual);
const not = Object.assign(jasmineMatchers.not, jestMatchers.not);
return Object.assign(jasmineMatchers, jestMatchers, {not});
};

// Expose addMatchers API
global.expect.extend = jestExpect.extend;
Copy link
Member

Choose a reason for hiding this comment

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

with the change mentioned above, we won't need to do this either :)

@cpojer
Copy link
Member

cpojer commented Oct 20, 2016

This does look good. Would you mind polishing it up so we can ship it? :)

cc @DmitriiAbramov

@aaronabramov
Copy link
Contributor

this looks good! There is a few things we'll need to clean up in www before we can land it (custom equality) but i remember that it wasn't that complicated.
(sorry for the long response. long vacation :/)

@cpojer
Copy link
Member

cpojer commented Oct 21, 2016

We can land it before cleaning up our internal code. We'll just update everything at FB the next time we release a version of Jest.

We should also find a way to expose matcherHint etc. so that messages can be pretty-printed, but that can be done in a separate PR.


jasmine.addMatchers({});

expect(expect.extend).toBeCalled();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

"c": "three\`",
"d": "vier",
}
`;
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'm not sure where this came from, should I remove it?

// globaly expost `jestExpect` and overrides jasmines
module.exports = () : void => {
global.expect = jestExpect;
};
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 seems overly simple for a file, but do you think there is a different/better way to globally expose it? I figured it should happen in this package as it's the one that handles all the runtime resolutions.

@@ -86,11 +86,17 @@ function jasmine2(

env.addReporter(reporter);

// Adapter for coercing jasmine `addMatcher` api into using
// jest's `expect.extend` API.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

should this be Jest's?

@@ -398,6 +402,8 @@ const matchers: MatchersObject = {

return {message, pass};
},

toMatchSnapshot,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there a reason these matchers were added in jest-jasmine2 and not in jest-matchers ?

Copy link
Member

Choose a reason for hiding this comment

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

yeah, we didn't want jest-matchers to depend on the snapshot feature yet.

@cpojer
Copy link
Member

cpojer commented Oct 24, 2016

thanks @blainekasten. The last iteration I think you might not have run locally yourself ( :P ) but I spend some time polishing it and will merge in a moment.

@cpojer cpojer merged commit 472af8a into jestjs:master Oct 24, 2016
@blainekasten
Copy link
Contributor Author

Awesome. Thanks for carrying it across the line for me. I wasn't sure what you guys were ultimately going to want. So thanks!

okonet pushed a commit to okonet/jest that referenced this pull request Nov 11, 2016
* Expose expect.extend and alias jasmine.addMatchers to it

* Clean up code for PR

* rename files

* Cleanup + updates.
nickpresta pushed a commit to nickpresta/jest that referenced this pull request Nov 15, 2016
* Expose expect.extend and alias jasmine.addMatchers to it

* Clean up code for PR

* rename files

* Cleanup + updates.
tushardhole pushed a commit to tushardhole/jest that referenced this pull request Aug 21, 2017
* Expose expect.extend and alias jasmine.addMatchers to it

* Clean up code for PR

* rename files

* Cleanup + updates.
@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.

4 participants