documentation icon indicating copy to clipboard operation
documentation copied to clipboard

[Bug]: unit testing > Testing auth endpoint controller

Open colideum opened this issue 2 years ago • 1 comments

Link to the documentation page or resource

https://docs.strapi.io/developer-docs/latest/guides/unit-testing.html#testing-auth-endpoint-controller

Describe the bug

  • Installed Strapi from the scratch.

  • Installed requirements for unit testing from documentation

  • Created user test cases (copy/paste from documentation). Results: ` FAIL tests/app.test.js (6.566 s) √ strapi is defined (2 ms) × should login user and return jwt token (126 ms) × should return users data for authenticated user (1 ms)

    ● should login user and return jwt token

    ValidationError: This attribute must be unique

    13 | it("should login user and return jwt token", async () => {
    14 |   /** Creates a new user and save it to the database */
    

    15 | await strapi.plugins["users-permissions"].services.user.add({ | ^ 16 | ...mockUserData, 17 | }); 18 |

    at handleYupError (node_modules/@strapi/utils/lib/validators.js:67:9)
    at node_modules/@strapi/utils/lib/validators.js:79:7
    at Object.create (node_modules/@strapi/strapi/lib/services/entity-service/index.js:127:23)
    at Object.<anonymous> (node_modules/@strapi/strapi/lib/services/entity-service/index.js:313:20)
    at Object.<anonymous> (tests/user/index.js:15:3)
    

    ● should return users data for authenticated user

    Model role not found

    34 | it('should return users data for authenticated user', async () => {
    35 |   /** Gets the default user role */
    

    36 | const defaultRole = await strapi.query('role', 'users-permissions').findOne({}, []); | ^ 37 | 38 | const role = defaultRole ? defaultRole.id : null; 39 |

    at Database.query (node_modules/@strapi/database/lib/index.js:44:13)
    at Strapi.query (node_modules/@strapi/strapi/lib/Strapi.js:534:20)
    at Object.query (tests/user/index.js:36:36)
    

Test Suites: 1 failed, 1 total Tests: 2 failed, 1 passed, 3 total Snapshots: 0 total Time: 6.792 s, estimated 7 s Ran all test suites. `

Additional context

Strapi: 4.3.8 node: v16.15.0

Suggested improvements or fixes

expect to be successful tests

Related issue(s)/PR(s)

No response

colideum avatar Sep 19 '22 13:09 colideum

Hello @colideum. I am sorry that you are having problems with unit testing. The documentation for unit testing is not necessarily up-to-date for v4, as noted at the top of the guide. You might check out this forum post which has some v4 examples.

stb13579 avatar Sep 19 '22 13:09 stb13579