Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
refactor: transpile with babel (#73)
This allows us to use ES module syntax (import/export), and any other
modern javascript features that are not yet supported in node. The
babel config targets node version 14. Basically this just converts ES
modules to common js, since node already supports most modern
javascript.

In development mode, we use babel-node to start the server. This
transpiles the code at runtime. So you don't need to worry about a
build step when running in dev mode.

For production mode, the code is transpiled at build time to the
`dist` directory.

Instead of including `require('dotenv').config()` at the top of the
entry point, we now load dotenv via the command line.
  • Loading branch information
lukePeavey committed May 28, 2021
1 parent f646080 commit 21b8423
Show file tree
Hide file tree
Showing 34 changed files with 4,266 additions and 1,079 deletions.
3 changes: 3 additions & 0 deletions .babelrc
@@ -0,0 +1,3 @@
{
"presets": [["latest-node", { "target": "12" }]]
}
1 change: 1 addition & 0 deletions .eslintignore
@@ -1,6 +1,7 @@
/.vscode
/.git
/data
/dist
/docs
/private
node_modules
3 changes: 3 additions & 0 deletions .gitignore
@@ -1,5 +1,8 @@
# See https://help.github.com/ignore-files/ for more about ignoring files.

# Transpiled code
/dist

# dependencies
/node_modules

Expand Down
7 changes: 3 additions & 4 deletions __tests__/routes/listAuthors.test.js
@@ -1,7 +1,6 @@
require('dotenv').config()
const request = require('supertest')
const app = require('../../src/app')
const db = require('../../scripts/db')
import request from 'supertest'
import app from '../../src/app'
import db from '../../scripts/db'

// Setup
beforeAll(async () => db.connect())
Expand Down
17 changes: 8 additions & 9 deletions __tests__/routes/listQuotes.test.js
@@ -1,12 +1,11 @@
require('dotenv').config()
const request = require('supertest')
const last = require('lodash/last')
const first = require('lodash/first')
const { stringify } = require('query-string')
const app = require('../../src/app')
const db = require('../../scripts/db')
const Authors = require('../../src/models/Authors')
const Quotes = require('../../src/models/Quotes')
import request from 'supertest'
import last from 'lodash/last'
import first from 'lodash/first'
import { stringify } from 'query-string'
import app from '../../src/app'
import db from '../../scripts/db'
import Authors from '../../src/models/Authors'
import Quotes from '../../src/models/Quotes'

// This tag will be used as the value of the `tag` param in tests
let singleTag
Expand Down
7 changes: 3 additions & 4 deletions __tests__/routes/listTags.test.js
@@ -1,7 +1,6 @@
require('dotenv').config()
const request = require('supertest')
const app = require('../../src/app')
const db = require('../../scripts/db')
import request from 'supertest'
import app from '../../src/app'
import db from '../../scripts/db'

beforeAll(async () => db.connect())

Expand Down
33 changes: 20 additions & 13 deletions __tests__/routes/randomQuote.test.js
@@ -1,13 +1,12 @@
require('dotenv').config()
const request = require('supertest')
const range = require('lodash/range')
const last = require('lodash/last')
const first = require('lodash/first')
const { stringify } = require('query-string')
const app = require('../../src/app')
const db = require('../../scripts/db')
const Authors = require('../../src/models/Authors')
const Quotes = require('../../src/models/Quotes')
import request from 'supertest'
import range from 'lodash/range'
import last from 'lodash/last'
import first from 'lodash/first'
import { stringify } from 'query-string'
import app from '../../src/app'
import db from '../../scripts/db'
import Authors from '../../src/models/Authors'
import Quotes from '../../src/models/Quotes'

// This tag will be used as the value of the `tag` param in tests
let singleTag
Expand Down Expand Up @@ -100,9 +99,17 @@ describe('GET /random', () => {
test(`Given a valid author \`name\`, returns a random quote by
the specified author.`, async () => {
// Select an author from the database.
const validAuthorName = singleAuthor.name
const query = { author: encodeURI(validAuthorName) }
const url = `/random?${stringify(query)}`
const name = encodeURI(singleAuthor.name)
const url = `/random?${stringify({ author: name })}`
const { status, body } = await request(app).get(url)
expect(status).toBe(200)
expect(body.author).toEqual(singleAuthor.name)
})

test(`Given an author \`name\`, returns a random quote by
the specified author.`, async () => {
// Select an author from the database.
const url = `/random?${stringify({ author: singleAuthor.name })}`
const { status, body } = await request(app).get(url)
expect(status).toBe(200)
expect(body.author).toEqual(singleAuthor.name)
Expand Down
9 changes: 4 additions & 5 deletions __tests__/routes/searchQuotes.test.js
@@ -1,8 +1,7 @@
require('dotenv').config()
const request = require('supertest')
const { stringify } = require('query-string')
const app = require('../../src/app')
const db = require('../../scripts/db')
import request from 'supertest'
import { stringify } from 'query-string'
import app from '../../src/app'
import db from '../../scripts/db'

beforeAll(async () => db.connect())

Expand Down

0 comments on commit 21b8423

Please sign in to comment.