Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix tests
  • Loading branch information
ruyadorno committed Oct 31, 2019
commit ebda4caefd52632d70c4d7d2f6ddce91aa632df3
90 changes: 9 additions & 81 deletions tap-snapshots/test-tap-fund.js-TAP.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,17 @@
'use strict'
exports[`test/tap/fund.js TAP fund containing multi-level nested deps with no funding > should omit dependencies with no funding declared 1`] = `
nested-no-funding-packages@1.0.0
├─┬ lorem@1.0.0
│ └── url: https://example.com/lorem
└─┬ bar@1.0.0
├── type: individual
├── url: http://example.com/donate
└─┬ sub-bar@1.0.0
└── url: https://example.com/sponsor
+-- lorem@1.0.0
| \`-- url: https://example.com/lorem
\`-- bar@1.0.0
+-- type: individual
+-- url: http://example.com/donate
\`-- sub-bar@1.0.0
\`-- url: https://example.com/sponsor


`

exports[`test/tap/fund.js TAP fund containing multi-level nested deps with no funding, using --json option > should omit dependencies with no funding declared 1`] = `
{
length: 3,
name: 'nested-no-funding-packages',
version: '1.0.0',
dependencies: {
lorem: { version: '1.0.0', funding: { url: 'https://example.com/lorem' } },
bar: {
version: '1.0.0',
funding: { type: 'individual', url: 'http://example.com/donate' },
dependencies: {
'sub-bar': {
version: '1.0.0',
funding: { url: 'https://example.com/sponsor' }
}
}
}
}
}
`

exports[`test/tap/fund.js TAP fund does not support global > should throw EFUNDGLOBAL error 1`] = `

`
Expand All @@ -48,52 +27,17 @@ npm ERR! code EFUNDGLOBAL
npm ERR! \`npm fund\` does not support globals
`

exports[`test/tap/fund.js TAP fund does not support global, using --json option > should throw EFUNDGLOBAL error 1`] = `
{
error: {
code: 'EFUNDGLOBAL',
summary: '\`npm fund\` does not support globals',
detail: ''
}
}
`

exports[`test/tap/fund.js TAP fund does not support global, using --json option > should write error msgs to stderr 1`] = `
npm ERR! code EFUNDGLOBAL
npm ERR! \`npm fund\` does not support globals
`

exports[`test/tap/fund.js TAP fund in which same maintainer owns all its deps > should print stack packages together 1`] = `
maintainer-owns-all-deps@1.0.0, dep-bar@1.0.0, dep-sub-foo@1.0.0, dep-foo@1.0.0
├── type: individual
└── url: http://example.com/donate

+-- type: individual
\`-- url: http://example.com/donate

`

exports[`test/tap/fund.js TAP fund in which same maintainer owns all its deps, using --json option > should print stack packages together 1`] = `
{
length: 3,
name: 'maintainer-owns-all-deps',
version: '1.0.0',
funding: { type: 'individual', url: 'http://example.com/donate' },
dependencies: {
'dep-bar': {
version: '1.0.0',
funding: { type: 'individual', url: 'http://example.com/donate' }
},
'dep-foo': {
version: '1.0.0',
funding: { type: 'individual', url: 'http://example.com/donate' },
dependencies: {
'dep-sub-foo': {
version: '1.0.0',
funding: { type: 'individual', url: 'http://example.com/donate' }
}
}
}
}
}
`

exports[`test/tap/fund.js TAP fund using package argument with no browser > should open funding url 1`] = `
Expand All @@ -103,24 +47,8 @@ http://example.com/donate

`

exports[`test/tap/fund.js TAP fund using package argument with no browser, using --json option > should open funding url 1`] = `
{
title: 'individual funding available at the following URL',
url: 'http://example.com/donate'
}
`

exports[`test/tap/fund.js TAP fund with no package containing funding > should print empty funding info 1`] = `
no-funding-package@0.0.0


`

exports[`test/tap/fund.js TAP fund with no package containing funding, using --json option > should print empty funding info 1`] = `
{
length: 0,
name: 'no-funding-package',
version: '0.0.0',
dependencies: {}
}
`
130 changes: 110 additions & 20 deletions test/tap/fund.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,29 +114,32 @@ function checkOutput (t, { code, stdout, stderr }) {
t.is(stderr, '', 'no warnings')
}

