I'm using fetch-mock in order to mock some requests to the server. This is where all the requests are made:
import fetchMock from 'fetch-mock'
import initialState from 'src/initial-state'
if (process.env.NODE_ENV === 'development') {
fetchMock.post('/some/endpoint', initialState.entities.multichannelEngagement)
}
But not only this endpoint is being mock, but all the requests made with isomorphic-fetch
import 'isomorphic-fetch'
export function makeRequest(endpoint, config = {}) {
return window.fetch(endpoint, config)
.then(response => {
return response.json()
.then(json => ({ json, response }))
.catch(() => ({ response }))
})
.then(({ json, response }) => {
if (!response.ok) {
throw json ? json : new Error(response.statusText)
} else {
return json
}
})
.catch((e) => {
return Promise.reject(e)
})
}
My webpack.config.js is as follows:
import path from 'path'
import dotenv from 'dotenv'
import webpack from 'webpack'
import info from './package.json'
const resolvePath = p => path.join(__dirname, p)
const __DEV__ = process.env.NODE_ENV !== 'production'
const { parsed: env } = dotenv.load()
env.NODE_ENV = process.env.NODE_ENV
Object.keys(env).forEach(k => env[k] = JSON.stringify(env[k]))
const config = {
name: info.name,
entry: {
app: 'src/index',
vendor: Object.keys(info.dependencies)
},
output: {
path: __DEV__ ? resolvePath('public') : resolvePath('../analytics-server/server/public'),
filename: '/js/[name].js',
publicPath: '/',
debug: __DEV__,
pathinfo: __DEV__
},
module: {
preLoaders: [{
// NOTE: Run linter before transpiling
test: /\.js$/,
loader: 'eslint-loader',
exclude: /node_modules/
}],
loaders: [{
test: /\.js$/,
loader: 'babel',
exclude: /node_modules/
}, {
// TODO: Remove after upgrading to webpack 2
test: /\.json$/,
loader: 'json'
}]
},
resolve: {
alias: {
src: resolvePath('src'),
core: resolvePath('src/core'),
components: resolvePath('src/components'),
modules: resolvePath('src/modules'),
services: resolvePath('src/services'),
resources: resolvePath('src/resources'),
locales: resolvePath('src/locales')
},
// NOTE: Empty string to properly resolve when providing extension
// TODO: Remove after upgrading to webpack 2
extensions: ['', '.js']
},
plugins: [
// NOTE: `NoErrorsPlugin` causes eslint warnings to stop the build process
// new webpack.NoErrorsPlugin(),
new webpack.optimize.CommonsChunkPlugin('commons', '/js/commons.js'),
new webpack.DefinePlugin({ process: { env } })
// new webpack.NormalModuleReplacementPlugin( /^fetch-mock$/, path.resolve( __dirname, 'node_modules', 'fetch-mock/src/client.js' ) )
],
eslint: {
configFile: resolvePath('.eslintrc')
}
}
if (__DEV__) {
config.devtool = 'source-map'
config.devServer = {
contentBase: 'public',
// NOTE: Options `inline` and `hot` shall be passed as CLI arguments
// inline: true,
// hot: true,
historyApiFallback: true
}
} else {
config.plugins.push(...[
new webpack.optimize.DedupePlugin(),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.UglifyJsPlugin({
compress: true,
acorn: true
})
])
}
export default config
The error I get when I run the app is "fetch-mock.js:93 Uncaught Error: No fallback response defined for GET to http://localhost:3000/api/session" which is the first request made in the app.
No idea why fetch-mock is mocking all requests. When evaluating on the chrome console, the value of the fetch on the makeRequest function is the fetch-mock function but as far as I know this is right.
BTW, I'm not in testing env, I'm on development because I need my backend mocked because it is not done yet.
Any idea why this is happening?
Thanks in advance
The problem is caused because fetch-mock
's main goal is to help with testing. In a test environment, it's better if you get an exception for any non-mocked calls being dispatched.
You can however add a catch
handler that delegates to the original fetch
, so any non-mocked request is passed to the real fetch. Something like the following:
/* FAKE FETCH ME */
fetchMock.get('/session', function getSession(url, opts) {
const jwt = extractToken(opts)
if (!jwt || jwt !== fakeToken) {
return delay({
status: 401,
body: JSON.stringify({
details: 'Unauthorized'
})
})
}
return delay({
status: 200,
body: JSON.stringify({
success: true,
data: fakeUserDetails
})
})
})
.catch(unmatchedUrl => {
// fallover call original fetch, because fetch-mock treats
// any unmatched call as an error - its target is testing
return realFetch(unmatchedUrl)
})
The library used to have an option for that but it was removed in V5. See documentation here:
In previous versions fetch-mock had a greed property, set to
- good - unmatched calls respond with a 200
- bad - unmatched calls error
- none - allow unmatched calls to use native fetch and the network
This has now been replaced by a .catch() method which accepts the same types of response as a normal call to .mock(matcher, response). It can also take an arbitrary function to completely customise behaviour of unmatched calls. It is chainable and can be called before or after other calls to .mock(). The api to check for unmatched calls remains unchanged.