A way to stub requests to Contentful.
Contentfaux will first need to be setup. It loads real data from a space configured and uses that to perform unit testing locally. Requests made to Contentful are intercepted and Contentfaux feeds the preloaded data instead.
To install, simply use npm
npm i -D contentfaux
You will also need to add variables to allow the setup data to be loaded before testing. This can be added either in your package.json file, or with environment variables.
If you choose to use the package.json approach, add the following parameters to your package.json file.
{
...
"config": {
"contentfaux": {
"spaceid": "abcd1234",
"apikey": "aaaabbbbccccdddd1111222233334444",
"dir": "path/to/directory"
}
}
}
- spaceid - Your Contentful space id.
- apikey - The api key provided by Contentful.
- dir - The directory to add the mock data. This will be relative to where you call Contentfaux. Defaults too
/approot/contentfaux
.
Alternatively, if you choose to use environment variables, the following can be used:
CONTENTFAUX_SPACEID=abcd1234
CONTENTFAUX_APIKEY=aaaabbbbccccdddd1111222233334444
CONTENTFAUX_DIR=path/to/directory
After it has been installed, add this block to your scripts in package.json:
{
"scripts": {
...,
"contentfaux": "contentfaux setup"
}
}
And run it!
npm run contentfaux
To stub, before calling any Contentful-related tests, include Contentfaux
.
require('contentfaux')
or
import 'contentfaux'
If you choose to do other tests with Contentful, be sure to keep a reference of Contentfaux and unstub.
const Contentfaux = require('contentfaux')
...
Contentfaux.unstub()
or
import Contentfaux from 'contentfaux'
...
Contentfaux.unstub()
MIT