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

chore: migrate jest-mock to ESM #10887

Merged
merged 3 commits into from Nov 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -41,6 +41,7 @@
- `[jest-repl, jest-runtime]` [**BREAKING**] Move the `jest-runtime` CLI into `jest-repl` ([#10016](https://github.com/facebook/jest/pull/10016))
- `[jest-resolve]` [**BREAKING**] Migrate to ESM ([#10688](https://github.com/facebook/jest/pull/10688))
- `[jest-resolve-dependencies]` [**BREAKING**] Migrate to ESM ([#10876](https://github.com/facebook/jest/pull/10876))
- `[jest-mock]` [**BREAKING**] Migrate to ESM ([#10887](https://github.com/facebook/jest/pull/10887))
- `[jest-util]` No longer checking `enumerable` when adding `process.domain` ([#10862](https://github.com/facebook/jest/pull/10862))

### Performance
Expand Down
17 changes: 9 additions & 8 deletions packages/jest-environment/src/index.ts
Expand Up @@ -8,10 +8,11 @@
import type {Context, Script} from 'vm';
import type {LegacyFakeTimers, ModernFakeTimers} from '@jest/fake-timers';
import type {Circus, Config, Global} from '@jest/types';
import jestMock = require('jest-mock');

type JestMockFn = typeof jestMock.fn;
type JestMockSpyOn = typeof jestMock.spyOn;
import type {
fn as JestMockFn,
spyOn as JestMockSpyOn,
ModuleMocker,
} from 'jest-mock';

// In Jest 25, remove `Partial` since it's incorrect. The properties are always
// passed, or not. The context itself is optional, not properties within it.
Expand All @@ -38,7 +39,7 @@ export declare class JestEnvironment {
global: Global.Global;
fakeTimers: LegacyFakeTimers<unknown> | null;
fakeTimersModern: ModernFakeTimers | null;
moduleMocker: jestMock.ModuleMocker | null;
moduleMocker: ModuleMocker | null;
/**
* @deprecated implement getVmContext instead
*/
Expand Down Expand Up @@ -112,7 +113,7 @@ export interface Jest {
/**
* Creates a mock function. Optionally takes a mock implementation.
*/
fn: JestMockFn;
fn: typeof JestMockFn;
/**
* Given the name of a module, use the automatic mocking system to generate a
* mocked version of the module for you.
Expand All @@ -136,7 +137,7 @@ export interface Jest {
*/
isMockFunction(
fn: (...args: Array<any>) => unknown,
): fn is ReturnType<JestMockFn>;
): fn is ReturnType<typeof JestMockFn>;
/**
* Mocks a module with an auto-mocked version when it is being required.
*/
Expand Down Expand Up @@ -252,7 +253,7 @@ export interface Jest {
* Note: By default, jest.spyOn also calls the spied method. This is
* different behavior from most other test libraries.
*/
spyOn: JestMockSpyOn;
spyOn: typeof JestMockSpyOn;
/**
* Indicates that the module system should never return a mocked version of
* the specified module from require() (e.g. that it should always return the
Expand Down
Expand Up @@ -8,7 +8,7 @@
import * as util from 'util';
import {runInNewContext} from 'vm';
import wrap from 'jest-snapshot-serializer-raw';
import mock = require('jest-mock');
import {ModuleMocker} from 'jest-mock';
import FakeTimers from '../legacyFakeTimers';

const timerConfig = {
Expand All @@ -22,11 +22,11 @@ const config = {
};

describe('FakeTimers', () => {
let moduleMocker: mock.ModuleMocker;
let moduleMocker: ModuleMocker;

beforeEach(() => {
const global = runInNewContext('this');
moduleMocker = new mock.ModuleMocker(global);
moduleMocker = new ModuleMocker(global);
});

describe('construction', () => {
Expand Down
4 changes: 4 additions & 0 deletions packages/jest-mock/README.md
Expand Up @@ -2,6 +2,10 @@

## API

```js
import {ModuleMocker} from 'jest-mock';
```

### `constructor(global)`

Creates a new module mocker that generates mocks as if they were created in an environment with the given global object.
Expand Down