Jest: Add ability to explicitly import global variables

Created on 13 Sep 2017  路  19Comments  路  Source: facebook/jest

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';

Most helpful comment

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

All 19 comments

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

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

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

@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

Jest assertions are in the expect package

how about the other variables? test?

As of now that's not possible

Exposing globals make it harder to type check as well: https://github.com/flowtype/flow-typed/issues/251

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

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

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?

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

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

@lax4mike lock-in

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

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.

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"],

_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

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

samzhang111 picture samzhang111  路  3Comments

StephanBijzitter picture StephanBijzitter  路  3Comments

stephenlautier picture stephenlautier  路  3Comments

mmcgahan picture mmcgahan  路  3Comments

withinboredom picture withinboredom  路  3Comments