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
Strapi unit tests failing #7811
Comments
Got the same output. Each of these changes make the test pass:
So I would assume something with the database configuration is not working as expected. Any thoughts from some more experienced users about this? |
After some checking it seems strapi hates it if you have database.json in your particular config/env/ folder. I found for me, changing it to database.js matching the same format as the default database.js in the default config makes the tests run. The other issue now is the sqlite db that's generated (using sqlite for my test db) doesn't get deleted or obey my specified file name. It generates data.db as opposed to what I specified |
@kasonde please try to remove all the options from {
"defaultConnection": "default",
"connections": {
"default": {
"connector": "bookshelf",
"settings": {
"client": "sqlite",
"filename": ".tmp/test.db"
}
}
}
}
This would make test run on you current database which can lead to serious problems. |
Same issue solved removing options as @qunabu said! |
id do all thing that @kasonde said. databse: mysql @derrickmehaffy its look like a hotfix |
@marefati110 what strapi version you have ? |
@qunabu |
I've managed to create and example for latest strapi |
The repo from @qunabu works great, I think those files should be on Strapi by default, it was a pain in the ass to debug/start testing |
@saulmmendoza I agree, I'm trying strapi but these many errors are so disappointing ☹ |
and it still doesn't work! please, explain how you do to test your app! |
I've noticed one thing in my CI/CD environment. The same tests are failing for one of the gitlab runner and pass with other. The difference between those is they are the same runners based on One that pass has more resources (cores and ram) then one that occasionally fails. Some of the packages seems to be not waiting for a callback when you have less resources, or CI/CD needs really great machine to run them. Anyway this can be address better in terms of feedback from the framework. |
Managed to make it work with
by installing |
In my experience I followed "Unit testing" Strapi docs and I received the mentioned error at the same row:
but looking at error stack I see a clear error during the database initialization:
🥇 I resolved this installing some missing dependencies (not mentioned on docs): here the other configurations:
my Strapi version is |
Running Strapi 3.5.4, I think I figured it out. I have the exact same error when using Postgres/bookshelf. Strapi runs completely fine normally with postgres. There aren't any missing modules. Using the exact same database config for tests fails on Strapi().load(), and removing .load() doesn't work for me. If I change the database connector to Mongo/mongoose, tests run fine with no error. So I threw it all in Docker and watched the output while it tried running the tests, and right before it fails I got a bunch of max connections errors from the Postgres server. I upped the max connections to 200 and it worked. So it seems that Strapi().load() opens too many connections to Postgres and then crashes when it's refused. |
I'm having the exact same problem:
Tried all proposed solutions but none seems to work. 🤔 UpdateI've figured out to work. I basically:
And now it seems working. Although the error messages are not clear at all. I wish somehow we could have clearer error messages but maybe it's a combination of Strapi + Jest |
@raulfdm could you share your #12 47.83 [2021-12-15T21:10:32.779Z] error bootstrap function in plugin "users-permissions" failed
#12 47.84 [2021-12-15T21:10:32.790Z] error KnexTimeoutError: Knex: Timeout acquiring a connection. The pool is probably full. Are you missing a .transacting(trx) call?
#12 47.84 at Client_SQLite3.acquireConnection (/srv/node_modules/knex/lib/client.js:348:26)
#12 47.84 at runNextTicks (internal/process/task_queues.js:58:5)
#12 47.84 at listOnTimeout (internal/timers.js:523:9)
#12 47.84 at processTimers (internal/timers.js:497:7)
#12 47.84 at Transaction.acquireConnection (/srv/node_modules/knex/lib/transaction.js:212:28)
#12 47.84 at Object.create (/srv/node_modules/strapi-connector-bookshelf/lib/queries.js:58:14)
#12 47.84 at Object.create (/srv/node_modules/strapi-database/lib/queries/helpers.js:15:18)
#12 47.84 at async Promise.all (index 178)
#12 47.84 at Object.updatePermissions (/srv/node_modules/strapi-plugin-users-permissions/services/UsersPermissions.js:317:7)
#12 47.84 at Object.<anonymous>.module.exports (/srv/node_modules/strapi-plugin-users-permissions/config/functions/bootstrap.js:214:3)
#12 47.84 at async Promise.all (index 6)
#12 47.84 at Strapi.runLifecyclesFunctions (/srv/node_modules/strapi/lib/Strapi.js:443:5)
#12 47.84 at Strapi.load (/srv/node_modules/strapi/lib/Strapi.js:379:5)
#12 47.84 at setupStrapi (/srv/test/helpers/strapi.js:9:5)
#12 47.84 at /srv/test/cron.test.js:9:5
#12 47.85 ● process.exit called with "1"
#12 47.85
#12 47.85 7 | if (!instance) {
#12 47.85 8 | /** the following code in copied from `./node_modules/strapi/lib/Strapi.js` */
#12 47.85 > 9 | await Strapi().load();
#12 47.85 | ^
#12 47.85 10 | instance = strapi; // strapi is global now
#12 47.85 11 | await instance.app
#12 47.85 12 | .use(instance.router.routes()) // populate KOA routes
#12 47.85
#12 47.85 at Strapi.stop (node_modules/strapi/lib/Strapi.js:316:13)
#12 47.85 at node_modules/strapi/lib/Strapi.js:450:18
#12 47.85 at async Promise.all (index 6)
#12 47.85 at Strapi.runLifecyclesFunctions (node_modules/strapi/lib/Strapi.js:443:5)
#12 47.85 at Strapi.load (node_modules/strapi/lib/Strapi.js:379:5)
#12 47.85 at setupStrapi (test/helpers/strapi.js:9:5)
#12 47.85 at test/cron.test.js:9:5 My stripped out {
"scripts": {
"develop": "strapi develop --watch-admin",
"start": "strapi start",
"build": "strapi build",
"strapi": "strapi",
"test": "jest --forceExit --detectOpenHandles --verbose"
},
"dependencies": {
"applicationinsights": "^2.1.9",
"applicationinsights-native-metrics": "^0.0.6",
"knex": "^0.21.18",
"pg": "8.7.1",
"strapi": "3.6.8",
"strapi-admin": "3.6.8",
"strapi-connector-bookshelf": "3.6.8",
"strapi-plugin-content-manager": "3.6.8",
"strapi-plugin-content-type-builder": "3.6.8",
"strapi-plugin-email": "3.6.8",
"strapi-plugin-i18n": "3.6.8",
"strapi-plugin-sync-roles-permissions": "^0.4.3",
"strapi-plugin-upload": "3.6.8",
"strapi-plugin-users-permissions": "3.6.8",
"@strapi/provider-upload-local": "^4.0.0",
"strapi-provider-upload-azure-storage": "^1.1.3",
"strapi-utils": "3.6.8"
},
"engines": {
"node": ">=10.16.0 <=14.x.x",
"npm": ">=6.0.0"
},
"jest": {
"testPathIgnorePatterns": [
"/node_modules/",
".tmp",
".cache"
],
"testEnvironment": "node"
},
"devDependencies": {
"jest": "^27.4.5",
"sqlite3": "^5.0.2",
"supertest": "^6.1.6"
}
} I do have env overrides defined here and they are the same as suggested above (I tried multiple variations). |
So, I get the error above when I run tests during the docker build step via RUN npm run test But if I comment out Also it seems to work sometimes (1 out of 100 times) and other tests that do not require strapi setup work just fine. UPDATE: running UPDATE: running it like this works... RUN npm run test || true
RUN npm run test It crashes on the first run but works on the second run. It could be that |
The way that Strapi is being used to create test harness is just not good. I am writing a I think most issues starts from the fact that we are doing import StrapiStart from '@strapi/strapi/lib/commands/start';
await StrapiStart({}); Then start the testing as: This just starts strapi as usual, but within a test environment. I think this should work for setting up a server process as well with a production env set of course. The SQLite issueAs for the main sqlite question here, I have just also noticed this issue too. So documentation should say: path.resolve(process.env.PWD, env('DATABASE_FILENAME', '.tmp/data.db')) instead of path.join(__dirname, '..', '..', env('DATABASE_FILENAME', '.tmp/data.db')) This works fine if the database file is coming from |
To see what is happening do this: NODE_ENV=test yarn strapi dev It will spit out what testing suite is swallowing. |
I have published the package as strapi-plugin-vitest on npm, try it out, watch not working as expected, but can use a workaround with chokidar. add this to scripts section of package.json: |
I see that there are some test and it's documentation issues because there are breaking changes between version, and to be honest its impossible to maintain the documentation. There is no no focus on backward compatibility - what used to work in 4.2 is not working 4.3 and so on. @emahuni What you did is just great, kudos I'm maintaining this repository for both v3 and v4 of Strapi which is good starting point for some people that implement test in their strapi applications. https://github.com/qunabu/strapi-unit-test-example Can you guide me how to implement your code in this example, is it worth is. Maybe you would be able to create a Pull Request ? |
Yes that'd be great. Will pull soon. |
I'm honestly a bit confused as to how a simple working setup for Jest + Typescript isn't provided by default or not covered in the docs for v4. I've been searching for a while and still can't get things to work properly, and it all seems like way too much effort just to have some tests for my app (which should be a default). I'm very interested in what you're doing @emahuni ! Would love to be able to setup vitest, feels like it would be easier to setup and run if it was properly integrated. |
I am facing the same issue as you @QuentinLeCaignec. Using Typsecript with strapi v4 and been struggling for hours to set up simple unit tests with Jest. Following official docs leads nowhere.. Did you find a way to make it work? |
Not really sadly. I have a working jest + TS with ts-jest, but initialising the Strapi instance or testing an endpoint with supertest doesn't work well, and Jest keeps sending errors of unclosed connections. I am going to wait until V4 of Strapi is truly "stable" with proper test support. |
@qunabu tried to do what you did in your repo but it doesn't seem to work for me since I use Typescript. Are you aware of any solution? Cheers mate! |
Typescript testing https://github.com/qunabu/strapi-unit-test-example/tree/ver4ts |
@qunabu thank you so much mate, it's very helpful and got it to work thanks to you! Cheers |
Hello! As we have not received any new or updated information to reproduce this issue in the last 14 days we are marking this issue as closed. Should you have new information please feel free to respond and we will consider reopening it. If anyone else have updated information for this issue, please open up a new bug report and simply reference this closed bug report so that we can get any new information you may have. If you have questions please refer to the contributor's guide on opening issues. Thank you and have a great day! |
I'm having the same issue with the following code trying to use supertest and jest with typescript on v4 on 4.4.7
` Also, how is this a bug with low priority? Unit testing is a very important part of CI/CD pipelines and in many cases cant even deploy without it... since strapi oficially suports typescript i think this should be ranked as a higher priority |
I am experiencing a similar issue. I have followed the official documentation, but I find the process to be excessively convoluted when trying to set up a basic configuration, including dummy data and programmatically setting permissions for testing endpoints, among other things.
For versions:
|
Setting
in the package.json did solve this issue for me. |
The Strapi Unit Testing docs don't produce the expected results
I've attempted to write unit tests in strapi following the unit testing docs for the latest strapi version and to no avail. Unit tests fail when you attempt to test using an env that isn't the default environment strapi runs in.
Steps to reproduce the behavior
Expected behavior
Tests must pass
System
The text was updated successfully, but these errors were encountered: