Test backend with in-memory server in CI.
This commit is contained in:
parent
c4dd2dc6e3
commit
624aa34754
6 changed files with 199 additions and 41 deletions
|
@ -19,6 +19,7 @@
|
|||
"major": "npm version major -m ':bookmark: v%s' && npm run build",
|
||||
"test": "SEND_TEST_EMAILS=0 ./node_modules/.bin/mocha tests/index.js",
|
||||
"testall": "SEND_TEST_EMAILS=1 ./node_modules/.bin/mocha tests/index.js",
|
||||
"testci": "babel-node --presets '@babel/preset-env' scripts/testci.js",
|
||||
"clean": "rimraf dist",
|
||||
"prettier": "npx prettier --write 'src/**' 'tests/**'",
|
||||
"lint": "eslint --fix \"src/*.js\"",
|
||||
|
@ -70,6 +71,7 @@
|
|||
"jszip": "3.7.1",
|
||||
"mdast-util-to-string": "2",
|
||||
"mocha": "^8.3.2",
|
||||
"mongodb-memory-server": "^8.3.0",
|
||||
"mongoose": "^6.1.8",
|
||||
"mongoose-bcrypt": "^1.8.1",
|
||||
"mongoose-encryption": "^2.1.0",
|
||||
|
|
30
packages/backend/scripts/testci.js
Normal file
30
packages/backend/scripts/testci.js
Normal file
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
* Starts an in-memory database and a server before running tests.
|
||||
*/
|
||||
|
||||
import '../tests/env.js';
|
||||
import mongoose from 'mongoose';
|
||||
import { MongoMemoryServer } from 'mongodb-memory-server';
|
||||
import { loadSampleData, runTasks } from '../src/cli/lib';
|
||||
import { startApp } from '../src/app';
|
||||
import { spawn } from 'child_process';
|
||||
|
||||
(async () => {
|
||||
return MongoMemoryServer.create({ instance: { port: 27017 } });
|
||||
})()
|
||||
.then((mongoServer) => {
|
||||
mongoose.connect(mongoServer.getUri() + "freesewing", { useNewUrlParser: true });
|
||||
})
|
||||
.then(() => { runTasks({ reboot: true }) })
|
||||
.then(loadSampleData)
|
||||
.then(startApp)
|
||||
.then(() => {
|
||||
// Forward command-line args to test process.
|
||||
const args = ['run', 'test', '--'].concat(process.argv.slice(2));
|
||||
spawn('npm', args, { stdio: 'inherit' })
|
||||
.on('exit', function(code) {
|
||||
// Propagate exit code so that test failures are recognized.
|
||||
process.exit(code);
|
||||
});
|
||||
});
|
||||
|
|
@ -11,47 +11,54 @@ import routes from './routes'
|
|||
import path from 'path'
|
||||
import fs from 'fs'
|
||||
|
||||
// Verify configuration
|
||||
verifyConfig(config, chalk)
|
||||
export const connectToDb = () => {
|
||||
// Connecting to the database
|
||||
mongoose.Promise = global.Promise
|
||||
mongoose
|
||||
.connect(config.db.uri, {
|
||||
useNewUrlParser: true,
|
||||
})
|
||||
.then(() => {
|
||||
console.log(chalk.green('Successfully connected to the database'))
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(chalk.red('Could not connect to the database. Exiting now...'), err)
|
||||
process.exit()
|
||||
})
|
||||
};
|
||||
|
||||
// Start Express
|
||||
const app = express()
|
||||
export const startApp = () => {
|
||||
// Verify configuration
|
||||
verifyConfig(config, chalk)
|
||||
|
||||
// Load Express middleware
|
||||
for (let type of Object.keys(expressMiddleware)) expressMiddleware[type](app)
|
||||
// Start Express
|
||||
const app = express()
|
||||
|
||||
// Load Passport middleware
|
||||
for (let type of Object.keys(passportMiddleware)) passportMiddleware[type](passport)
|
||||
// Load Express middleware
|
||||
for (let type of Object.keys(expressMiddleware)) expressMiddleware[type](app)
|
||||
|
||||
// Load routes
|
||||
for (let type of Object.keys(routes)) routes[type](app, passport)
|
||||
// Load Passport middleware
|
||||
for (let type of Object.keys(passportMiddleware)) passportMiddleware[type](passport)
|
||||
|
||||
// Connecting to the database
|
||||
mongoose.Promise = global.Promise
|
||||
mongoose
|
||||
.connect(config.db.uri, {
|
||||
useNewUrlParser: true,
|
||||
})
|
||||
.then(() => {
|
||||
console.log(chalk.green('Successfully connected to the database'))
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(chalk.red('Could not connect to the database. Exiting now...'), err)
|
||||
process.exit()
|
||||
// Load routes
|
||||
for (let type of Object.keys(routes)) routes[type](app, passport)
|
||||
|
||||
// Catch-all route (Load index.html once instead of at every request)
|
||||
const index = fs.readFileSync(path.resolve(__dirname, 'landing', 'index.html'))
|
||||
app.get('/', async (req, res) => res
|
||||
.set('Content-Type', 'text/html')
|
||||
.status(200)
|
||||
.send(index)
|
||||
)
|
||||
|
||||
const port = process.env.PORT || 3000
|
||||
|
||||
app.listen(port, (err) => {
|
||||
if (err) console.error(chalk.red('Error occured'), err)
|
||||
if (process.env.NODE_ENV === 'development') console.log(chalk.yellow('> in development'))
|
||||
console.log(chalk.green(`> listening on port ${port}`))
|
||||
})
|
||||
|
||||
// Catch-all route (Load index.html once instead of at every request)
|
||||
const index = fs.readFileSync(path.resolve(__dirname, 'landing', 'index.html'))
|
||||
app.get('/', async (req, res) => res
|
||||
.set('Content-Type', 'text/html')
|
||||
.status(200)
|
||||
.send(index)
|
||||
)
|
||||
return app;
|
||||
};
|
||||
|
||||
const port = process.env.PORT || 3000
|
||||
|
||||
app.listen(port, (err) => {
|
||||
if (err) console.error(chalk.red('Error occured'), err)
|
||||
if (__DEV__) console.log(chalk.yellow('> in development'))
|
||||
console.log(chalk.green(`> listening on port ${port}`))
|
||||
})
|
||||
|
|
8
packages/backend/src/index.js
Normal file
8
packages/backend/src/index.js
Normal file
|
@ -0,0 +1,8 @@
|
|||
import { connectToDb, startApp } from './app'
|
||||
|
||||
connectToDb();
|
||||
|
||||
const app = startApp();
|
||||
|
||||
export default app;
|
||||
|
20
packages/backend/tests/env.js
Normal file
20
packages/backend/tests/env.js
Normal file
|
@ -0,0 +1,20 @@
|
|||
process.env.FS_BACKEND = "http://localhost:3000";
|
||||
process.env.FS_STATIC = "https://static.freesewing.org";
|
||||
process.env.FS_STORAGE = "/tmp/backendstorage";
|
||||
process.env.FS_MONGO_URI = "mongodb://localhost:27017/freesewing";
|
||||
process.env.FS_ENC_KEY = "longRandomStringHere";
|
||||
process.env.FS_STRAPI_HOST = "posts.freesewing.org";
|
||||
process.env.FS_STRAPI_PROTOCOL = "https";
|
||||
process.env.FS_STRAPI_PORT = "443";
|
||||
process.env.FS_STRAPI_USERNAME = "REPLACEME";
|
||||
process.env.FS_STRAPI_PASSWORD = "REPLACEME";
|
||||
process.env.FS_STRAPI_TMP = "/fs/ramdisk";
|
||||
process.env.FS_SMTP_USER = "smtpRelayUsername";
|
||||
process.env.FS_SMTP_PASS = "smtpRelayPassword";
|
||||
process.env.FS_SMTP_HOST = "smtp.relay.somedomain.com";
|
||||
process.env.FS_GITHUB_TOKEN = "githubTokenHere";
|
||||
process.env.FS_GITHUB_CLIENT_ID = "githubClientIdHere";
|
||||
process.env.FS_GITHUB_CLIENT_SECRET = "githubClientSecretHere";
|
||||
process.env.FS_GOOGLE_CLIENT_ID = "googleClientIdHere";
|
||||
process.env.FS_GOOGLE_CLIENT_SECRET = "googleClientSecretHere";
|
||||
process.env.FS_JWT_ISSUER = "freesewing.org";
|
Loading…
Add table
Add a link
Reference in a new issue