question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Usage with jest unstable

See original GitHub issue

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.

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:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
boazpoolmancommented, Dec 8, 2021

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.

0reactions
boazpoolmancommented, Oct 22, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Your Jest Tests are Leaking Memory
Even if you don't use Jest, it's still useful to understand the process in case you need to debug memory leaks in other...
Read more >
Troubleshooting
Use this guide to resolve issues with Jest. Tests are Failing and You Don't Know Why. Try using the debugging support built into...
Read more >
javascript - Trouble mocking ES6 Module using jest. ...
I'm trying to mock the call to Notify to expect it to have been called and while this will run, it raises an...
Read more >
jest(1) — jest — Debian unstable
This can also be a JSON encoded value which Jest will use as configuration. [string]; --coverage: Indicates that test coverage information ...
Read more >
Jest Testing like a Pro - Tips and tricks
If the methods you want to test are asynchronous, be it callbacks, promises or async, it is not a problem with Jest. The...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found