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

Add ability to explicitly import global variables #4473

Closed
NameFILIP opened this issue Sep 13, 2017 · 20 comments · Fixed by #9801
Closed

Add ability to explicitly import global variables #4473

NameFILIP opened this issue Sep 13, 2017 · 20 comments · Fixed by #9801

Comments

@NameFILIP
Copy link

NameFILIP commented Sep 13, 2017

While exposing global variables like test, expect, etc. is a "good" idea, it would be nice to be able to avoid it and explicitly require/import it from a module. For example:

import { test, expect } from 'jest';
@thymikee
Copy link
Collaborator

@aaronabramov is working on this, but it's not a high priority issue for now.

@eugen35
Copy link

eugen35 commented Dec 29, 2017

Is now possible explicitly import only expect. For example, so: const expect = require('jest').expect; ??

@SimenB
Copy link
Member

SimenB commented Dec 29, 2017

const expect = require('expect'); is possible

@eugen35
Copy link

eugen35 commented Dec 30, 2017

@SimenB But this is not for jest assertions? (In your require your not point to jest). What npm package do your mean? I need explicitly import jest assertions, like expect(1+1).toBe(2); Because i like jest assertions for brevity

@SimenB
Copy link
Member

SimenB commented Dec 30, 2017

Jest assertions are in the expect package

@NameFILIP
Copy link
Author

how about the other variables? test?

@SimenB
Copy link
Member

SimenB commented Dec 30, 2017

As of now that's not possible

@maartenschumacher
Copy link

Exposing globals make it harder to type check as well: flow-typed/flow-typed#251

@zaynv
Copy link

zaynv commented Feb 14, 2018

I understand that this is low priority, but could it be reopened until this feature has been implemented?

@novellizator
Copy link

novellizator commented Apr 19, 2018

We have also ditched jest, just because of absence of this.

@lax4mike
Copy link

While exposing global variables like test, expect, etc. is a "good" idea

@NameFILIP I've always wondered about why testing frameworks use globals. Why is it a "good" idea?

@NameFILIP
Copy link
Author

@lax4mike the quotes around "good" mean I'd prefer it to not be global

@lax4mike
Copy link

I don't like globals either, so I'm wondering why they're so prevalent in this space. Any ideas?

@gobengo
Copy link

gobengo commented Jul 11, 2018

@lax4mike lock-in

@SimenB
Copy link
Member

SimenB commented Jul 12, 2018

Jest Circus (#6295) will technically allow you to import all globals except for expect and jest, in addition to removing the jasmine globals.
Once that's landed and shipped as default, we can start looking into some sort of "no-globals" mode.

EDIT: you can import expect today, but it's missing snapshot matchers

@bugeats
Copy link

bugeats commented Oct 23, 2018

I don't like globals either, so I'm wondering why they're so prevalent in this space. Any ideas?

Lack of imagination and long series of copying what test frameworks have always done. For some bizarre reason, it's suddenly ok to use magic and globals in your JS as long as it's a test file.

If anyone is interested in a powerful but minimalist alternative to Jest that doesn't use globals, take a look at substack/tape. Ideally, Jest would offer a similar interface to use explicit requires and plain functions.

@xenoterracide
Copy link

xenoterracide commented Dec 12, 2019

I have this problem when I enabled

"types": ["reflect-metadata"],

in typescript, this is the workaround https://github.com/stherrienaspnet/nodejs-inversify-typescript-jest/blob/master/tsconfig.json

"types": ["reflect-metadata", "jest"],

@agilgur5
Copy link

Duplicating my comment in a related PR as this issue seems to be more visible.
Since it doesn't seem like this feature will be added to core jest soon, I decided to implement a user-land solution as a micro-package called jest-without-globals. Per the docs, it just allows you to explicitly import jest's globals as such:

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 jest-tested projects.


If anyone is interested in a powerful but minimalist alternative to Jest that doesn't use globals, take a look at substack/tape.

Or AVA for something that's tape-like, but more modern and with more stuff built-in. I've used both a decent bit and prefer Jest over it for a number of reasons (nicer output, more built-ins, many more ways to configure, etc), even if the API is a bit cleaner

@SimenB
Copy link
Member

SimenB commented Apr 12, 2020

This will be available in the next version of Jest via #9801.

@github-actions
Copy link

This issue 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 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.