Skip to content

Commit

Permalink
fix(runtime): handle missing mocked property (#12213)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Jan 4, 2022
1 parent 4eabd9d commit fcd626d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
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

0 comments on commit fcd626d

Please sign in to comment.