Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ module.exports = {
'import/no-extraneous-dependencies': 0,
},
},
{
files: ['packages/jest-jasmine2/**', 'packages/jest-circus/**'],
rules: {
'no-array-constructor': 0,
},
},
],
parser: 'babel-eslint',
plugins: ['markdown', 'import', 'prettier'],
Expand Down
8 changes: 4 additions & 4 deletions packages/jest-circus/src/eventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ const eventHandler: EventHandler = (event, state): void => {
} else if (type === 'afterAll') {
// Attaching `afterAll` errors to each test makes execution flow
// too complicated, so we'll consider them to be global.
state.unhandledErrors.push([error, asyncError]);
state.unhandledErrors.push(new Array(error, asyncError));
} else {
invariant(test, 'always present for `*Each` hooks');
test!.errors.push([error, asyncError]);
test!.errors.push(new Array(error, asyncError));
}
break;
}
Expand Down Expand Up @@ -143,11 +143,11 @@ const eventHandler: EventHandler = (event, state): void => {
error,
test: {asyncError},
} = event;
event.test.errors.push([error, asyncError]);
event.test.errors.push(new Array(error, asyncError));
break;
}
case 'test_retry': {
event.test.errors = [];
event.test.errors = new Array();
break;
}
case 'run_start': {
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-circus/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const _runTestsForDescribeBlock = async (describeBlock: DescribeBlock) => {

// Tests that fail and are retried we run after other tests
const retryTimes = parseInt(global[RETRY_TIMES], 10) || 0;
const deferredRetryTests = [];
const deferredRetryTests = new Array();

for (const test of describeBlock.tests) {
await _runTest(test);
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-circus/src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const INITIAL_STATE: State = {
rootDescribeBlock: ROOT_DESCRIBE_BLOCK,
testNamePattern: null,
testTimeout: 5000,
unhandledErrors: [],
unhandledErrors: new Array(),
};

global[STATE_SYM] = INITIAL_STATE;
Expand Down
29 changes: 16 additions & 13 deletions packages/jest-circus/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ export const makeDescribe = (
}

return {
children: [],
hooks: [],
children: new Array(),
hooks: new Array(),
mode: _mode,
name: convertDescriptorToString(name),
parent,
tests: [],
tests: new Array(),
};
};

Expand All @@ -63,7 +63,7 @@ export const makeTest = (
): TestEntry => ({
asyncError,
duration: null,
errors: [],
errors: new Array(),
fn,
invocations: 0,
mode,
Expand Down Expand Up @@ -95,8 +95,8 @@ export const getAllHooksForDescribe = (describe: DescribeBlock) => {
beforeAll: Array<Hook>;
afterAll: Array<Hook>;
} = {
afterAll: [],
beforeAll: [],
afterAll: new Array(),
beforeAll: new Array(),
};

if (hasEnabledTest(describe)) {
Expand All @@ -119,11 +119,11 @@ export const getEachHooksForTest = (test: TestEntry) => {
const result: {
beforeEach: Array<Hook>;
afterEach: Array<Hook>;
} = {afterEach: [], beforeEach: []};
} = {afterEach: new Array(), beforeEach: new Array()};
let block: DescribeBlock | undefined | null = test.parent;

do {
const beforeEachForCurrentBlock = [];
const beforeEachForCurrentBlock = new Array();
for (const hook of block.hooks) {
switch (hook.type) {
case 'beforeEach':
Expand All @@ -136,7 +136,10 @@ export const getEachHooksForTest = (test: TestEntry) => {
}
// 'beforeEach' hooks are executed from top to bottom, the opposite of the
// way we traversed it.
result.beforeEach = [...beforeEachForCurrentBlock, ...result.beforeEach];
result.beforeEach = new Array(
...beforeEachForCurrentBlock,
...result.beforeEach,
);
} while ((block = block.parent));
return result;
};
Expand Down Expand Up @@ -248,9 +251,9 @@ export const makeRunResult = (

const makeTestResults = (describeBlock: DescribeBlock): TestResults => {
const {includeTestLocationInResult} = getState();
let testResults: TestResults = [];
let testResults: TestResults = new Array();
for (const test of describeBlock.tests) {
const testPath = [];
const testPath = new Array();
let parent: TestEntry | DescribeBlock = test;
do {
testPath.unshift(parent.name);
Expand Down Expand Up @@ -298,7 +301,7 @@ const makeTestResults = (describeBlock: DescribeBlock): TestResults => {
// Return a string that identifies the test (concat of parent describe block
// names + test title)
export const getTestID = (test: TestEntry) => {
const titles = [];
const titles = new Array();
let parent: TestEntry | DescribeBlock = test;
do {
titles.unshift(parent.name);
Expand Down Expand Up @@ -342,7 +345,7 @@ export const addErrorToEachTestUnderDescribe = (
asyncError: Exception,
) => {
for (const test of describeBlock.tests) {
test.errors.push([error, asyncError]);
test.errors.push(new Array(error, asyncError));
}

for (const child of describeBlock.children) {
Expand Down
4 changes: 3 additions & 1 deletion packages/jest-jasmine2/src/queueRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ export default function queueRunner(options: Options) {
}
});

promise = Promise.race([promise, token]);
// `new Array` to avoid contaminated array prototypes
// $FlowFixMe
promise = Promise.race(new Array(promise, token));

if (!timeout) {
return promise;
Expand Down
5 changes: 5 additions & 0 deletions packages/jest-util/src/installCommonGlobals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ export default function(
value: fs.existsSync.bind(fs),
writable: false,
},
[symbol.for('jest-native-array')]: {
enumerable: false,
value: Array.bind(Array),
writable: false,
},
'jest-symbol-do-not-touch': {
enumerable: false,
value: symbol,
Expand Down
12 changes: 12 additions & 0 deletions scripts/babel-plugin-jest-native-globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ module.exports = ({template}) => {
const fsExistsFileDeclaration = template(`
var jestExistsFile = global[Symbol.for('jest-native-exists-file')] || fs.existsSync;
`);
const arrayDeclaration = template(`
var Array = global[Symbol.for('jest-native-array')] || Array;
`);

return {
name: 'jest-native-globals',
Expand Down Expand Up @@ -116,6 +119,15 @@ module.exports = ({template}) => {
path.parentPath.replaceWithSourceString('jestExistsFile');
}
}
if (path.node.name === 'Array' && !state.jestInjectedArray) {
state.jestInjectedArray = true;
path
.findParent(p => p.isProgram())
.unshiftContainer('body', arrayDeclaration());
path
.findParent(p => p.isProgram())
.unshiftContainer('body', symbolDeclaration());
}
},
},
};
Expand Down