导入反应图时 Jest 失败并显示 'self is not defined'
Jest fails with 'self is not defined' when importing react-diagrams
我正在尝试创建一个与 npm 依赖项交互的基本笑话测试:react-diagrams
失败的测试
import { DiagramModel } from '@projectstorm/react-diagrams'
test('importing react diagrams', () => {
let x = DiagramModel
});
仅引用 DiagramModel
class 会导致此错误:
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)
其他测试工作正常,并且在其他地方捆绑时依赖项工作正常。
jest.config.js
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
package.json
"jest": "^26.6.3",
"ts-jest": "^26.5.2",
...
有什么办法可以解决这个问题吗?
正在复制
在 codesandbox (but could not get the test runner to pick it up). The full repo
中添加了测试 + 配置
经过一些测试,它终于可以使用这个配置了:
jest.config.js
module.exports = {
preset: 'ts-jest',
testEnvironment: 'jsdom',
};
package.json :
{
"name": "jest-test",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"devDependencies": {
"jest": "^26.6.3",
"ts-jest": "^26.5.2"
},
"scripts": {
"test": "jest"
},
"dependencies": {
"@emotion/react": "^11.1.5",
"@emotion/styled": "^11.3.0",
"@projectstorm/react-canvas-core": "^6.5.2",
"@projectstorm/react-diagrams": "^6.5.2",
"@projectstorm/react-diagrams-routing": "^6.5.2",
"closest": "^0.0.1",
"dagre": "^0.8.5",
"pathfinding": "^0.4.18",
"paths-js": "^0.4.11",
"react": "^17.0.2",
"resize-observer-polyfill": "^1.5.1"
}
}
您忽略了 @projectstorm/react-diagrams
是一个 react
库,它需要 dom
环境而不是 nodejs
。
我正在尝试创建一个与 npm 依赖项交互的基本笑话测试:react-diagrams
失败的测试
import { DiagramModel } from '@projectstorm/react-diagrams'
test('importing react diagrams', () => {
let x = DiagramModel
});
仅引用 DiagramModel
class 会导致此错误:
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)
其他测试工作正常,并且在其他地方捆绑时依赖项工作正常。
jest.config.js
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
package.json
"jest": "^26.6.3",
"ts-jest": "^26.5.2",
...
有什么办法可以解决这个问题吗?
正在复制
在 codesandbox (but could not get the test runner to pick it up). The full repo
中添加了测试 + 配置经过一些测试,它终于可以使用这个配置了:
jest.config.js
module.exports = {
preset: 'ts-jest',
testEnvironment: 'jsdom',
};
package.json :
{
"name": "jest-test",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"devDependencies": {
"jest": "^26.6.3",
"ts-jest": "^26.5.2"
},
"scripts": {
"test": "jest"
},
"dependencies": {
"@emotion/react": "^11.1.5",
"@emotion/styled": "^11.3.0",
"@projectstorm/react-canvas-core": "^6.5.2",
"@projectstorm/react-diagrams": "^6.5.2",
"@projectstorm/react-diagrams-routing": "^6.5.2",
"closest": "^0.0.1",
"dagre": "^0.8.5",
"pathfinding": "^0.4.18",
"paths-js": "^0.4.11",
"react": "^17.0.2",
"resize-observer-polyfill": "^1.5.1"
}
}
您忽略了 @projectstorm/react-diagrams
是一个 react
库,它需要 dom
环境而不是 nodejs
。