Skip to content

Commit

Permalink
chore(tests): finish test refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar committed Feb 4, 2021
1 parent a6425f8 commit 8572e95
Showing 1 changed file with 44 additions and 19 deletions.
63 changes: 44 additions & 19 deletions test/lib/publish.js
Expand Up @@ -155,9 +155,10 @@ t.test('re-loads publishConfig if added during script process', (t) => {
})
})

t.test('should not log if silent', (t) => {
t.test('should not log if silent (dry run)', (t) => {
t.plan(2)

const registry = 'https://registry.npmjs.org'
const testDir = t.testdir({
'package.json': JSON.stringify({
name: 'my-cool-pkg',
Expand All @@ -171,9 +172,14 @@ t.test('should not log if silent', (t) => {
json: false,
defaultTag: 'latest',
dryRun: true,
registry: 'https://registry.npmjs.org/',
registry,
},
config: {
...config,
getCredentialsByURI: () => {
throw new Error('should not call getCredentialsByURI in dry run')
}
},
config,
},
'../../lib/utils/tar.js': {
getContents: () => ({}),
Expand All @@ -195,7 +201,7 @@ t.test('should not log if silent', (t) => {
libnpmpack: async () => '',
libnpmpublish: {
publish: (manifest, tarData, opts) => {
throw new Error('should not call libnpmpublish!')
throw new Error('should not call libnpmpublish in dry run')
},
},
})
Expand All @@ -207,8 +213,10 @@ t.test('should not log if silent', (t) => {
})
})

t.test('should log tarball contents', (t) => {
t.test('should log tarball contents (dry run)', (t) => {
t.plan(3)

const registry = 'https://registry.npmjs.org'
const testDir = t.testdir({
'package.json': JSON.stringify({
name: 'my-cool-pkg',
Expand All @@ -222,12 +230,12 @@ t.test('should log tarball contents', (t) => {
json: false,
defaultTag: 'latest',
dryRun: true,
registry: 'https://registry.npmjs.org/',
registry,
},
config: {
...config,
getCredentialsByURI: () => {
throw new Error('should not call getCredentialsByURI!')
throw new Error('should not call getCredentialsByURI in dry run')
}},
},
'../../lib/utils/tar.js': {
Expand All @@ -247,7 +255,7 @@ t.test('should log tarball contents', (t) => {
libnpmpack: async () => '',
libnpmpublish: {
publish: () => {
throw new Error('should not call libnpmpublish!')
throw new Error('should not call libnpmpublish in dry run')
},
},
})
Expand Down Expand Up @@ -277,12 +285,15 @@ t.test('shows usage with wrong set of arguments', (t) => {

t.test('throws when invalid tag', (t) => {
t.plan(1)

const registry = 'https://registry.npmjs.org'

const publish = requireInject('../../lib/publish.js', {
'../../lib/npm.js': {
flatOptions: {
json: false,
defaultTag: '0.0.13',
registry: 'https://registry.npmjs.org/',
registry,
},
config,
},
Expand All @@ -296,7 +307,9 @@ t.test('throws when invalid tag', (t) => {
})

t.test('can publish a tarball', t => {
t.plan(3)
t.plan(4)

const registry = 'https://registry.npmjs.org/'
const testDir = t.testdir({
package: {
'package.json': JSON.stringify({
Expand All @@ -322,9 +335,12 @@ t.test('can publish a tarball', t => {
flatOptions: {
json: true,
defaultTag: 'latest',
registry: 'https://registry.npmjs.org/',
registry,
},
config: {
...config,
getCredentialsByURI: registryCredentials(t, registry),
},
config,
},
'../../lib/utils/tar.js': {
getContents: () => ({
Expand Down Expand Up @@ -355,14 +371,19 @@ t.test('can publish a tarball', t => {
})

t.test('throw if not logged in', async t => {
t.plan(1)
t.plan(2)
const registry = 'https://unauthed.registry'

const publish = requireInject('../../lib/publish.js', {
'../../lib/npm.js': {
flatOptions: {
json: false,
registry: 'https://unauthed.registry',
registry,
},
config: {
...config,
getCredentialsByURI: registryCredentials(t, registry),
},
config,
},
})

Expand All @@ -375,9 +396,10 @@ t.test('throw if not logged in', async t => {
})

t.test('read registry only from publishConfig', t => {
t.plan(3)
t.plan(4)

const publishConfig = { registry: 'https://some.registry' }
const registry = 'https://some.registry'
const publishConfig = { registry }
const testDir = t.testdir({
'package.json': JSON.stringify({
name: 'my-cool-pkg',
Expand All @@ -391,7 +413,10 @@ t.test('read registry only from publishConfig', t => {
flatOptions: {
json: false,
},
config,
config: {
...config,
getCredentialsByURI: registryCredentials(t, registry),
},
},
'../../lib/utils/tar.js': {
getContents: () => ({
Expand All @@ -403,7 +428,7 @@ t.test('read registry only from publishConfig', t => {
libnpmpublish: {
publish: (manifest, tarData, opts) => {
t.match(manifest, { name: 'my-cool-pkg', version: '1.0.0' }, 'gets manifest')
t.same(opts.registry, publishConfig.registry, 'publishConfig is passed through')
t.same(opts.registry, registry, 'publishConfig is passed through')
},
},
})
Expand Down

0 comments on commit 8572e95

Please sign in to comment.