Skip to content

Commit

Permalink
chore: remove some node 10 compat code (#12515)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Feb 28, 2022
1 parent a20bd2c commit 59a7af7
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 71 deletions.
4 changes: 0 additions & 4 deletions packages/jest-circus/src/formatNodeAssertErrors.ts
Expand Up @@ -79,10 +79,6 @@ const getOperatorName = (operator: string | undefined, stack: string) => {
if (stack.match('.throws')) {
return 'throws';
}
// this fallback is only needed for versions older than node 10
if (stack.match('.fail')) {
return 'fail';
}
return '';
};

Expand Down
4 changes: 0 additions & 4 deletions packages/jest-jasmine2/src/assertionErrorMessage.ts
Expand Up @@ -42,10 +42,6 @@ const getOperatorName = (operator: string | null, stack: string) => {
if (stack.match('.throws')) {
return 'throws';
}
// this fallback is only needed for versions older than node 10
if (stack.match('.fail')) {
return 'fail';
}
return '';
};

Expand Down
7 changes: 1 addition & 6 deletions packages/jest-source-map/src/__tests__/getCallsite.test.ts
Expand Up @@ -9,12 +9,7 @@ import * as fs from 'graceful-fs';
import SourceMap from 'source-map';
import getCallsite from '../getCallsite';

// Node 10.5.x compatibility
jest.mock('graceful-fs', () => ({
...jest.createMockFromModule<typeof import('fs')>('fs'),
ReadStream: jest.requireActual('fs').ReadStream,
WriteStream: jest.requireActual('fs').WriteStream,
}));
jest.mock('graceful-fs');

describe('getCallsite', () => {
test('without source map', () => {
Expand Down
90 changes: 35 additions & 55 deletions packages/jest-transform/src/__tests__/ScriptTransformer.test.ts
Expand Up @@ -11,30 +11,40 @@ import type {Config} from '@jest/types';
import type {Options, ShouldInstrumentOptions, Transformer} from '../types';

jest
.mock('graceful-fs', () =>
// Node 10.5.x compatibility
({
...jest.createMockFromModule('fs'),
ReadStream: jest.requireActual('fs').ReadStream,
WriteStream: jest.requireActual('fs').WriteStream,
readFileSync: jest.fn(path => {
if (mockFs[path]) {
return mockFs[path];
}

throw new Error(`Cannot read path '${path}'.`);
}),
statSync: path => ({
isFile: () => !!mockFs[path],
mtime: {getTime: () => 42, toString: () => '42'},
}),
}),
)
.mock('graceful-fs', () => ({
...jest.requireActual('graceful-fs'),
realPathSync: {
native: dirInput => dirInput,
},
/* eslint-disable sort-keys */
readFileSync: jest.fn((path, options) => {
mockInvariant(typeof path === 'string');

expect(options).toBe('utf8');
if (mockFs[path]) {
return mockFs[path];
}

throw new Error(`Cannot read path '${path}'.`);
}),
writeFileSync: jest.fn((path, data, options) => {
mockInvariant(typeof path === 'string');
expect(options).toBe('utf8');
mockFs[path] = data;
}),

unlinkSync: jest.fn(),
statSync: jest.fn(path => ({
isFile() {
mockInvariant(typeof path === 'string');
return !!mockFs[path];
},
mtime: {getTime: () => 42, toString: () => '42'},
})),

existsSync: jest.fn(path => {
mockInvariant(typeof path === 'string');

return !!mockFs[path];
}),
/* eslint-enable */
}))
.mock('jest-haste-map', () => ({
getStatic() {
Expand Down Expand Up @@ -257,36 +267,6 @@ describe('ScriptTransformer', () => {
});

fs = require('graceful-fs');
fs.readFileSync = jest.fn((path, options) => {
invariant(typeof path === 'string');

expect(options).toBe('utf8');
if (mockFs[path]) {
return mockFs[path];
}

throw new Error(`Cannot read path '${path}'.`);
});
fs.writeFileSync = jest.fn((path, data, options) => {
invariant(typeof path === 'string');
expect(options).toBe('utf8');
mockFs[path] = data;
});

fs.unlinkSync = jest.fn();
fs.statSync = jest.fn(path => ({
isFile() {
invariant(typeof path === 'string');
return !!mockFs[path];
},
mtime: {getTime: () => 42, toString: () => '42'},
}));

fs.existsSync = jest.fn(path => {
invariant(typeof path === 'string');

return !!mockFs[path];
});

writeFileAtomic = require('write-file-atomic');

Expand Down Expand Up @@ -410,7 +390,7 @@ describe('ScriptTransformer', () => {
];

incorrectReturnValues.forEach(([returnValue, filePath]) => {
invariant(typeof filePath === 'string');
mockInvariant(typeof filePath === 'string');
require('passthrough-preprocessor').process.mockReturnValue(returnValue);
expect(() =>
scriptTransformer.transform(filePath, getCoverageOptions()),
Expand All @@ -423,7 +403,7 @@ describe('ScriptTransformer', () => {
];

correctReturnValues.forEach(([returnValue, filePath]) => {
invariant(typeof filePath === 'string');
mockInvariant(typeof filePath === 'string');
require('passthrough-preprocessor').process.mockReturnValue(returnValue);
expect(() =>
scriptTransformer.transform(filePath, getCoverageOptions()),
Expand Down Expand Up @@ -1844,7 +1824,7 @@ function getCoverageOptions(
};
}

function invariant(subject: unknown): asserts subject {
function mockInvariant(subject: unknown): asserts subject {
if (!subject) {
throw new Error('Went boom');
}
Expand Down
2 changes: 0 additions & 2 deletions packages/pretty-format/src/__tests__/DOMCollection.test.ts
Expand Up @@ -125,8 +125,6 @@ describe('DOMCollection plugin for list items', () => {
expect(select.options).toPrettyPrintTo(expectedHTMLOptionsCollection);
});

// When Jest upgrades to a version of jsdom later than 12.2.0,
// the class name might become HTMLFormControlsCollection
const expectedHTMLFormControlsCollection = [
'HTMLCollection [',
' <select>',
Expand Down

0 comments on commit 59a7af7

Please sign in to comment.