Skip to content

add excludeEnvVariables option with tests #3

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ The following configuration options are available:
{
theme: 'okaidia', // The prismjs theme to use
disabledForXHR: true // Disable the middleware for XHR requests
excludeEnvVariables: [], // Name of environment variables to exclude
disableSourceMapSupport: false // Disables support for sourcemaps
}
```
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const defaultOptions = {
disabledForXHR: true,
disableSourceMapSupport: false,
excludeEnvVariables: [],
theme: 'okaidia'
};

Expand Down
7 changes: 4 additions & 3 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ function parseStack(opts, error) {
return results.then(r => r.filter(i => i));
}

function getEnvironment() {
return util.toKeyValueList(process.env);
function getEnvironment(opts) {
const environment = util.toKeyValueList(process.env);
return environment.filter(v => opts.excludeEnvVariables.indexOf(v.key) < 0);
}

function getGlobals() {
Expand Down Expand Up @@ -167,7 +168,7 @@ module.exports = function main(opts, error, req) {
headers: getHeaders(req),
request: getRequest(req),
stack,
environment: getEnvironment(),
environment: getEnvironment(opts),
globals: getGlobals(),
process: getProcess()
};
Expand Down
88 changes: 78 additions & 10 deletions tests/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,35 @@ const stack = require('../index').express;
let app;
let server;

beforeEach(done => {
app = express();
function checkBody(body) {
const html = cheerio.load(body);

server = app.listen(3000, done);
});
expect(html('title').text()).to.contain('Exception Page');
}

afterEach(() => {
server.close();
});
function checkEnvVarsInclude(body, envVar) {
const html = cheerio.load(body);

function checkBody(body) {
expect(html('div.environment').text()).to.contain(envVar);
}

function checkEnvVarsExclude(body, envVar) {
const html = cheerio.load(body);

expect(html('title').text()).to.contain('Exception Page');
expect(html('div.environment').text()).to.not.contain(envVar);
}

describe('#stack', () => {
describe('#stack with default config', () => {
beforeEach(done => {
app = express();

server = app.listen(3000, done);
});

afterEach(() => {
server.close();
});

it('fails when NODE_ENV="production"', () => {
process.env.NODE_ENV = 'production';

Expand Down Expand Up @@ -67,3 +79,59 @@ describe('#stack', () => {
});
});
});

describe('#stack with custom excludeEnvVariables config', () => {
const testEnvVarName = 'TEST_ENV_VAR';

beforeEach(done => {
process.env[testEnvVarName] = 'Hello World';

app = express();
server = app.listen(3000, done);
});

afterEach(() => {
server.close();
});

it('has env variable when not excluded', done => {
app.use((req, res, next) => {
setImmediate(() => {
next(new TypeError('Hello World'));
});
});
app.use(stack());

request('http://localhost:3000/', (error, response, body) => {
expect(response.statusCode).to.equal(500);

checkBody(body);
checkEnvVarsInclude(body, testEnvVarName);
done();
});
});

it('miss env variable when excluded', done => {
const excluded = [];
excluded.push(testEnvVarName);

app.use((req, res, next) => {
setImmediate(() => {
next(new TypeError('Hello World'));
});
});
app.use(
stack({
excludeEnvVariables: excluded
})
);

request('http://localhost:3000/', (error, response, body) => {
expect(response.statusCode).to.equal(500);

checkBody(body);
checkEnvVarsExclude(body, testEnvVarName);
done();
});
});
});
89 changes: 78 additions & 11 deletions tests/koa.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,37 @@ const stack = require('../index').koa;
let app;
let server;

beforeEach(done => {
app = new Koa();
function checkBody(body) {
const html = cheerio.load(body);

app.use(stack());
expect(html('title').text()).to.contain('Exception Page');
}

server = app.listen(3000, done);
});
function checkEnvVarsInclude(body, envVar) {
const html = cheerio.load(body);

afterEach(() => {
server.close();
});
expect(html('div.environment').text()).to.contain(envVar);
}

function checkBody(body) {
function checkEnvVarsExclude(body, envVar) {
const html = cheerio.load(body);

expect(html('title').text()).to.contain('Exception Page');
expect(html('div.environment').text()).to.not.contain(envVar);
}

describe('#stack', () => {
describe('#stack with default config', () => {
beforeEach(done => {
app = new Koa();

app.use(stack());

server = app.listen(3000, done);
});

afterEach(() => {
server.close();
});

it('fails when NODE_ENV="production"', () => {
process.env.NODE_ENV = 'production';

Expand All @@ -52,3 +64,58 @@ describe('#stack', () => {
});
});
});

describe('#stack with custom excludeEnvVariables config', () => {
const testEnvVarName = 'TEST_ENV_VAR';

beforeEach(done => {
app = new Koa();
process.env[testEnvVarName] = 'Hello World';
done();
});

afterEach(() => {
server.close();
});

it('has env variable when not excluded', done => {
app.use(stack());
server = app.listen(3000);

app.use(async () => {
throw new TypeError('Hello World');
});

request('http://localhost:3000/', (error, response, body) => {
expect(response.statusCode).to.equal(500);

checkBody(body);
checkEnvVarsInclude(body, testEnvVarName);
done();
});
});

it('miss env variable when excluded', done => {
const excluded = [];
excluded.push(testEnvVarName);

app.use(
stack({
excludeEnvVariables: excluded
})
);
server = app.listen(3000);

app.use(async () => {
throw new TypeError('Hello World');
});

request('http://localhost:3000/', (error, response, body) => {
expect(response.statusCode).to.equal(500);

checkBody(body);
checkEnvVarsExclude(body, testEnvVarName);
done();
});
});
});