From 2e803798ac6b51d4e9db53846806e3437e07a624 Mon Sep 17 00:00:00 2001 From: Luke Karrys Date: Wed, 3 Nov 2021 14:43:16 -0700 Subject: [PATCH] fixup! use config set and simplify snapshot formatting --- .../test/lib/commands/shrinkwrap.js.test.cjs | 254 ++++++++++-------- test/lib/commands/shrinkwrap.js | 17 +- 2 files changed, 150 insertions(+), 121 deletions(-) diff --git a/tap-snapshots/test/lib/commands/shrinkwrap.js.test.cjs b/tap-snapshots/test/lib/commands/shrinkwrap.js.test.cjs index 9b7b989bbf9e6..a0d5795776d6f 100644 --- a/tap-snapshots/test/lib/commands/shrinkwrap.js.test.cjs +++ b/tap-snapshots/test/lib/commands/shrinkwrap.js.test.cjs @@ -6,147 +6,162 @@ */ 'use strict' exports[`test/lib/commands/shrinkwrap.js TAP with hidden lockfile ancient > must match snapshot 1`] = ` - localPrefix = { +{ + "localPrefix": { "node_modules": { ".package-lock.json": { "lockfileVersion": 1 } } - } - config = {} - npm-shrinkwrap.json = { + }, + "config": {}, + "shrinkwrap": { "name": "tap-testdir-shrinkwrap-with-hidden-lockfile-ancient", "lockfileVersion": 1, "requires": true - } - logs = [ + }, + "logs": [ "created a lockfile as npm-shrinkwrap.json" ] +} ` exports[`test/lib/commands/shrinkwrap.js TAP with hidden lockfile ancient upgrade > must match snapshot 1`] = ` - localPrefix = { +{ + "localPrefix": { "node_modules": { ".package-lock.json": { "lockfileVersion": 1 } } - } - config = { + }, + "config": { "lockfileVersion": 3 - } - npm-shrinkwrap.json = { + }, + "shrinkwrap": { "name": "tap-testdir-shrinkwrap-with-hidden-lockfile-ancient-upgrade", "lockfileVersion": 3, "requires": true, "packages": {} - } - logs = [ + }, + "logs": [ "created a lockfile as npm-shrinkwrap.json with version 3" ] +} ` exports[`test/lib/commands/shrinkwrap.js TAP with hidden lockfile existing > must match snapshot 1`] = ` - localPrefix = { +{ + "localPrefix": { "node_modules": { ".package-lock.json": { "lockfileVersion": 2 } } - } - config = {} - npm-shrinkwrap.json = { + }, + "config": {}, + "shrinkwrap": { "name": "tap-testdir-shrinkwrap-with-hidden-lockfile-existing", "lockfileVersion": 2, "requires": true, "packages": {} - } - logs = [ + }, + "logs": [ "created a lockfile as npm-shrinkwrap.json" ] +} ` exports[`test/lib/commands/shrinkwrap.js TAP with hidden lockfile existing downgrade > must match snapshot 1`] = ` - localPrefix = { +{ + "localPrefix": { "node_modules": { ".package-lock.json": { "lockfileVersion": 2 } } - } - config = { + }, + "config": { "lockfileVersion": 1 - } - npm-shrinkwrap.json = { + }, + "shrinkwrap": { "name": "tap-testdir-shrinkwrap-with-hidden-lockfile-existing-downgrade", "lockfileVersion": 1, "requires": true - } - logs = [ + }, + "logs": [ "created a lockfile as npm-shrinkwrap.json with version 1" ] +} ` exports[`test/lib/commands/shrinkwrap.js TAP with hidden lockfile existing upgrade > must match snapshot 1`] = ` - localPrefix = { +{ + "localPrefix": { "node_modules": { ".package-lock.json": { "lockfileVersion": 2 } } - } - config = { + }, + "config": { "lockfileVersion": 3 - } - npm-shrinkwrap.json = { + }, + "shrinkwrap": { "name": "tap-testdir-shrinkwrap-with-hidden-lockfile-existing-upgrade", "lockfileVersion": 3, "requires": true, "packages": {} - } - logs = [ + }, + "logs": [ "created a lockfile as npm-shrinkwrap.json with version 3" ] +} ` exports[`test/lib/commands/shrinkwrap.js TAP with nothing ancient > must match snapshot 1`] = ` - localPrefix = {} - config = {} - npm-shrinkwrap.json = { +{ + "localPrefix": {}, + "config": {}, + "shrinkwrap": { "name": "tap-testdir-shrinkwrap-with-nothing-ancient", "lockfileVersion": 2, "requires": true, "packages": {} - } - logs = [ + }, + "logs": [ "created a lockfile as npm-shrinkwrap.json with version 2" ] +} ` exports[`test/lib/commands/shrinkwrap.js TAP with nothing ancient upgrade > must match snapshot 1`] = ` - localPrefix = {} - config = { +{ + "localPrefix": {}, + "config": { "lockfileVersion": 3 - } - npm-shrinkwrap.json = { + }, + "shrinkwrap": { "name": "tap-testdir-shrinkwrap-with-nothing-ancient-upgrade", "lockfileVersion": 3, "requires": true, "packages": {} - } - logs = [ + }, + "logs": [ "created a lockfile as npm-shrinkwrap.json with version 3" ] +} ` exports[`test/lib/commands/shrinkwrap.js TAP with npm-shrinkwrap.json ancient > must match snapshot 1`] = ` - localPrefix = { +{ + "localPrefix": { "npm-shrinkwrap.json": { "lockfileVersion": 1 } - } - config = {} - npm-shrinkwrap.json = { + }, + "config": {}, + "shrinkwrap": { "name": "tap-testdir-shrinkwrap-with-npm-shrinkwrap.json-ancient", "lockfileVersion": 2, "requires": true, @@ -155,22 +170,24 @@ exports[`test/lib/commands/shrinkwrap.js TAP with npm-shrinkwrap.json ancient > "name": "tap-testdir-shrinkwrap-with-npm-shrinkwrap.json-ancient" } } - } - logs = [ + }, + "logs": [ "npm-shrinkwrap.json updated to version 2" ] +} ` exports[`test/lib/commands/shrinkwrap.js TAP with npm-shrinkwrap.json ancient upgrade > must match snapshot 1`] = ` - localPrefix = { +{ + "localPrefix": { "npm-shrinkwrap.json": { "lockfileVersion": 1 } - } - config = { + }, + "config": { "lockfileVersion": 3 - } - npm-shrinkwrap.json = { + }, + "shrinkwrap": { "name": "tap-testdir-shrinkwrap-with-npm-shrinkwrap.json-ancient-upgrade", "lockfileVersion": 3, "requires": true, @@ -179,20 +196,22 @@ exports[`test/lib/commands/shrinkwrap.js TAP with npm-shrinkwrap.json ancient up "name": "tap-testdir-shrinkwrap-with-npm-shrinkwrap.json-ancient-upgrade" } } - } - logs = [ + }, + "logs": [ "npm-shrinkwrap.json updated to version 3" ] +} ` exports[`test/lib/commands/shrinkwrap.js TAP with npm-shrinkwrap.json existing > must match snapshot 1`] = ` - localPrefix = { +{ + "localPrefix": { "npm-shrinkwrap.json": { "lockfileVersion": 2 } - } - config = {} - npm-shrinkwrap.json = { + }, + "config": {}, + "shrinkwrap": { "name": "tap-testdir-shrinkwrap-with-npm-shrinkwrap.json-existing", "lockfileVersion": 2, "requires": true, @@ -201,41 +220,45 @@ exports[`test/lib/commands/shrinkwrap.js TAP with npm-shrinkwrap.json existing > "name": "tap-testdir-shrinkwrap-with-npm-shrinkwrap.json-existing" } } - } - logs = [ + }, + "logs": [ "npm-shrinkwrap.json up to date" ] +} ` exports[`test/lib/commands/shrinkwrap.js TAP with npm-shrinkwrap.json existing downgrade > must match snapshot 1`] = ` - localPrefix = { +{ + "localPrefix": { "npm-shrinkwrap.json": { "lockfileVersion": 2 } - } - config = { + }, + "config": { "lockfileVersion": 1 - } - npm-shrinkwrap.json = { + }, + "shrinkwrap": { "name": "tap-testdir-shrinkwrap-with-npm-shrinkwrap.json-existing-downgrade", "lockfileVersion": 1, "requires": true - } - logs = [ + }, + "logs": [ "npm-shrinkwrap.json updated to version 1" ] +} ` exports[`test/lib/commands/shrinkwrap.js TAP with npm-shrinkwrap.json existing upgrade > must match snapshot 1`] = ` - localPrefix = { +{ + "localPrefix": { "npm-shrinkwrap.json": { "lockfileVersion": 2 } - } - config = { + }, + "config": { "lockfileVersion": 3 - } - npm-shrinkwrap.json = { + }, + "shrinkwrap": { "name": "tap-testdir-shrinkwrap-with-npm-shrinkwrap.json-existing-upgrade", "lockfileVersion": 3, "requires": true, @@ -244,20 +267,22 @@ exports[`test/lib/commands/shrinkwrap.js TAP with npm-shrinkwrap.json existing u "name": "tap-testdir-shrinkwrap-with-npm-shrinkwrap.json-existing-upgrade" } } - } - logs = [ + }, + "logs": [ "npm-shrinkwrap.json updated to version 3" ] +} ` exports[`test/lib/commands/shrinkwrap.js TAP with package-lock.json ancient > must match snapshot 1`] = ` - localPrefix = { +{ + "localPrefix": { "package-lock.json": { "lockfileVersion": 1 } - } - config = {} - npm-shrinkwrap.json = { + }, + "config": {}, + "shrinkwrap": { "name": "tap-testdir-shrinkwrap-with-package-lock.json-ancient", "lockfileVersion": 2, "requires": true, @@ -266,22 +291,24 @@ exports[`test/lib/commands/shrinkwrap.js TAP with package-lock.json ancient > mu "name": "tap-testdir-shrinkwrap-with-package-lock.json-ancient" } } - } - logs = [ + }, + "logs": [ "package-lock.json has been renamed to npm-shrinkwrap.json and updated to version 2" ] +} ` exports[`test/lib/commands/shrinkwrap.js TAP with package-lock.json ancient upgrade > must match snapshot 1`] = ` - localPrefix = { +{ + "localPrefix": { "package-lock.json": { "lockfileVersion": 1 } - } - config = { + }, + "config": { "lockfileVersion": 3 - } - npm-shrinkwrap.json = { + }, + "shrinkwrap": { "name": "tap-testdir-shrinkwrap-with-package-lock.json-ancient-upgrade", "lockfileVersion": 3, "requires": true, @@ -290,20 +317,22 @@ exports[`test/lib/commands/shrinkwrap.js TAP with package-lock.json ancient upgr "name": "tap-testdir-shrinkwrap-with-package-lock.json-ancient-upgrade" } } - } - logs = [ + }, + "logs": [ "package-lock.json has been renamed to npm-shrinkwrap.json and updated to version 3" ] +} ` exports[`test/lib/commands/shrinkwrap.js TAP with package-lock.json existing > must match snapshot 1`] = ` - localPrefix = { +{ + "localPrefix": { "package-lock.json": { "lockfileVersion": 2 } - } - config = {} - npm-shrinkwrap.json = { + }, + "config": {}, + "shrinkwrap": { "name": "tap-testdir-shrinkwrap-with-package-lock.json-existing", "lockfileVersion": 2, "requires": true, @@ -312,41 +341,45 @@ exports[`test/lib/commands/shrinkwrap.js TAP with package-lock.json existing > m "name": "tap-testdir-shrinkwrap-with-package-lock.json-existing" } } - } - logs = [ + }, + "logs": [ "package-lock.json has been renamed to npm-shrinkwrap.json" ] +} ` exports[`test/lib/commands/shrinkwrap.js TAP with package-lock.json existing downgrade > must match snapshot 1`] = ` - localPrefix = { +{ + "localPrefix": { "package-lock.json": { "lockfileVersion": 2 } - } - config = { + }, + "config": { "lockfileVersion": 1 - } - npm-shrinkwrap.json = { + }, + "shrinkwrap": { "name": "tap-testdir-shrinkwrap-with-package-lock.json-existing-downgrade", "lockfileVersion": 1, "requires": true - } - logs = [ + }, + "logs": [ "package-lock.json has been renamed to npm-shrinkwrap.json and updated to version 1" ] +} ` exports[`test/lib/commands/shrinkwrap.js TAP with package-lock.json existing upgrade > must match snapshot 1`] = ` - localPrefix = { +{ + "localPrefix": { "package-lock.json": { "lockfileVersion": 2 } - } - config = { + }, + "config": { "lockfileVersion": 3 - } - npm-shrinkwrap.json = { + }, + "shrinkwrap": { "name": "tap-testdir-shrinkwrap-with-package-lock.json-existing-upgrade", "lockfileVersion": 3, "requires": true, @@ -355,8 +388,9 @@ exports[`test/lib/commands/shrinkwrap.js TAP with package-lock.json existing upg "name": "tap-testdir-shrinkwrap-with-package-lock.json-existing-upgrade" } } - } - logs = [ + }, + "logs": [ "package-lock.json has been renamed to npm-shrinkwrap.json and updated to version 3" ] +} ` diff --git a/test/lib/commands/shrinkwrap.js b/test/lib/commands/shrinkwrap.js index 32e36da5a65e7..112aa0a28a29b 100644 --- a/test/lib/commands/shrinkwrap.js +++ b/test/lib/commands/shrinkwrap.js @@ -4,21 +4,16 @@ const { resolve } = require('path') const { real: mockNpm } = require('../../fixtures/mock-npm') // Attempt to parse json values in snapshots before -// stringifying to remove escaped values -const stringify = (obj) => JSON.stringify(obj, (k, v) => { +// stringifying to remove escaped values like \\" +// This also doesn't reorder the keys of the object +// like tap does by default which is nice in this case +t.formatSnapshot = (obj) => JSON.stringify(obj, (k, v) => { try { return JSON.parse(v) } catch (_) {} return v }, 2) -t.formatSnapshot = (obj) => [ - `localPrefix = ${stringify(obj.testdir)}`, - `config = ${stringify(obj.config)}`, - `npm-shrinkwrap.json = ${stringify(obj.shrinkwrap)}`, - `logs = ${stringify(obj.logs)}`, -].join('\n').split('\n').map((s) => ` ${s}`).join('\n') - // Run shrinkwrap against a specified testdir with config items // and make some assertions that should always be true. Sets // the results on t.context for use in child tests @@ -34,7 +29,7 @@ const shrinkwrap = async ( npm.localPrefix = t.testdir(testdir) if (config.lockfileVersion) - npm.flatOptions.lockfileVersion = config.lockfileVersion + npm.config.set('lockfile-version', config.lockfileVersion) if (config.global) npm.config.set('global', config.global) @@ -53,7 +48,7 @@ const shrinkwrap = async ( t.same(warnings, [], 'no warnings') t.teardown(() => delete t.context) t.context = { - testdir, + localPrefix: testdir, config, shrinkwrap: JSON.parse(fs.readFileSync(newFile)), logs: notices,