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

fix(runtime): handle missing mocked property #12213

Merged
merged 3 commits into from Jan 4, 2022
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -4,9 +4,10 @@

### Fixes

- `[@jest/transform]` Update dependency package `pirates` to 4.0.4 ([#12136](https://github.com/facebook/jest/pull/12136))
- `[jest-environment-node]` Add `AbortSignal` ([#12157](https://github.com/facebook/jest/pull/12157))
- `[jest-environment-node]` Add Missing node global `performance` ([#12002](https://github.com/facebook/jest/pull/12002))
- `[jest-runtime]` Handle missing `mocked` property ([#12002](https://github.com/facebook/jest/pull/12002))
- `[@jest/transform]` Update dependency package `pirates` to 4.0.4 ([#12213](https://github.com/facebook/jest/pull/12213))

### Chore & Maintenance

Expand Down
48 changes: 47 additions & 1 deletion e2e/__tests__/testEnvironment.test.ts
Expand Up @@ -5,9 +5,18 @@
* LICENSE file in the root directory of this source tree.
*/

import {json as runWithJson} from '../runJest';
import {tmpdir} from 'os';
import * as path from 'path';
import slash = require('slash');
import {cleanup, createEmptyPackage, writeFiles} from '../Utils';
import runJest, {json as runWithJson} from '../runJest';
import * as testFixturePackage from '../test-environment/package.json';

const DIR = path.resolve(tmpdir(), 'test-env-no-mocked');

beforeEach(() => cleanup(DIR));
afterAll(() => cleanup(DIR));

it('respects testEnvironment docblock', () => {
expect(testFixturePackage.jest.testEnvironment).toEqual('node');

Expand All @@ -16,3 +25,40 @@ it('respects testEnvironment docblock', () => {
expect(result.success).toBe(true);
expect(result.numTotalTests).toBe(3);
});

it('handles missing `mocked` property', () => {
createEmptyPackage(DIR);
writeFiles(DIR, {
'env.js': `
const Node = require('${slash(
require.resolve('jest-environment-node'),
)}');

module.exports = class Thing extends Node {
constructor(...args) {
super(...args);

this.moduleMocker.mocked = undefined;
}
};
`,
'test.js': `
/**
* @jest-environment ./env.js
*/

jest.mocked();

test('halla', () => {
expect(global.thing).toBe('nope');
});
`,
});

const {exitCode, stderr} = runJest(DIR);

expect(exitCode).toBe(1);
expect(stderr).toContain(
'Your test environment does not support `mocked`, please update it.',
);
});
8 changes: 7 additions & 1 deletion packages/jest-runtime/src/index.ts
Expand Up @@ -1924,7 +1924,13 @@ export default class Runtime {
};
const fn = this._moduleMocker.fn.bind(this._moduleMocker);
const spyOn = this._moduleMocker.spyOn.bind(this._moduleMocker);
const mocked = this._moduleMocker.mocked.bind(this._moduleMocker);
const mocked =
this._moduleMocker.mocked?.bind(this._moduleMocker) ??
(() => {
throw new Error(
'Your test environment does not support `mocked`, please update it.',
);
});

const setTimeout = (timeout: number) => {
if (this._environment.global.jasmine) {
Expand Down