diff --git a/sections/errorhandling/centralizedhandling.md b/sections/errorhandling/centralizedhandling.md index b307fcdf..1d184626 100644 --- a/sections/errorhandling/centralizedhandling.md +++ b/sections/errorhandling/centralizedhandling.md @@ -13,7 +13,7 @@ Without one dedicated object for error handling, greater are the chances of impo // DAL layer, we don't handle errors here DB.addDocument(newCustomer, (error, result) => { if (error) - throw new Error("Great error explanation comes here", other useful parameters) + throw new Error('Great error explanation comes here', other useful parameters) }); // API route code, we catch both sync and async errors and forward to the middleware @@ -45,7 +45,7 @@ app.use(async (err, req, res, next) => { // DAL layer, we don't handle errors here DB.addDocument(newCustomer, (error: Error, result: Result) => { if (error) - throw new Error("Great error explanation comes here", other useful parameters) + throw new Error('Great error explanation comes here', other useful parameters) }); // API route code, we catch both sync and async errors and forward to the middleware diff --git a/sections/errorhandling/operationalvsprogrammererror.md b/sections/errorhandling/operationalvsprogrammererror.md index fa98057b..69ccee2c 100644 --- a/sections/errorhandling/operationalvsprogrammererror.md +++ b/sections/errorhandling/operationalvsprogrammererror.md @@ -11,7 +11,7 @@ Distinguishing the following two error types will minimize your app downtime and ```javascript // marking an error object as operational -const myError = new Error("How can I add new product when no value provided?"); +const myError = new Error('How can I add new product when no value provided?'); myError.isOperational = true; // or if you're using some centralized error factory (see other examples at the bullet "Use only the built-in Error object") @@ -25,7 +25,7 @@ class AppError { } }; -throw new AppError(errorManagement.commonErrors.InvalidInput, "Describe here what happened", true); +throw new AppError(errorManagement.commonErrors.InvalidInput, 'Describe here what happened', true); ``` @@ -52,7 +52,7 @@ export class AppError extends Error { } // marking an error object as operational (true) -throw new AppError(errorManagement.commonErrors.InvalidInput, "Describe here what happened", true); +throw new AppError(errorManagement.commonErrors.InvalidInput, 'Describe here what happened', true); ``` diff --git a/sections/errorhandling/testingerrorflows.md b/sections/errorhandling/testingerrorflows.md index d079d2e3..552ca1f5 100644 --- a/sections/errorhandling/testingerrorflows.md +++ b/sections/errorhandling/testingerrorflows.md @@ -10,11 +10,11 @@ Testing ‘happy’ paths is no better than testing failures. Good testing code Javascript ```javascript -describe("Facebook chat", () => { - it("Notifies on new chat message", () => { +describe('Facebook chat', () => { + it('Notifies on new chat message', () => { const chatService = new chatService(); chatService.participants = getDisconnectedParticipants(); - expect(chatService.sendMessage.bind({ message: "Hi" })).to.throw(ConnectionError); + expect(chatService.sendMessage.bind({ message: 'Hi' })).to.throw(ConnectionError); }); }); ``` @@ -24,11 +24,11 @@ describe("Facebook chat", () => { Typescript ```typescript -describe("Facebook chat", () => { - it("Notifies on new chat message", () => { +describe('Facebook chat', () => { + it('Notifies on new chat message', () => { const chatService = new chatService(); chatService.participants = getDisconnectedParticipants(); - expect(chatService.sendMessage.bind({ message: "Hi" })).to.throw(ConnectionError); + expect(chatService.sendMessage.bind({ message: 'Hi' })).to.throw(ConnectionError); }); }); ``` @@ -40,11 +40,11 @@ describe("Facebook chat", () => { Javascript ```javascript -it("Creates new Facebook group", () => { +it('Creates new Facebook group', () => { const invalidGroupInfo = {}; return httpRequest({ method: 'POST', - uri: "facebook.com/api/groups", + uri: 'facebook.com/api/groups', resolveWithFullResponse: true, body: invalidGroupInfo, json: true @@ -61,12 +61,12 @@ it("Creates new Facebook group", () => { Typescript ```typescript -it("Creates new Facebook group", async () => { +it('Creates new Facebook group', async () => { let invalidGroupInfo = {}; try { const response = await httpRequest({ method: 'POST', - uri: "facebook.com/api/groups", + uri: 'facebook.com/api/groups', resolveWithFullResponse: true, body: invalidGroupInfo, json: true diff --git a/sections/errorhandling/useonlythebuiltinerror.md b/sections/errorhandling/useonlythebuiltinerror.md index 8c59285a..b4b96fe4 100644 --- a/sections/errorhandling/useonlythebuiltinerror.md +++ b/sections/errorhandling/useonlythebuiltinerror.md @@ -9,7 +9,7 @@ The permissive nature of JavaScript along with its variety of code-flow options ```javascript // throwing an Error from typical function, whether sync or async if(!productToAdd) - throw new Error("How can I add new product when no value provided?"); + throw new Error('How can I add new product when no value provided?'); // 'throwing' an Error from EventEmitter const myEmitter = new MyEmitter(); @@ -20,7 +20,7 @@ const addProduct = async (productToAdd) => { try { const existingProduct = await DAL.getProduct(productToAdd.id); if (existingProduct !== null) { - throw new Error("Product already exists!"); + throw new Error('Product already exists!'); } } catch (err) { // ... @@ -33,7 +33,7 @@ const addProduct = async (productToAdd) => { ```javascript // throwing a string lacks any stack trace information and other important data properties if(!productToAdd) - throw ("How can I add new product when no value provided?"); + throw ('How can I add new product when no value provided?'); ``` ### Code example – doing it even better @@ -56,7 +56,7 @@ module.exports.AppError = AppError; // client throwing an exception if(user == null) - throw new AppError(commonErrors.resourceNotFound, commonHTTPErrors.notFound, "further explanation", true) + throw new AppError(commonErrors.resourceNotFound, commonHTTPErrors.notFound, 'further explanation', true) ``` @@ -85,7 +85,7 @@ export class AppError extends Error { // client throwing an exception if(user == null) - throw new AppError(commonErrors.resourceNotFound, commonHTTPErrors.notFound, "further explanation", true) + throw new AppError(commonErrors.resourceNotFound, commonHTTPErrors.notFound, 'further explanation', true) ``` diff --git a/sections/production/createmaintenanceendpoint.md b/sections/production/createmaintenanceendpoint.md index 405ad5bc..2db04a8b 100644 --- a/sections/production/createmaintenanceendpoint.md +++ b/sections/production/createmaintenanceendpoint.md @@ -27,7 +27,7 @@ router.get('/ops/heapdump', (req, res, next) => { heapdump.writeSnapshot((err, filename) => { console.log('heapdump file is ready to be sent to the caller', filename); - fs.readFile(filename, "utf-8", (err, data) => { + fs.readFile(filename, 'utf-8', (err, data) => { res.end(data); }); }); diff --git a/sections/production/setnodeenv.md b/sections/production/setnodeenv.md index 00c46bd6..4c721fec 100644 --- a/sections/production/setnodeenv.md +++ b/sections/production/setnodeenv.md @@ -18,7 +18,7 @@ $ node ```javascript // Reading the environment variable using code -if (process.env.NODE_ENV === "production") +if (process.env.NODE_ENV === 'production') useCaching = true; ``` diff --git a/sections/projectstructre/separateexpress.md b/sections/projectstructre/separateexpress.md index 16b48b1d..679b2a8b 100644 --- a/sections/projectstructre/separateexpress.md +++ b/sections/projectstructre/separateexpress.md @@ -13,8 +13,8 @@ The latest Express generator comes with a great practice that is worth to keep - ```javascript const app = express(); app.use(bodyParser.json()); -app.use("/api/events", events.API); -app.use("/api/forms", forms); +app.use('/api/events', events.API); +app.use('/api/forms', forms); ``` ### Code example: Server network declaration, should reside in /bin/www diff --git a/sections/security/sandbox.md b/sections/security/sandbox.md index 366f8f03..34539566 100644 --- a/sections/security/sandbox.md +++ b/sections/security/sandbox.md @@ -11,22 +11,22 @@ As a rule of thumb, one should run his own JavaScript files only. Theories aside ### Code example - Using Sandbox library to run code in isolation ```javascript -const Sandbox = require("sandbox"); +const Sandbox = require('sandbox'); const s = new Sandbox(); -s.run("lol)hai", (output) => { +s.run('lol)hai', (output) => { console.log(output); //output='Syntax error' }); // Example 4 - Restricted code -s.run("process.platform", (output) => { +s.run('process.platform', (output) => { console.log(output); //output=Null }); // Example 5 - Infinite loop -s.run("while (true) {}", (output) => { +s.run('while (true) {}', (output) => { console.log(output); //output='Timeout' }); diff --git a/sections/security/validation.md b/sections/security/validation.md index 837a1d15..e37cdd37 100644 --- a/sections/security/validation.md +++ b/sections/security/validation.md @@ -30,7 +30,7 @@ Validation is about being very explicit on what payload our app is willing to ac ### Example - Validating an entity using JSON-Schema ```javascript -const JSONValidator = require("jsonschema").Validator; +const JSONValidator = require('jsonschema').Validator; class Product { @@ -52,7 +52,7 @@ class Product { ```javascript // The validator is a generic middleware that gets the entity it should validate and takes care to return // HTTP status 400 (Bad Request) should the body payload validation fail -router.post("/" , **validator(Product.validate)**, async (req, res, next) => { +router.post('/' , **validator(Product.validate)**, async (req, res, next) => { // route handling code goes here }); diff --git a/sections/testingandquality/aaa.md b/sections/testingandquality/aaa.md index 3a3acdf4..8678da7e 100644 --- a/sections/testingandquality/aaa.md +++ b/sections/testingandquality/aaa.md @@ -20,7 +20,7 @@ describe.skip('Customer classifier', () => { test('When customer spent more than 500$, should be classified as premium', () => { //Arrange const customerToClassify = {spent:505, joined: new Date(), id:1} - const DBStub = sinon.stub(dataAccess, "getCustomer") + const DBStub = sinon.stub(dataAccess, 'getCustomer') .reply({id:1, classification: 'regular'}); //Act @@ -38,7 +38,7 @@ describe.skip('Customer classifier', () => { ```javascript test('Should be classified as premium', () => { const customerToClassify = {spent:505, joined: new Date(), id:1} - const DBStub = sinon.stub(dataAccess, "getCustomer") + const DBStub = sinon.stub(dataAccess, 'getCustomer') .reply({id:1, classification: 'regular'}); const receivedClassification = customerClassifier.classifyCustomer(customerToClassify); expect(receivedClassification).toMatch('premium'); diff --git a/sections/testingandquality/avoid-global-test-fixture.md b/sections/testingandquality/avoid-global-test-fixture.md index 54fa0acc..92631c92 100644 --- a/sections/testingandquality/avoid-global-test-fixture.md +++ b/sections/testingandquality/avoid-global-test-fixture.md @@ -10,12 +10,12 @@ ### Code example: each test acts on its own set of data ```javascript -it("When updating site name, get successful confirmation", async () => { +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" + name: 'siteForUpdateTest' }); - const updateNameResult = await SiteService.changeName(siteUnderTest, "newName"); + const updateNameResult = await SiteService.changeName(siteUnderTest, 'newName'); expect(updateNameResult).to.be(true); }); ``` @@ -28,15 +28,15 @@ 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"); +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); }); -it("When querying by site name, get the right site", async () => { - //I know that site name "portal" exists - I saw it in the seed files - const siteToCheck = await SiteService.getSiteByName("Portal"); - expect(siteToCheck.name).to.be.equal("Portal"); //Failure! The previous test change the name :[ +it('When querying by site name, get the right site', async () => { + //I know that site name 'portal' exists - I saw it in the seed files + const siteToCheck = await SiteService.getSiteByName('Portal'); + expect(siteToCheck.name).to.be.equal('Portal'); //Failure! The previous test change the name :[ }); ``` \ No newline at end of file