Skip to content
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

Set robots to no crawl on test environment #81

Merged
merged 1 commit into from Aug 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions Dockerfile
Expand Up @@ -9,8 +9,6 @@ RUN npm ci && \
rm -rf ./.cache ./src/assets package-lock.json && \
find ./src -type f -name '*.spec.js' -delete

ENV NODE_ENV=production

EXPOSE 3000

CMD ["npm", "start"]
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -16,7 +16,7 @@
"eslint": "eslint '**/*.js' --ignore-pattern public/",
"eslint:report": "npm run eslint -- -f json -o reports/eslint.xml",
"stylelint": "stylelint 'src/assets/**/*.scss'",
"test": "NODE_ENV=test && jest .*.spec.js --verbose",
"test": "export NODE_ENV=jest && jest .*.spec.js --verbose",
"test:watch": "npm run test -- --watch",
"test:coverage": "npm run test -- --coverage"
},
Expand Down
24 changes: 21 additions & 3 deletions src/app.js
Expand Up @@ -4,16 +4,21 @@ const morgan = require('morgan');
const compression = require('compression');
const minifyHTML = require('express-minify-html');

const { version } = require('../package.json');

const routes = require('./routes');

const environment = process.env.NODE_ENV || 'production';

const app = express();

/* istanbul ignore next */
if (process.env.NODE_ENV !== 'test') {
app.use(helmet());
if (process.env.NODE_ENV !== 'jest') {
app.use(morgan('combined'));
}

app.use(helmet());

app.use(compression());
app.set('view engine', 'pug');
app.set('views', './src/views');
Expand All @@ -31,7 +36,20 @@ app.use('/public', express.static('./src/public'));

app.use(routes);

app.get('/robots.txt', (req, res) => res.type('text/plain').send('User-agent: *\nDisallow:'));
app.get('/robots.txt', (req, res) => {
let robots = 'User-agent: *\nDisallow:';
if (environment === 'test') {
robots = 'User-agent: *\nDisallow: /';
}

res.type('text/plain').send(robots);
});

app.get('/version', (req, res) => res.json({
version,
environment,
viewCache: app.get('view cache'),
}));

app.get('/status', (req, res) => res.sendStatus(200));

Expand Down
11 changes: 11 additions & 0 deletions src/app.spec.js
Expand Up @@ -2,12 +2,23 @@ const request = require('supertest');

const app = require('./app');

const packageFile = require('../package.json');

it('gets robots', async () => {
const response = await request(app).get('/robots.txt');

expect(response.text).toBe('User-agent: *\nDisallow:');
});

it('gets version', async () => {
const response = await request(app).get('/version');

expect(response.body).toStrictEqual({
version: packageFile.version,
environment: 'jest',
});
});

it('gets health check', async () => {
const response = await request(app).get('/status');

Expand Down