Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage with jest unstable #17

Closed
derweili opened this issue Sep 21, 2021 · 6 comments
Closed

Usage with jest unstable #17

derweili opened this issue Sep 21, 2021 · 6 comments
Labels
bug Something isn't working

Comments

@derweili
Copy link

I 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.

error.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);
      })
  });
@boazpoolman
Copy link
Member

Hi @derweili!
What an interesting usecase for this plugin. Haven't used the plugin like this myself yet, but it seems like it fits your purpose great!

The importOnBootstrap setting will simply run the importAllConfig service, just as it would when importing from the admin panel. This service is asynchronous, but is used with await so should be OK.

Though I did find that inside this service the plugin will rerun importSingleConfig for each config, though this is also a asynchronous method which is not used with await. Maby this fixes the issue?

Are you in the position to test this with your setup as described above? If so I could create a PR for you to test.

@boazpoolman boazpoolman added the bug Something isn't working label Oct 15, 2021
@derweili derweili reopened this Oct 15, 2021
@derweili
Copy link
Author

@boazpoolman yes I can test it again. What do I have to do?

Install a newer version of this plugin?

@boazpoolman
Copy link
Member

boazpoolman commented Oct 16, 2021

@derweili I've created a PR in which this issue is hopefully fixed.

You can use it in your project like so:

yarn add boazpoolman/strapi-plugin-config-sync#pull/23/head
npm install boazpoolman/strapi-plugin-config-sync#pull/23/head

@derweili
Copy link
Author

@boazpoolman I just testet the PR, unfortunately I still get the error.

I also sometimes (very rarely) get an error when initiating the Strapi instance. But because this only happend two times, I wasn't able to verify if this is related to this plugin.

Don't you have to return this await importSingleConfig function?
https://github.com/boazpoolman/strapi-plugin-config-sync/pull/23/files#diff-e487fde79a6fb1723e2b7bcb2fd3e62ba4148f537b0d9cd725ecacd2bc6f57a0R160
Although this also does not solve the error, I tried this already.

@boazpoolman
Copy link
Member

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 the importAllConfig is used only to execute the import and won't need to return anything.

@boazpoolman
Copy link
Member

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:

# Using yarn
yarn cs import -y && [test-command]

# Using npm
npm run cs import -y && [test-command]

Released in v1.0.0-alpha.1 of strapi-plugin-config-sync.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants