projectstorm / react-diagrams

a super simple, no-nonsense diagramming library written in react that just works
https://projectstorm.cloud/react-diagrams
MIT License
8.51k stars 1.17k forks source link

Error 'self is not defined' when using in Jest test #840

Closed ajthinking closed 3 years ago

ajthinking commented 3 years ago

Im trying to create a basic jest test interacting with react-diagrams classes

The failing test

import { DiagramModel } from '@projectstorm/react-diagrams'

test('importing react diagrams', () => {
    let x = DiagramModel
});

Simply referencing the DiagramModel class causes this error:

    ReferenceError: self is not defined

    > 1 | import { DiagramModel } from '@projectstorm/react-diagrams'
        | ^
      2 |
      3 | test('importing react diagrams', () => {
      4 |     let x = DiagramModel

      at Object.<anonymous> (node_modules/@projectstorm/react-diagrams/dist/index.umd.js:1:331)
      at Object.<anonymous> (tests/DiagramModel.test.ts:1:1)

Other tests and other dependencies works fine. React-diagrams works perfect when bundeled elsewhere.

jest.config.js

module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
};

package.json

"jest": "^26.6.3",
"ts-jest": "^26.5.2",
...

Any ideas what I can do to remedy this?

ajthinking commented 3 years ago

Fixed with this jest.config.js:

module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'jsdom',
};