Skip to content

Jest errors out when importing this package #14

@MasterOdin

Description

@MasterOdin

Our app was hitting the following error when we added tests that relied on a package that relied on this package:

TypeError: Cannot assign to read only property 'name' of function 'function SequenceMatcher(isjunk, a, b, autojunk) {

      at ../../node_modules/difflib/lib/difflib.js:78:25
      at call (../../node_modules/difflib/lib/difflib.js:660:3)
      at Object.<anonymous> (../../node_modules/difflib/lib/difflib.js:1486:4)
      at Object.<anonymous> (../../node_modules/difflib/index.js:1:18)

with the issue being these two lines:

Per https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name, the issue is that the name property for a function is not writable, and so doing assignment isn't allowed, and will do nothing in non-strict contexts, or error in strict contexts. For an example of the former context, see this REPL output:

> a = (function() { console.log('foo') });
[Function: a]
> a.name
'a'
> a.name = 'b'
'b'
> a.name
'a'

If assigning the name is necessary, it needs to be done by doing Object.defineProperty instead, which can bypass the writable attribute:

> a = (function() { console.log('foo') });
[Function: a]
> a.name
'a'
> Object.defineProperty(a, 'name', { value: 'b' })
[Function: b]
> a
[Function: b]
> a.name
'b'

To workaround this, for now we're mocking out the dependency with a stub as we don't depend on the output, but it'd be nice to not have to.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions