From fcd626d202069073e6212acaf730d672323c7859 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 4 Jan 2022 23:54:08 +0100 Subject: [PATCH] fix(runtime): handle missing `mocked` property (#12213) --- CHANGELOG.md | 3 +- e2e/__tests__/testEnvironment.test.ts | 48 ++++++++++++++++++++++++++- packages/jest-runtime/src/index.ts | 8 ++++- 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 772c0304c42e..445c90effdbd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/e2e/__tests__/testEnvironment.test.ts b/e2e/__tests__/testEnvironment.test.ts index 8b70dab5d7d4..9c9a7e825d2e 100644 --- a/e2e/__tests__/testEnvironment.test.ts +++ b/e2e/__tests__/testEnvironment.test.ts @@ -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'); @@ -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.', + ); +}); diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 3fd0f6d21dbd..69fd7a6c9db7 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -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) {