防止用 Jest 调用本地导入的模块
Prevent calling of local imported modules with Jest
我正在使用的代码:
// redux-reducer.js
import { combineReducers, loop, Effects } from 'redux-loop'
import { loginStart } from './actions'
import {
signUp,
login,
refreshAccessTokenStart,
replayAction,
postConfirmationToken,
postRequestForPasswordReset,
postNewPassword
} from './effects'
...
...
...
// redux-reducer-test.js
import combinedReducer from './reducers'
beforeEach(() => {
require.requireMock('effects') // something like this
})
...
...
...
问题是本地 ./effects
模块调用 react native 环境配置脚本。
我想以 ./effects
不被执行的方式模拟 ./effects
模块。
尚未完成的事情:
- 使用
require.requireMock('effects')
方法
- 让它自动被模拟
- 使用 proxyquire:我认为
proxyquire.noCallThru()
会阻止 ./effects
成为 运行。似乎 proxyquire 与 Jest 不兼容,并且 Jest 中应该(?)以某种方式具有此功能?
有兴趣听听您的想法并进一步了解 Jest!
还没有在文档中,这对我有用:
来自Jest blog about v15 changes:
Automocking is now disabled by default in Jest. This is by far the
most confusing feature for new users and in many ways it doesn't make
sense for small projects. We introduced automocking at Facebook and it
worked great for us when unit testing was adopted in a large existing
code base with few existing tests, but over time it felt like people
spent more time fighting with mocked/unmocked modules than it would
have taken them to write a test normally. We also noticed that library
authors often require a huge number of basic modules that always have
to be manually unmocked. Even for Jest itself we realized that the
majority of tests had automocking disabled manually. We still believe
that explicit automocking can be incredibly valuable. This change
simply trades implicit mocks for explicit mocks via calls to
jest.mock(moduleName).
If you would still like to use automocking by default, enable the
automock setting in your configuration or manually call
jest.enableAutomock() in your test or setup file.
Automocking 现在可以与我的 package.json jest 配置一起使用,如下所示:
"jest": {
"preset": "jest-react-native",
"testRegex": "\.test\.js$",
"setupFiles": [
"./setupJest.js"
],
"automock": true
},
好像文档不完整,关注博客!
它在文档中,在这里:Jest Docs
automock [boolean]
Default: false
This option is disabled by default. If you are introducing Jest to a large organization with an existing codebase but few tests, enabling this option can be helpful to introduce unit tests gradually. Modules can be explicitly auto-mocked using jest.mock(moduleName).
我正在使用的代码:
// redux-reducer.js
import { combineReducers, loop, Effects } from 'redux-loop'
import { loginStart } from './actions'
import {
signUp,
login,
refreshAccessTokenStart,
replayAction,
postConfirmationToken,
postRequestForPasswordReset,
postNewPassword
} from './effects'
...
...
...
// redux-reducer-test.js
import combinedReducer from './reducers'
beforeEach(() => {
require.requireMock('effects') // something like this
})
...
...
...
问题是本地 ./effects
模块调用 react native 环境配置脚本。
我想以 ./effects
不被执行的方式模拟 ./effects
模块。
尚未完成的事情:
- 使用
require.requireMock('effects')
方法 - 让它自动被模拟
- 使用 proxyquire:我认为
proxyquire.noCallThru()
会阻止./effects
成为 运行。似乎 proxyquire 与 Jest 不兼容,并且 Jest 中应该(?)以某种方式具有此功能?
有兴趣听听您的想法并进一步了解 Jest!
还没有在文档中,这对我有用:
来自Jest blog about v15 changes:
Automocking is now disabled by default in Jest. This is by far the most confusing feature for new users and in many ways it doesn't make sense for small projects. We introduced automocking at Facebook and it worked great for us when unit testing was adopted in a large existing code base with few existing tests, but over time it felt like people spent more time fighting with mocked/unmocked modules than it would have taken them to write a test normally. We also noticed that library authors often require a huge number of basic modules that always have to be manually unmocked. Even for Jest itself we realized that the majority of tests had automocking disabled manually. We still believe that explicit automocking can be incredibly valuable. This change simply trades implicit mocks for explicit mocks via calls to jest.mock(moduleName).
If you would still like to use automocking by default, enable the automock setting in your configuration or manually call jest.enableAutomock() in your test or setup file.
Automocking 现在可以与我的 package.json jest 配置一起使用,如下所示:
"jest": {
"preset": "jest-react-native",
"testRegex": "\.test\.js$",
"setupFiles": [
"./setupJest.js"
],
"automock": true
},
好像文档不完整,关注博客!
它在文档中,在这里:Jest Docs
automock [boolean]
Default: false
This option is disabled by default. If you are introducing Jest to a large organization with an existing codebase but few tests, enabling this option can be helpful to introduce unit tests gradually. Modules can be explicitly auto-mocked using jest.mock(moduleName).