Skip to content

[Bug]: Array.sort in hoist-jest might reversing all statements #4185

@hanjunspirit

Description

@hanjunspirit

Version

27.1.5

Steps to reproduce

for the code:

class Base {
  sum(a: number, b: number) {
    return a + b;
  }
}
class MMath extends Base {}
export { MMath };

Expected behavior

Actual behavior

It will be compiled into:

export { MMath };
class MMath extends Base {
}
class Base {
    sum(a, b) {
        return a + b;
    }
}

This leads to Base class not being found, causing an error.

Debug log

Additional context

This piece of code has different results in different versions of Node.js:
[1,2,3].sort(() => 1)

For example,
in Node.js 10.24.0, it returns [3, 2, 1],
But in Node.js 12.18.3, it returns [1, 2, 3]

This depends on the implementation of the sort algorithm, and the result is unstable.

So for the sort in hoist-jest(#3498), if all isJestGlobalImport and isHoistableStatement return false, it might reversing all statements.

Environment

Node: 10.24.0
Jest: 27.1.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions