-
-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for overriding libtap's internal settings
- Loading branch information
Showing
6 changed files
with
269 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* IMPORTANT | ||
* This snapshot file is auto-generated, but designed for humans. | ||
* It should be checked into source control and tracked carefully. | ||
* Re-generate by setting TAP_SNAPSHOT=1 and running tests. | ||
* Make sure to inspect the output below. Do not ignore changes! | ||
*/ | ||
'use strict' | ||
exports[`test/run/libtap-settings.js TAP print out a different snapshot file location > must match snapshot 1`] = ` | ||
TAP version 13 | ||
ok 1 - {CWD}/test/run/tap-testdir-libtap-settings/test.js # SKIP { | ||
# some-path/test.js.test.cjs | ||
1..0 | ||
# {time} | ||
} | ||
1..1 | ||
# skip: 1 | ||
# {time} | ||
` | ||
|
||
exports[`test/run/libtap-settings.js TAP print out the normal snapshot file location > must match snapshot 1`] = ` | ||
TAP version 13 | ||
ok 1 - {CWD}/test/run/tap-testdir-libtap-settings/test.js # SKIP { | ||
# {CWD}/test/run/tap-testdir-libtap-settings/tap-snapshots/test.js.test.cjs | ||
1..0 | ||
# {time} | ||
} | ||
1..1 | ||
# skip: 1 | ||
# {time} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
const { | ||
tmpfile, | ||
run, | ||
tap, | ||
t, | ||
} = require('./') | ||
|
||
const { resolve } = require('path') | ||
|
||
const path = t.testdir({ | ||
settings: { | ||
'ok.js': ` | ||
module.exports = { | ||
snapshotFile: (cwd, main, argv) => 'some-path/' + main + argv + '.test.cjs' | ||
} | ||
`, | ||
'ok-empty.js': ` | ||
module.exports = {} | ||
`, | ||
'unknown-field.js': ` | ||
module.exports = { | ||
foo: 'bar' | ||
} | ||
`, | ||
'wrong-type-field.js': ` | ||
module.exports = { | ||
snapshotFile: 75 | ||
} | ||
`, | ||
'export-function.js': ` | ||
module.exports = () => {} | ||
`, | ||
'export-array.js': ` | ||
module.exports = [1, 2, 3] | ||
`, | ||
'export-false.js': ` | ||
module.exports = false | ||
`, | ||
'export-null.js': ` | ||
module.exports = null | ||
`, | ||
}, | ||
'test.js': ` | ||
const t = require(${tap}) | ||
t.comment(t.snapshotFile) | ||
`, | ||
}) | ||
|
||
const testFile = resolve(path, 'test.js') | ||
const settings = n => `--libtap-settings=settings/${n}.js` | ||
|
||
t.test('print out a different snapshot file location', t => { | ||
run([settings('ok'), testFile], {cwd: path}, (er, o, e) => { | ||
t.notOk(er) | ||
t.equal(e, '') | ||
t.matchSnapshot(o) | ||
t.end() | ||
}) | ||
}) | ||
|
||
t.test('print out the normal snapshot file location', t => { | ||
run([settings('ok-empty'), testFile], {cwd: path}, (er, o, e) => { | ||
t.notOk(er) | ||
t.equal(e, '') | ||
t.matchSnapshot(o) | ||
t.end() | ||
}) | ||
}) | ||
|
||
t.test('fails if module not found', t => { | ||
run([settings('does-not-exist'), testFile], {cwd: path}, (er, o, e) => { | ||
t.match(er, { code: 1 }) | ||
t.match(e, 'Error: ') // specific msg different across node versions | ||
t.equal(o, '') | ||
t.end() | ||
}) | ||
}) | ||
|
||
t.test('adding an unknown field is invalid', t => { | ||
run([settings('unknown-field'), testFile], {cwd: path}, (er, o, e) => { | ||
t.match(er, { code: 1 }) | ||
t.match(e, 'Error: Unrecognized libtap setting: foo') | ||
t.equal(o, '') | ||
t.end() | ||
}) | ||
}) | ||
|
||
t.test('fields must be same type as libtap defines', t => { | ||
run([settings('wrong-type-field'), testFile], {cwd: path}, (er, o, e) => { | ||
t.match(er, { code: 1 }) | ||
t.match(e, 'Error: Invalid type for libtap setting snapshotFile. Expected function, received number.') | ||
t.equal(o, '') | ||
t.end() | ||
}) | ||
}) | ||
|
||
t.test('exporting function is invalid', t => { | ||
run([settings('export-function'), testFile], {cwd: path}, (er, o, e) => { | ||
t.match(er, { code: 1 }) | ||
t.match(e, 'Error: invalid libtap settings: function') | ||
t.equal(o, '') | ||
t.end() | ||
}) | ||
}) | ||
|
||
t.test('exporting array is invalid', t => { | ||
run([settings('export-array'), testFile], {cwd: path}, (er, o, e) => { | ||
t.match(er, { code: 1 }) | ||
t.match(e, 'Error: invalid libtap settings: array') | ||
t.equal(o, '') | ||
t.end() | ||
}) | ||
}) | ||
|
||
t.test('exporting false is invalid', t => { | ||
run([settings('export-false'), testFile], {cwd: path}, (er, o, e) => { | ||
t.match(er, { code: 1 }) | ||
t.match(e, 'Error: invalid libtap settings: boolean') | ||
t.equal(o, '') | ||
t.end() | ||
}) | ||
}) | ||
|
||
t.test('exporting null is invalid', t => { | ||
run([settings('export-null'), testFile], {cwd: path}, (er, o, e) => { | ||
t.match(er, { code: 1 }) | ||
t.match(e, 'Error: invalid libtap settings: null') | ||
t.equal(o, '') | ||
t.end() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const t = require('../..') | ||
const { resolve } = require('path') | ||
|
||
t.afterEach(() => delete process.env.TAP_LIBTAP_SETTINGS) | ||
|
||
t.test('override the snapshot location', t => { | ||
process.env.TAP_LIBTAP_SETTINGS = resolve(t.testdir({ | ||
'settings.js': ` | ||
module.exports = { | ||
snapshotFile: (cwd, main, argv) => 'some-path/' + main + argv + '.snap' | ||
} | ||
` | ||
}), 'settings.js') | ||
const settings = t.mock('../../settings.js') | ||
t.equal(settings.snapshotFile('a', 'b', 'c'), 'some-path/bc.snap') | ||
t.end() | ||
}) | ||
|
||
t.test('no fields, thats ok', t => { | ||
process.env.TAP_LIBTAP_SETTINGS = resolve(t.testdir({ | ||
'settings.js': ` | ||
module.exports = {} | ||
` | ||
}), 'settings.js') | ||
const settings = t.mock('../../settings.js') | ||
t.strictSame(settings, require('../../settings.js')) | ||
t.end() | ||
}) | ||
|
||
t.test('wrong export tests', t => { | ||
const cases = Object.entries({ | ||
function: '() => {}', | ||
boolean: 'false', | ||
array: '[]', | ||
null: 'null', | ||
'unknown field': `{ foo: 'bar' }`, | ||
'wrong field type': '{ snapshotFile: 1234 }', | ||
}) | ||
t.plan(cases.length) | ||
for (const [name, code] of cases) { | ||
t.test(`export ${name}`, t => { | ||
process.env.TAP_LIBTAP_SETTINGS = resolve(t.testdir({ | ||
'settings.js': ` | ||
module.exports = ${code} | ||
` | ||
}), 'settings.js') | ||
t.throws(() => t.mock('../../settings.js')) | ||
t.end() | ||
}) | ||
} | ||
}) |