-
-
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
feat: add @jest/globals
package for importing globals explicitly
#9801
Changes from all commits
4bc98b5
96cef21
3aef5f6
7b82687
bb5e1fe
69302ac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import runJest from '../runJest'; | ||
|
||
test('imported globals', () => { | ||
const result = runJest('imported-globals'); | ||
expect(result.exitCode).toBe(0); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import { | ||
expect as importedExpect, | ||
jest as importedJest, | ||
test as importedTest, | ||
} from '@jest/globals'; | ||
|
||
importedTest('they match the globals', () => { | ||
importedExpect(importedExpect).toBe(expect); | ||
importedExpect(importedJest).toBe(jest); | ||
importedExpect(importedTest).toBe(test); | ||
|
||
expect(importedExpect).toBe(expect); | ||
expect(importedJest).toBe(jest); | ||
expect(importedTest).toBe(test); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
module.exports = require('../../babel.config'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"jest": { | ||
"testEnvironment": "node" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
**/__mocks__/** | ||
**/__tests__/** | ||
src | ||
tsconfig.json | ||
tsconfig.tsbuildinfo |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"name": "@jest/globals", | ||
"version": "25.3.0", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/facebook/jest.git", | ||
"directory": "packages/jest-globals" | ||
}, | ||
"engines": { | ||
"node": ">= 8.3" | ||
}, | ||
"license": "MIT", | ||
"main": "build/index.js", | ||
"types": "build/index.d.ts", | ||
"typesVersions": { | ||
"<3.8": { | ||
"build/*": [ | ||
"build/ts3.4/*" | ||
] | ||
} | ||
}, | ||
"dependencies": { | ||
"@jest/environment": "^25.3.0", | ||
"@jest/types": "^25.3.0", | ||
"expect": "^25.3.0" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
test('throw when directly imported', () => { | ||
expect(() => require('../')).toThrowError( | ||
'Do not import `@jest/globals` outside of the Jest test environment', | ||
); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import importedExpect = require('expect'); | ||
import type {Jest} from '@jest/environment'; | ||
import type {Global} from '@jest/types'; | ||
|
||
export declare type jest = Jest; | ||
|
||
export declare type expect = typeof importedExpect; | ||
|
||
export declare type it = Global.GlobalAdditions['it']; | ||
export declare type test = Global.GlobalAdditions['test']; | ||
export declare type fit = Global.GlobalAdditions['fit']; | ||
export declare type xit = Global.GlobalAdditions['xit']; | ||
export declare type xtest = Global.GlobalAdditions['xtest']; | ||
export declare type describe = Global.GlobalAdditions['describe']; | ||
export declare type xdescribe = Global.GlobalAdditions['xdescribe']; | ||
export declare type fdescribe = Global.GlobalAdditions['fdescribe']; | ||
export declare type beforeAll = Global.GlobalAdditions['beforeAll']; | ||
export declare type beforeEach = Global.GlobalAdditions['beforeEach']; | ||
export declare type afterEach = Global.GlobalAdditions['afterEach']; | ||
export declare type afterAll = Global.GlobalAdditions['afterAll']; | ||
|
||
throw new Error( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this file ensure types are correct, but also that it's not imported outside of jest. Compiled code is just the |
||
'Do not import `@jest/globals` outside of the Jest test environment', | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
// we don't want `@types/jest` to be referenced | ||
"types": ["node"], | ||
"rootDir": "src", | ||
"outDir": "build" | ||
}, | ||
"references": [ | ||
{"path": "../expect"}, | ||
{"path": "../jest-environment"}, | ||
{"path": "../jest-types"} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ export type BlockMode = void | 'skip' | 'only' | 'todo'; | |
export type TestMode = BlockMode; | ||
export type TestName = Global.TestName; | ||
export type TestFn = Global.TestFn; | ||
export type HookFn = (done?: DoneFn) => Promise<any> | null | undefined; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we don't actually accept |
||
export type HookFn = Global.HookFn; | ||
export type AsyncFn = TestFn | HookFn; | ||
export type SharedHookType = 'afterAll' | 'beforeAll'; | ||
export type HookType = SharedHookType | 'afterEach' | 'beforeEach'; | ||
|
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.
changes not related, just some local cleanup from looking into not adding the globals to the global env. Went with a simpler solution, but I liked these refactorings 🤷