Skip to content

JavaScript Styleguide

Luis Carbonell edited this page Jun 11, 2019 · 2 revisions

Style Guide

Writing Tests

Imports

// GOOD
const _ = require("lodash");
const { assert, expect } = require('chai');
const should = require('chai').should();

// BAD
let _ = require("lodash");
let { assert, expect } = require('chai');
let should = require('chai').should();

// BAD
var _ = require("lodash");
var { assert, expect } = require('chai');
var should = require('chai').should();

// VERY BAD
var _ = require("lodash");
var assert = require('chai');
var expect = require('chai');
var should = require('chai').should();

Describing Classes

// GOOD
describe("Neuron", function() {
  describe("neuron.activate()", function() {
    it("neuron.activate() => {number}", ...)
    it("neuron.activate(input) => {number}", ...)
    it("neuron.activate(options) => {number}", ...)
    it("neuron.activate(input, options) => {number}", ...)
  })
})

// BAD
describe("Neuron", function() {
  describe("neuron.activate()", function() {
    it("neuron.activate() => {number}", ...)
    it("neuron.activate(number) => {number}", ...)
    it("neuron.activate(object) => {number}", ...)
    it("neuron.activate(number, object) => {number}", ...)
  })
})

// VERY BAD
describe("Neuron", function() {
  describe("neuron.activate()", function() {
    it("should return a number when no parameters are passed", ...)
    it("should return a number when one real number is passed a parameter", ...)
    it("should return a number when an options object is passed as a parameter", ...)
    it("should return a number when a one real number and an options object are passed as parameters", ...)
  })
})

Describing Functions

// GOOD
describe("sum()", function() {
  it("sum(number, other) => {number}", ...)
  it("sum(...numbers) => {number}", ...)
  it("sum(numbers) => {number}", ...)
})

// BAD
describe("Sum", function() {
  it("sum(number, other) => {number}", ...)
  it("sum(...numbers) => {number}", ...)
  it("sum(numbers) => {number}", ...)
})

// VERY BAD
describe("sum()", function() {
  it("should return a number when two numbers are passed as parameters", ...)
  it("should return a number when many number are passed as parameters", ...)
  it("should return a number when an array of numbers is passed as a parameter", ...)
})
Clone this wiki locally