Skip to content

Commit d7ca973

Browse files
committed
Refactor out middleware, organise dir structure and more test coverage
1 parent 4cd9522 commit d7ca973

10 files changed

Lines changed: 172 additions & 14 deletions

File tree

.circleci/config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,5 @@ jobs:
3030
# run tests!
3131
- run: yarn test
3232

33+
- store_artifacts:
34+
path: ./coverage

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ node_modules
33
/yarn-error.log
44
doc
55
/doc.html
6+
/coverage

index.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
const path = require('path')
2-
3-
const middleware = keys => (req, res, next) => {
4-
if (!req.env) req.env = {}
5-
keys.forEach(key => req.env[key] = process.env[key])
6-
next()
7-
}
2+
const middleware = require('./lib/middleware')
83

94
module.exports = function NuxtEnv ({ keys }) {
105
this.addServerMiddleware(middleware(keys))
116

12-
const src = path.resolve(__dirname, 'plugin.js')
7+
const src = path.resolve(__dirname, 'lib/plugin.js')
138
this.addPlugin({
149
src,
1510
filename: 'nuxt-env'

lib/middleware.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = (keys = []) => (req = {}, res, next = () => {}) => {
2+
if (!req.env) req.env = {}
3+
keys.forEach(key => req.env[key] = process.env[key])
4+
next()
5+
}
File renamed without changes.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"name": "nuxt-env",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"description": "Inject env vars for your Nuxt app at runtime",
55
"main": "index.js",
66
"repository": "https://github.com/samtgarson/nuxt-env",
77
"author": "samtgarson@gmail.com",
88
"license": "MIT",
99
"scripts": {
1010
"test": "yarn test:unit && yarn test:lint",
11-
"test:unit": "NODE_ENV=test jest",
11+
"test:unit": "NODE_ENV=test jest --coverage",
1212
"test:lint": "eslint --ignore-path .gitignore --ignore-pattern '/doc/**' ."
1313
},
1414
"babel": {

test/e2e.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1+
import { Nuxt, Builder } from 'nuxt'
2+
import request from 'request-promise-native'
3+
import config from './support/app/nuxt.config'
4+
15
jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000
26
process.env.PORT = process.env.PORT || 5060
37
process.env.NODE_ENV = 'production'
48

5-
const { Nuxt, Builder } = require('nuxt')
6-
const request = require('request-promise-native')
7-
8-
const config = require('./support/app/nuxt.config')
9-
109
const url = path => `http://localhost:${process.env.PORT}${path}`
1110
const get = path => request({
1211
followRedirect: req => req.headers.location.startsWith('/'),

test/index.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import module from '@/index'
2+
3+
jest.mock('@/lib/middleware', () => jest.fn(() => 'middleware result'))
4+
5+
const FakeModule = {
6+
addServerMiddleware: jest.fn(),
7+
addPlugin: jest.fn()
8+
}
9+
10+
const subject = module.bind(FakeModule)
11+
const args = {
12+
keys: ['TEST_ENV']
13+
}
14+
15+
describe('Module', () => {
16+
it('adds the server middleware', () => {
17+
subject(args)
18+
19+
expect(FakeModule.addServerMiddleware)
20+
.toHaveBeenCalledWith('middleware result')
21+
})
22+
23+
it('adds the plugin', () => {
24+
subject(args)
25+
26+
expect(FakeModule.addPlugin).toHaveBeenCalled()
27+
const { calls: { 0: { 0: calls } } } = FakeModule.addPlugin.mock
28+
29+
expect(calls.filename).toEqual('nuxt-env')
30+
expect(calls.src).toMatch(/lib\/plugin\.js/)
31+
})
32+
})

test/lib/middleware.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import Middleware from '@/lib/middleware'
2+
3+
describe('Middleware', () => {
4+
it('creates a middleware function', () => {
5+
const middleware = Middleware()
6+
7+
expect(middleware).toBeInstanceOf(Function)
8+
})
9+
10+
it('adds env to the request', () => {
11+
const expectedKey = 'TEST_VALUE'
12+
const expectedValue = 'test value'
13+
const req = { env: {} }
14+
const middleware = Middleware([expectedKey])
15+
process.env[expectedKey] = expectedValue
16+
17+
middleware(req, {})
18+
19+
expect(req.env).toHaveProperty(expectedKey, expectedValue)
20+
})
21+
22+
it('calls next when its finished', () => {
23+
const middleware = Middleware()
24+
const next = jest.fn()
25+
middleware({}, {}, next)
26+
27+
expect(next).toHaveBeenCalled()
28+
})
29+
30+
it('handles empty arguments', () => {
31+
const middleware = Middleware()
32+
expect(() => { middleware() }).not.toThrow()
33+
})
34+
})

test/lib/plugin.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import plugin from '@/lib/plugin'
2+
3+
let inject
4+
let context
5+
const originalContext = {
6+
beforeNuxtRender: jest.fn(),
7+
env: {
8+
test1: 'foo',
9+
test2: 'bar'
10+
},
11+
nuxtState: {
12+
env: {}
13+
}
14+
}
15+
16+
const expectedEnv = {
17+
test1: 'foo', // from the config env
18+
test2: 'baz', // from config env, overwritten by nuxt-env
19+
test3: 'foobar' // from nuxt-env
20+
}
21+
22+
beforeEach(() => {
23+
inject = jest.fn()
24+
context = { ...originalContext }
25+
})
26+
27+
describe('Plugin', () => {
28+
describe('client side', () => {
29+
beforeEach(() => {
30+
context.isClient = true
31+
plugin(context, inject)
32+
})
33+
34+
it('injects the env', () => {
35+
expect(inject).toHaveBeenCalledWith('env', context.nuxtState.env)
36+
})
37+
})
38+
39+
describe('server side', () => {
40+
const reqEnv = { test2: 'baz', test3: 'foobar' }
41+
42+
beforeEach(() => {
43+
context.isClient = false
44+
})
45+
46+
describe('in a normal case', () => {
47+
beforeEach(() => {
48+
context.req = { env: reqEnv }
49+
plugin(context, inject)
50+
})
51+
52+
it('injects the env', () => {
53+
expect(inject).toHaveBeenCalled()
54+
const { calls: { 0: { 0: calls } } } = inject.mock
55+
expect(calls).toEqual('env')
56+
})
57+
58+
it('merges the two env vars', () => {
59+
const { calls: { 0: { 1: calls } } } = inject.mock
60+
expect(calls).toMatchObject(expectedEnv)
61+
})
62+
63+
it('calls before render hook with a callback', () => {
64+
expect(context.beforeNuxtRender).toHaveBeenCalled()
65+
const { calls: { 0: { 0: hookCallback } } } = context.beforeNuxtRender.mock
66+
67+
expect(hookCallback).toBeInstanceOf(Function)
68+
})
69+
70+
it('adds env to nuxtState', () => {
71+
const { calls: { 0: { 0: hookCallback } } } = context.beforeNuxtRender.mock
72+
73+
hookCallback(context)
74+
75+
expect(context.nuxtState.env).toMatchObject(expectedEnv)
76+
})
77+
})
78+
79+
describe('with missing dependencies', () => {
80+
beforeEach(() => {
81+
delete context.env
82+
})
83+
84+
it('has sensible defaults', () => {
85+
const runPlugin = () => { plugin(context, inject) }
86+
expect(runPlugin).not.toThrow()
87+
})
88+
})
89+
})
90+
})

0 commit comments

Comments
 (0)