Skip to content

Commit

Permalink
fix: don't allow mutating original config object by the caller (#5)
Browse files Browse the repository at this point in the history
`loadConfig` with `merge=false` was using spread operator to
clone  properties from fixture's configuration. Since that is
a shallow copy, child objects were returned by reference and
allowed caller to mutate those objects, affecting further
uses of `loadConfig`. Ensure that is not possible by returning
a copy of loaded config.

This was a problem when one test loaded config and passed it
to Nuxt Builder. Nuxt builder then appended generated plugins
to plugins object and those plugins were then used by subsequent
tests, even if it intended to build new, clean fixture.
  • Loading branch information
rchl authored and ricardogobbosouza committed Oct 15, 2019
1 parent 9375099 commit 642cd1b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/setup.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const clonedeep = require('clone-deep')
const deepmerge = require('deepmerge')
const request = require('request-promise-native')
const getPort = require('get-port')
Expand All @@ -15,8 +16,8 @@ const loadConfig = (dir, fixture = null, override = {}, { merge = false } = {})
return deepmerge.all([config, override])
} else {
return {
...config,
...override
...clonedeep(config),
...clonedeep(override)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"test": "yarn lint && jest"
},
"dependencies": {
"clone-deep": "^4.0.1",
"deepmerge": "^4.1.1",
"get-port": "^5.0.0",
"request": "^2.88.0",
Expand Down
16 changes: 16 additions & 0 deletions test/setup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,20 @@ describe('loadConfig', () => {
const config = loadConfig(__dirname, 'setup', override, { merge: true })
expect(config.env).toEqual({ ENV1: 'true', ENV2: 'true' })
})

test('returns a copy of loaded config when not merging', () => {
let config = loadConfig(__dirname, 'setup')
config.render.injectScripts = true

config = loadConfig(__dirname, 'setup')
expect(config.render.injectScripts).toEqual(undefined)
})

test('returns a copy of loaded config when merging', () => {
const override = { render: { injectScripts: true } }
let config = loadConfig(__dirname, 'setup', override, { merge: true })

config = loadConfig(__dirname, 'setup')
expect(config.render.injectScripts).toEqual(undefined)
})
})
16 changes: 16 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2616,6 +2616,15 @@ cliui@^5.0.0:
strip-ansi "^5.2.0"
wrap-ansi "^5.1.0"

clone-deep@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/clone-deep/-/clone-deep-4.0.1.tgz#c19fd9bdbbf85942b4fd979c84dcf7d5f07c2387"
integrity sha512-neHB9xuzh/wk0dIHweyAXv2aPGZIVk3pLMe+/RNzINf17fe0OG96QroktYAUm7SM1PBnzTabaLboqqxDyMU+SQ==
dependencies:
is-plain-object "^2.0.4"
kind-of "^6.0.2"
shallow-clone "^3.0.0"

co@^4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/co/-/co-4.6.0.tgz#6ea6bdf3d853ae54ccb8e47bfa0bf3f9031fb184"
Expand Down Expand Up @@ -8936,6 +8945,13 @@ sha.js@^2.4.0, sha.js@^2.4.8:
inherits "^2.0.1"
safe-buffer "^5.0.1"

shallow-clone@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/shallow-clone/-/shallow-clone-3.0.1.tgz#8f2981ad92531f55035b01fb230769a40e02efa3"
integrity sha512-/6KqX+GVUdqPuPPd2LxDDxzX6CAbjJehAAOKlNpqqUpAqPM6HeL8f+o3a+JsyGjn2lv0WY8UsTgUJjU9Ok55NA==
dependencies:
kind-of "^6.0.2"

shebang-command@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/shebang-command/-/shebang-command-1.2.0.tgz#44aac65b695b03398968c39f363fee5deafdf1ea"
Expand Down

0 comments on commit 642cd1b

Please sign in to comment.