-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Conversation
There was a problem hiding this 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); | ||
}); |
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
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 @@ | |||
/** |
There was a problem hiding this comment.
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! |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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'; |
There was a problem hiding this comment.
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(); | ||
}); | ||
|
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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)
?
There was a problem hiding this comment.
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', () => { |
There was a problem hiding this comment.
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}); | |||
}; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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 :)
This does look good. Would you mind polishing it up so we can ship it? :) |
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. |
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 |
|
||
jasmine.addMatchers({}); | ||
|
||
expect(expect.extend).toBeCalled(); |
There was a problem hiding this comment.
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", | ||
} | ||
`; |
There was a problem hiding this comment.
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; | ||
}; |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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.
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. |
0f71b9a
to
bb6b2ca
Compare
Awesome. Thanks for carrying it across the line for me. I wasn't sure what you guys were ultimately going to want. So thanks! |
* Expose expect.extend and alias jasmine.addMatchers to it * Clean up code for PR * rename files * Cleanup + updates.
* Expose expect.extend and alias jasmine.addMatchers to it * Clean up code for PR * rename files * Cleanup + updates.
* Expose expect.extend and alias jasmine.addMatchers to it * Clean up code for PR * rename files * Cleanup + updates.
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. |
Summary
This solves #1835.
It exposes
expect.extend
as the jest API for adding matchers and coerces jasminesaddMatchers
API.Test plan
I've added
jest-jasmine2/jasmine-uses-jests-extend-api-test.js
which proves a few things:jasmine.addMatchers
callsexpect.extend
jasmine.addMatchers
properly gets the API coerced intoexpect.extend
You can test this by running:
npm run build && npm run jest jasmine-uses-jest