This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //can choose here whether to test in-process or against external URL | |
| const app = express(); | |
| app.get('/user', function(req, res) { | |
| res.status(200).json({ name: 'john' }); | |
| }); | |
| request(app) | |
| .get('/user') | |
| .expect('Content-Type', /json/) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //this test is fast (no DB) and we're tagging it correspondigly | |
| //now the user/CI can run it frequently | |
| describe('Order service', function() { | |
| describe('Add new order #cold-test #sanity', function() { | |
| it('Scenario - no currency was supplied. Excpectation - Use the default currency #sanity', function() { | |
| //code logic here | |
| }); | |
| }); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| it.only("When no product name, it throws error 400", async() => { | |
| expect(addNewProduct)).to.eventually.throw(AppError).with.property('code', "InvalidInput"); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| it("When no product name, it throws error 400", async() => { | |
| let errorWeExceptFor = null; | |
| try { | |
| const result = await addNewProduct({name:'nest'});} | |
| catch (error) { | |
| expect(error.code).to.equal('InvalidInput'); | |
| errorWeExceptFor = error; | |
| } | |
| expect(errorWeExceptFor).not.to.be.null; | |
| //if this assertion fails, the tests results/reports will only show |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| it("When updating site name, get successful confirmation", async () => { | |
| //test is adding a fresh new records and acting on the records only | |
| const siteUnderTest = await SiteService.addSite({ | |
| name: "siteForUpdateTest" | |
| }); | |
| const updateNameResult = await SiteService.changeName(siteUnderTest, "newName"); | |
| expect(updateNameResult).to.be(true); | |
| }); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| before(() => { | |
| //adding sites and admins data to our DB. Where is the data? outside. At some external json or migration framework | |
| await DB.AddSeedDataFromJson('seed.json'); | |
| }); | |
| it("When updating site name, get successful confirmation", async () => { | |
| //I know that site name "portal" exists - I saw it in the seed files | |
| const siteToUpdate = await SiteService.getSiteByName("Portal"); | |
| const updateNameResult = await SiteService.changeName(siteToUpdate, "newName"); | |
| expect(updateNameResult).to.be(true); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require('mocha-testcheck').install(); | |
| const {expect} = require('chai'); | |
| const faker = require('faker'); | |
| describe('Product service', () => { | |
| describe('Adding new', () => { | |
| //this will run 100 times with different random properties | |
| check.it('Add new product with random yet valid properties, always successful', | |
| gen.int, gen.string, (id, name) => { | |
| expect(addNewProduct(id, name).status).to.equal('approved'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| it("Better: When adding new valid product, get successful confirmation", async () => { | |
| const addProductResult = addProduct(faker.commerce.productName(), faker.random.number()); | |
| //Generated random input: {'Sleek Cotton Computer', 85481} | |
| expect(addProductResult).to.be.true; | |
| //Test failed, the random input triggered some path we never planned for. | |
| //We discovered a bug early! | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const addProduct = (name, price) =>{ | |
| const productNameRegexNoSpace = /^\S*$/;//no white-space allowd | |
| if(!productNameRegexNoSpace.test(name)) | |
| return false;//this path never reached due to dull input | |
| //some logic here | |
| return true; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| it("When a valid product is about to be deleted, ensure an email is sent", async () => { | |
| //Assume we already added here a product | |
| const spy = sinon.spy(Emailer.prototype, "sendEmail"); | |
| new ProductService().deletePrice(theProductWeJustAdded); | |
| //hmmm OK: we deal with internals? Yes, but as a side effect of testing the requirements (sending an email) | |
| }); |