Jim's
Tutorials

Fall 2018
course
site

I got bogged down setting up my javascript testing environment to do what I wanted... which is not rare. I wanted to use jest to unit test with ES6 syntax support and it just didn't get there. https://jestjs.io/docs/en/getting-started I used the setup guide from the official site to install some packages - the package.json looks like this:

{
  "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-jest": "^23.6.0",
    "jest": "^23.6.0",
    "regenerator-runtime": "^0.12.1"
  },
  "scripts": {
    "test": "jest"
  }
}

I got the example code working with commonjs import/export language which looks like this:

const sum = (a, b) => a + b
const mul = (a, b) => a * b
const sub = (a, b) => a - b
const div = (a, b) => a / b

module.exports = {sum, mul, sub, div};
const { sum, mul, sub, div } = require("./math")

test("Adding 1 + 1 equals 2", () => {
  expect(sum(1, 1)).toBe(2)
})
test("Multiplying 1 * 1 equals 1", () => {
  expect(mul(1, 1)).toBe(1)
})
test("Subtracting 1 - 1 equals 0", () => {
  expect(sub(1, 1)).toBe(0)
})
test("Dividing 1 / 1 equals 1", () => {
  expect(div(1, 1)).toBe(1)
})

But I wanted to get it to work with the ES6 syntax that I've defaulted to when working with javascript that looks like this:

const sum = (a, b) => a + b
const mul = (a, b) => a * b
const sub = (a, b) => a - b
const div = (a, b) => a / b

export default {sum, mul, sub, div};
import { sum, mul, sub, div } from "./math";

test("Adding 1 + 1 equals 2", () => {
  expect(sum(1, 1)).toBe(2)
})
test("Multiplying 1 * 1 equals 1", () => {
  expect(mul(1, 1)).toBe(1)
})
test("Subtracting 1 - 1 equals 0", () => {
  expect(sub(1, 1)).toBe(0)
})
test("Dividing 1 / 1 equals 1", () => {
  expect(div(1, 1)).toBe(1)
})

But I did not get there. The first example I followed, which failed to run, was using a hybrid of commonjs import statements and es6 export statements which I found rather odd. You can see that here:

https://flaviocopes.com/jest/

I suppose I'll just resign myself to module.exports for now :).