function testFundCmd ({ title, expect, args = [], opts = {}, output = checkOutput }) {
const validate = (t, json) => (err, code, stdout, stderr) => {
function jsonTest (t, { assertionMsg, expected, stdout }) {
let parsed = stdout

t.doesNotThrow(function () {
parsed = JSON.parse(stdout)
}, 'valid JSON')

t.deepEqual(parsed, expected, assertionMsg)
}

function snapshotTest (t, { stdout, assertionMsg }) {
t.matchSnapshot(stdout, assertionMsg)
}

function testFundCmd ({ title, assertionMsg, args = [], opts = {}, output = checkOutput, assertion = snapshotTest, expected }) {
const validate = (t) => (err, code, stdout, stderr) => {
if (err) throw err

output(t, { code, stdout, stderr })
assertion(t, { assertionMsg, expected, stdout })

let parsed = stdout
if (json) {
t.doesNotThrow(function () {
parsed = JSON.parse(stdout)
}, 'valid JSON')
}

t.matchSnapshot(parsed, expect)
t.end()
}

test(title, (t) => {
common.npm(['fund'].concat(args), opts, validate(t))
})

test(`${title}, using --json option`, (t) => {
common.npm(['fund', '--json'].concat(args), opts, validate(t, true))
common.npm(['fund', '--unicode=false'].concat(args), opts, validate(t))
})
}

Expand All @@ -148,25 +151,81 @@ test('setup', function (t) {

testFundCmd({
title: 'fund with no package containing funding',
expect: 'should print empty funding info',
assertionMsg: 'should print empty funding info',
opts: { cwd: noFunding }
})

testFundCmd({
title: 'fund in which same maintainer owns all its deps',
expect: 'should print stack packages together',
assertionMsg: 'should print stack packages together',
opts: { cwd: maintainerOwnsAllDeps }
})

testFundCmd({
title: 'fund in which same maintainer owns all its deps, using --json option',
assertionMsg: 'should print stack packages together',
args: ['--json'],
opts: { cwd: maintainerOwnsAllDeps },
assertion: jsonTest,
expected: {
length: 3,
name: 'maintainer-owns-all-deps',
version: '1.0.0',
funding: { type: 'individual', url: 'http://example.com/donate' },
dependencies: {
'dep-bar': {
version: '1.0.0',
funding: { type: 'individual', url: 'http://example.com/donate' }
},
'dep-foo': {
version: '1.0.0',
funding: { type: 'individual', url: 'http://example.com/donate' },
dependencies: {
'dep-sub-foo': {
version: '1.0.0',
funding: { type: 'individual', url: 'http://example.com/donate' }
}
}
}
}
}
})

testFundCmd({
title: 'fund containing multi-level nested deps with no funding',
expect: 'should omit dependencies with no funding declared',
assertionMsg: 'should omit dependencies with no funding declared',
opts: { cwd: nestedNoFundingPackages }
})

testFundCmd({
title: 'fund containing multi-level nested deps with no funding, using --json option',
assertionMsg: 'should omit dependencies with no funding declared',
args: ['--json'],
opts: { cwd: nestedNoFundingPackages },
assertion: jsonTest,
expected: {
length: 3,
name: 'nested-no-funding-packages',
version: '1.0.0',
dependencies: {
lorem: { version: '1.0.0', funding: { url: 'https://example.com/lorem' } },
bar: {
version: '1.0.0',
funding: { type: 'individual', url: 'http://example.com/donate' },
dependencies: {
'sub-bar': {
version: '1.0.0',
funding: { url: 'https://example.com/sponsor' }
}
}
}
}
}
})

testFundCmd({
title: 'fund does not support global',
expect: 'should throw EFUNDGLOBAL error',
assertionMsg: 'should throw EFUNDGLOBAL error',
args: ['--global'],
output: (t, { code, stdout, stderr }) => {
t.is(code, 1, `exited code 0`)
Expand All @@ -175,13 +234,44 @@ testFundCmd({
}
})

testFundCmd({
title: 'fund does not support global, using --json option',
assertionMsg: 'should throw EFUNDGLOBAL error',
args: ['--global', '--json'],
output: (t, { code, stdout, stderr }) => {
t.is(code, 1, `exited code 0`)
const [ errCode, errCmd ] = stderr.split('\n')
t.matchSnapshot(`${errCode}\n${errCmd}`, 'should write error msgs to stderr')
},
assertion: jsonTest,
expected: {
error: {
code: 'EFUNDGLOBAL',
summary: '\`npm fund\` does not support globals',
detail: ''
}
}
})

testFundCmd({
title: 'fund using package argument with no browser',
expect: 'should open funding url',
assertionMsg: 'should open funding url',
args: ['--browser', 'undefined', '.'],
opts: { cwd: maintainerOwnsAllDeps }
})

testFundCmd({
title: 'fund using package argument with no browser, using --json option',
assertionMsg: 'should open funding url',
args: ['--json', '--browser', 'undefined', '.'],
opts: { cwd: maintainerOwnsAllDeps },
assertion: jsonTest,
expected: {
title: 'individual funding available at the following URL',
url: 'http://example.com/donate'
}
})

test('fund using package argument', function (t) {
const fakeBrowser = path.join(common.pkg, '_script.sh')
const outFile = path.join(common.pkg, '_output')
Expand Down