当我使用 jest 22.4 时,我进行了如下测试
it ('should blah', () => {
const spy = jest.spyOn(React, 'useRef').mockReturnValueOnce(blahMock);
const comp = shallowWithIntl(<Blah />);
const compShallow = comp.first().shallow();
expect(spy).toBeCalled()
});
这适用于 jest 22.4,不适用于 jest 24.9。我不确定那个版本是问题,因为我们的团队提升了一些东西。我将在下面粘贴我们当前和旧的包 json devdependencies 区域。
旧的 package.json 开发依赖项
"devDependencies": {
"@testing-library/react": "^8.0.1",
"axios-mock-adapter": "^1.14.0",
"babel-cli": "^6.24.1",
"babel-core": "^6.24.1",
"babel-jest": "^23.0.0",
"babel-plugin-transform-object-assign": "^6.22.0",
"babel-plugin-transform-object-rest-spread": "^6.23.0",
"babel-plugin-transform-regenerator": "^6.24.1",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.24.1",
"enzyme": "^3.3.0",
"enzyme-adapter-react-16": "^1.1.1",
"enzyme-react-intl": "^1.4.8",
"enzyme-redux": "^0.2.1",
"enzyme-to-json": "^3.3.3",
"eslint": "^5.0.0",
"eslint-config-terra": "^2.5.0",
"generic-site-app": "^1.0.0",
"html-webpack-plugin": "^3.2.0",
"husky": "^0.14.3",
"identity-obj-proxy": "^3.0.0",
"jest": "^22.4.3",
"jest-canvas-mock": "^1.1.0",
"jsdoc": "^3.5.4",
"jsdom": "11.11.0",
"nightwatch": "^0.9.12",
"orion-toolkit-js": "^3.12.0",
"prettier": "^1.14.2",
"prettier-eslint": "^8.8.2",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-intl": "^2.3.0",
"react-on-rails": "7.0.4",
"react-redux": "^5.0.7",
"react-router": "^3.0.5",
"react-test-renderer": "^16.2.0",
"redux": "^3.7.2",
"redux-saga": "^0.15.3",
"redux-test-utils": "^0.2.2",
"regenerator-runtime": "^0.11.1",
"rimraf": "^2.6.1",
"shelljs": "^0.8.1",
"style-loader": "^0.20.3",
"stylelint": "^9.2.0",
"stylelint-config-sass-guidelines": "^5.0.0",
"terra-markdown": "^2.24.0",
"terra-slide-panel": "^3.3.0",
"terra-toolkit": "^4.26.0",
"url-loader": "^1.1.2",
"webpack-bundle-analyzer": "^3.0.3",
"webpack-merge": "^4.2.1"
}
新的 package.json 开发依赖项
"devDependencies": {
"@babel/cli": "^7.7.0",
"@babel/core": "^7.7.2",
"@babel/plugin-proposal-object-rest-spread": "^7.6.2",
"@babel/plugin-transform-object-assign": "^7.2.0",
"@babel/preset-env": "^7.7.1",
"@babel/preset-react": "^7.7.0",
"@testing-library/react": "^8.0.1",
"axios-mock-adapter": "^1.14.0",
"babel-jest": "^24.9.0",
"browserslist-config-terra": "^1.3.0",
"core-js": "^3.4.1",
"enzyme": "^3.3.0",
"enzyme-adapter-react-16": "^1.1.1",
"enzyme-react-intl": "^1.4.8",
"enzyme-redux": "^0.2.1",
"enzyme-to-json": "^3.3.3",
"eslint": "^5.0.0",
"eslint-config-terra": "^2.5.0",
"generic-site-app": "^1.0.0",
"html-webpack-plugin": "^3.2.0",
"husky": "^0.14.3",
"identity-obj-proxy": "^3.0.0",
"jest": "^24.9.0",
"jest-canvas-mock": "^1.1.0",
"jsdoc": "^3.5.4",
"jsdom": "11.11.0",
"prettier": "^1.14.2",
"prettier-eslint": "^8.8.2",
"raf": "^3.4.1",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-intl": "^2.3.0",
"react-on-rails": "7.0.4",
"react-redux": "^5.0.7",
"react-router": "^3.0.5",
"react-test-renderer": "^16.2.0",
"redux": "^3.7.2",
"redux-saga": "^0.15.3",
"redux-test-utils": "^0.2.2",
"regenerator-runtime": "^0.13.3",
"rimraf": "^2.6.1",
"shelljs": "^0.8.1",
"style-loader": "^0.20.3",
"stylelint": "^9.2.0",
"stylelint-config-sass-guidelines": "^5.0.0",
"terra-aggregate-translations": "^1.4.0",
"terra-markdown": "^2.24.0",
"terra-slide-panel": "^3.3.0",
"terra-toolkit": "^5.13.0",
"url-loader": "^1.1.2",
"webpack": "^4.41.2",
"webpack-bundle-analyzer": "^3.0.3",
"webpack-cli": "^3.3.10",
"webpack-dev-server": "^3.9.0",
"webpack-merge": "^4.2.1"
}