-
-
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
Allow importing Jest as an ES6 module #7571
Conversation
Allows importing Jest as an ES6 module to replace using global function names (ex. jest.expect, jest.describe, jest.test, etc).
Codecov Report
@@ Coverage Diff @@
## master #7571 +/- ##
=========================================
+ Coverage 67.99% 68% +<.01%
=========================================
Files 248 249 +1
Lines 9490 9491 +1
Branches 5 6 +1
=========================================
+ Hits 6453 6454 +1
Misses 3035 3035
Partials 2 2
Continue to review full report at Codecov.
|
@SimenB has there been discussion on this before? Couldn't find any relevant issues/PRs. I'm wondering whether there are any downsides, because the way AVA does this seems so much cleaner to me. |
Yeah, #4473 and #5192. I know @rubennorte wants it as well. When it comes to this PR, I think it's more likely that we'll support it by binding e.g. The approach in this PR is no different from something you can do in userland, just copy |
@SimenB it's true this is different from Ava, which provides all assertions through a callback param. I wasn't trying to achieve that. I was just trying to add the option of importing functions instead of relying on globals. Also, this isn't exactly the same as importing from import jest from 'jest'
const { expect } = jest And finally, something like this might make a more ava-like |
Yeah, but I don't think that's something we necessarily want in core - if we do this, I think we want to fully implement it, not fake it. And it's as mentioned pretty easy to add in userland.
I meant to copy the version of that file from this PR. To get named exports, just do something like this: // custom-jest.js
export const expect = global.expect;
export const test = global.test;
// etc // test.js
import {test, expect} from './custom-jest'; Having a separate export is actually a pretty neat idea. I had a working implementation of passing We are currently looking to migrate away from Jasmine over to something called |
If there's any chance for this to happen it would be amazing. A problem we have right now is that in the monorepo in which I'm working most of the libs are being tested with Karma. My idea is to convert to Jest feature-by-feature, but we (as far as I know) can't have both Jasmine and Jest declaring the same things on the global scope. The solution to this as I saw it was to import jest and use |
I like the idea of
As I understand, you want to run the same test files with both karma and jest. If this PR is just for migrating current tests to jest, you can use the described approach e.g. |
Any movement on this? I'd love this feature! |
We're also waiting for this feature to be merged. Upon moving some tests from mocha to jest, I'm surprised this isn't a feature by default. Hopefully there will be movement on this PR. In the meantime, the following fix will make it almost transparent. // <rootDir>/test/jest.js
export default jest;
export const { expect, test } = global; // <rootDir>/jest.config.js
module.exports = {
moduleNameMapper: {
'^jest$': '<rootDir>/test/jest.js'
}
}; Now we can |
It would be really great to use Jest in this way! Please, give us the option to import Jest! |
This comment has been minimized.
This comment has been minimized.
it: test, | ||
test, | ||
xdescribe: describe.skip, | ||
}; |
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 to be missing fit
, xit
, and xtest
. (full list is at https://jestjs.io/docs/en/api)
So since it doesn't seem like this feature will be added to core import { describe, it, expect } from 'jest-without-globals'
describe('describe should create a section', () => {
it('it should checkmark', () => {
expect('').toBe('')
})
}) I'm currently using it in a few of my projects already and am planning on using it in all of my The global namespace is unfortunately still polluted ( If the Jest team is amenable to linking to an external package, I could submit a PR to add |
|
I just landed #9801 which has support for this. It doesn't prevent setting of globals, but it gives you a (type safe) way to import them. Will add a way to avoid mutating the global env soonish |
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
Allows importing jest as an ES6 module instead of using globals (see below for an example).
Everything still works the same as before, but if you prefer Ava's approach of no global variables, this allows using the same type of syntax. Also might be useful if your editor complains if you are using global variables.
I added a test and documentation. Please see
docs/Es6Import.md
for more info.I realize most users probably won't use this, but I just thought it might be useful to make it available as an option in case someone does want it. Also might be useful for exposing new functions in the future.
Please let me know if I did anything wrong or need to make any more changes.
Test plan