Usage with jest unstable
See original GitHub issueI tried to used this plugin together with Jest.
I thought I could setup the user permissions using strapi admin, export the settings using this plugin and load those settings during the test runs with jest. So I do not have to manually setup the permissions during each test run.
This first worked for me, but I then noticed errors: Sometimes, the settings do not get imported.
Example:
When running npm test
5 times, it will likely work 4 times but 1 time it will fail, without anything changed in between.
In my example I have some routes wich should only be accessible for authorized users. Most of the time the requests work but sometimes I get forbidden errors.
https://user-images.githubusercontent.com/6911678/134194098-9aa14a68-f913-49bb-ad5a-a9922b94b470.mov
I suspect that the import runs asynchronously in the background and sometimes the import is not completed before the tests run. But I am not sure about that.
Is there a reference implementation for use with Jest?
I setup Jest as Described in the strapi docs: https://strapi.io/documentation/developer-docs/latest/guides/unit-testing.html#testing-basic-endpoint-controller
this is my config-sync config:
module.exports = ({ env }) => ({
'config-sync': {
destination: "extensions/config-sync/files/",
minify: false,
importOnBootstrap: true,
include: [
"core-store",
"role-permissions"
],
exclude: [
"core-store.plugin_users-permissions_grant"
]
},
});
This is the test that’s running in my example video:
it('should create ticket', async () => {
await request(strapi.server) // app server is an instance of Class: http.Server
.post('/tickets')
.set('accept', 'application/json')
.set('Content-Type', 'application/json')
.set('Authorization', 'Bearer ' + jwt)
.send({
...ticketData
})
.expect('Content-Type', /json/)
.expect(200)
.then(data => {
expect(data.body).toBeDefined();
expect(data.body.title).toBe(ticketData.title);
})
});
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (4 by maintainers)
Strapi v4 has been released, and this plugin was migrated along with it.
In the migrated plugin this should likely be fixed as a lot of improvements have been made for the
importOnBootstrap
setting. Alternatively you could use the CLI to import the config before you run your test command:Released in v1.0.0-alpha.1 of
strapi-plugin-config-sync
.Thanks for testing again @derweili!
Seems hard to reproduce, too bad that the PR doesn’t fix the issue. I’ll keep the issue open to hopefully be able to fix this in a future release (maby with Strapi v4?)
PS: Returning the
importSingleConfig
shouldn’t be needed, as theimportAllConfig
is used only to execute the import and won’t need to return anything.