the-road-to-graphql / fullstack-apollo-express-postgresql-boilerplate

💥 A sophisticated GraphQL with Apollo, Express and PostgreSQL boilerplate project.
https://roadtoreact.com
MIT License
1.2k stars 265 forks source link

run test starting server #45

Open pmosconi opened 5 years ago

pmosconi commented 5 years ago

ISSUE: Server: NPM Scripts for Testing #7

I moved database seeding to a dedicated tests/setup.js file from src/index.js. src/index.js now exports httpServer wich will be imported by setup.js and starts it only when not in test mode. tests/user.spec.js imports setup.js initializes the database in the before() function that is executed at the very beginning of the suite; likewise, the after() function terminates the process in order to prevent execution hanging for ever. I also added a timeout in npm run test command to take into account the delay caused by seeding the database and set an IS_TEST environment test that is used to identify the enviroment (IMHO cleaner than testing for the existence of the test database name which can be now declared in .env).

To make all work in my environment I had to switch from pg to mssql and I apologise for this additional change being included in the PR, but this way I am sure this is a working project. To revert, just replace tedious with pg in package.json and replace 'mssql' with 'postgres' in models/index.js. I also added the host parameter to account for a remote database (such as the one I am using): it is defined .env as well. Since the default is localhost, I guess that if not defined it will be ignored by Sequelize constructor.

Other changes, again not required: .babelrc: useful for debug .vscode/launch.json: debug startup for server and tests respectively