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

Switch from Mocha to Ava for faster tests #289

Merged
merged 2 commits into from
Jan 6, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 38 additions & 42 deletions appengine/analytics/test/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

'use strict';

require(`../../../test/_setup`);

const express = require(`express`);
const path = require(`path`);
const proxyquire = require(`proxyquire`).noPreserveCache();
Expand Down Expand Up @@ -50,54 +52,48 @@ function getSample () {
};
}

describe(`appengine/analytics/app.js`, () => {
let sample;
test.beforeEach(stubConsole);
test.afterEach(restoreConsole);

beforeEach(() => {
sample = getSample();
test.cb(`should record a visit`, (t) => {
const sample = getSample();
const expectedResult = `Event tracked.`;

assert(sample.mocks.express.calledOnce);
assert(sample.app.listen.calledOnce);
assert.equal(sample.app.listen.firstCall.args[0], process.env.PORT || 8080);
});
request(sample.app)
.get(`/`)
.expect(200)
.expect((response) => {
t.is(response.text, expectedResult);
})
.end(t.end);
});

it(`should record a visit`, (done) => {
const expectedResult = `Event tracked.`;
test.cb(`should handle request error`, (t) => {
const sample = getSample();
const expectedResult = `request_error`;

request(sample.app)
.get(`/`)
.expect(200)
.expect((response) => {
assert.equal(response.text, expectedResult);
})
.end(done);
});
sample.mocks.request.post.onFirstCall().callsArgWith(2, expectedResult);

it(`should handle request error`, (done) => {
const expectedResult = `request_error`;

sample.mocks.request.post.onFirstCall().callsArgWith(2, expectedResult);
request(sample.app)
.get(`/`)
.expect(500)
.expect((response) => {
t.is(response.text, `${expectedResult}\n`);
})
.end(t.end);
});

request(sample.app)
.get(`/`)
.expect(500)
.expect((response) => {
assert.equal(response.text, expectedResult + `\n`);
})
.end(done);
test.cb(`should handle track error`, (t) => {
const sample = getSample();
sample.mocks.request.post.onFirstCall().callsArgWith(2, null, {
statusCode: 400
});

it(`should handle track error`, (done) => {
sample.mocks.request.post.onFirstCall().callsArgWith(2, null, {
statusCode: 400
});

request(sample.app)
.get('/')
.expect(500)
.expect((response) => {
assert.notEqual(response.text.indexOf('Error: Tracking failed'), -1);
})
.end(done);
});
request(sample.app)
.get('/')
.expect(500)
.expect((response) => {
t.true(response.text.includes('Error: Tracking failed'));
})
.end(t.end);
});
2 changes: 1 addition & 1 deletion appengine/bower/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"scripts": {
"postinstall": "bower install --config.interactive=false",
"test": "cd ..; npm run t -- appengine/analytics/test/*.test.js"
"test": "cd ..; npm run t -- appengine/bower/test/*.test.js"
},
"dependencies": {
"bower": "1.8.0",
Expand Down
34 changes: 14 additions & 20 deletions appengine/bower/test/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

'use strict';

require(`../../../test/_setup`);

const express = require(`express`);
const path = require(`path`);
const proxyquire = require(`proxyquire`).noPreserveCache();
Expand All @@ -38,26 +40,18 @@ function getSample () {
};
}

describe(`appengine/bower/server.js`, () => {
let sample;

beforeEach(() => {
sample = getSample();
test.beforeEach(stubConsole);
test.afterEach(restoreConsole);

assert(sample.mocks.express.calledOnce);
assert(sample.app.listen.calledOnce);
assert.equal(sample.app.listen.firstCall.args[0], process.env.PORT || 8080);
});
test.cb(`should render a page`, (t) => {
const sample = getSample();
const expectedResult = `<h1>Hello World!</h1><p>Express.js + Bower on Google App Engine.</p>`;

it(`should render a page`, (done) => {
const expectedResult = `<h1>Hello World!</h1><p>Express.js + Bower on Google App Engine.</p>`;

request(sample.app)
.get(`/`)
.expect(200)
.expect((response) => {
assert.notEqual(response.text.indexOf(expectedResult), -1);
})
.end(done);
});
request(sample.app)
.get(`/`)
.expect(200)
.expect((response) => {
t.true(response.text.includes(expectedResult));
})
.end(t.end);
});
2 changes: 1 addition & 1 deletion appengine/cloudsql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"node": ">=4.3.2"
},
"scripts": {
"test": "cd ..; npm run t -- appengine/analytics/test/*.test.js"
"test": "cd ..; npm run t -- appengine/cloudsql/test/*.test.js"
},
"dependencies": {
"express": "4.14.0",
Expand Down
115 changes: 59 additions & 56 deletions appengine/cloudsql/test/createTables.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

'use strict';

require(`../../../test/_setup`);

const path = require(`path`);
const proxyquire = require(`proxyquire`).noPreserveCache();

Expand Down Expand Up @@ -54,64 +56,65 @@ function getSample () {
};
}

describe(`appengine/cloudsql/createTables.js`, () => {
it(`should record a visit`, (done) => {
const sample = getSample();
const expectedResult = `created visits table!`;

assert(sample.mocks.prompt.start.calledOnce);
assert(sample.mocks.prompt.get.calledOnce);
assert.deepEqual(sample.mocks.prompt.get.firstCall.args[0], [
`user`,
`password`,
`database`
]);

setTimeout(() => {
const uri = `mysql://${sample.mocks.config.user}:${sample.mocks.config.password}@127.0.0.1:3306/${sample.mocks.config.database}`;
assert.deepEqual(sample.mocks.mysql.createConnection.firstCall.args, [uri]);
assert(console.log.calledWith(expectedResult));
done();
}, 10);
});
test.beforeEach(stubConsole);
test.afterEach(restoreConsole);

test.cb.serial(`should record a visit`, (t) => {
const sample = getSample();
const expectedResult = `created visits table!`;

t.true(sample.mocks.prompt.start.calledOnce);
t.true(sample.mocks.prompt.get.calledOnce);
t.deepEqual(sample.mocks.prompt.get.firstCall.args[0], [
`user`,
`password`,
`database`
]);

setTimeout(() => {
const uri = `mysql://${sample.mocks.config.user}:${sample.mocks.config.password}@127.0.0.1:3306/${sample.mocks.config.database}`;
t.deepEqual(sample.mocks.mysql.createConnection.firstCall.args, [uri]);
t.true(console.log.calledWith(expectedResult));
t.end();
}, 10);
});

test.cb.serial(`should handle prompt error`, (t) => {
const error = new Error(`error`);
const sample = getSample();

it(`should handle prompt error`, (done) => {
const expectedResult = `createTables_prompt_error`;
const sample = getSample();

proxyquire(SAMPLE_PATH, {
mysql: sample.mocks.mysql,
prompt: {
start: sinon.stub(),
get: sinon.stub().callsArgWith(1, expectedResult)
}
});

setTimeout(() => {
assert(console.error.calledWith(expectedResult));
done();
}, 10);
proxyquire(SAMPLE_PATH, {
mysql: sample.mocks.mysql,
prompt: {
start: sinon.stub(),
get: sinon.stub().yields(error)
}
});

it(`should handle insert error`, (done) => {
const expectedResult = `createTables_insert_error`;
const sample = getSample();

const connectionMock = {
query: sinon.stub().callsArgWith(1, expectedResult),
end: sinon.stub()
};

proxyquire(SAMPLE_PATH, {
mysql: {
createConnection: sinon.stub().returns(connectionMock)
},
prompt: sample.mocks.prompt
});

setTimeout(() => {
assert(console.error.calledWith(expectedResult));
done();
}, 10);
setTimeout(() => {
t.true(console.error.calledWith(error));
t.end();
}, 10);
});

test.cb.serial(`should handle insert error`, (t) => {
const error = new Error(`error`);
const sample = getSample();

const connectionMock = {
query: sinon.stub().yields(error),
end: sinon.stub()
};

proxyquire(SAMPLE_PATH, {
mysql: {
createConnection: sinon.stub().returns(connectionMock)
},
prompt: sample.mocks.prompt
});

setTimeout(() => {
t.true(console.error.calledWith(error));
t.end();
}, 10);
});
Loading