| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,14 +8,18 @@ const assert = require('node:assert/strict'); | |||
| 8 | 8 | const stackFramesRegexp = /(\s+)((.+?)\s+\()?(?:\(?(.+?):(\d+)(?::(\d+))?)\)?(\s+\{)?(\n|$)/g; | |
| 9 | 9 | const windowNewlineRegexp = /\r/g; | |
| 10 | 10 | ||
| 11 | - function replaceStackTrace(str) { | ||
| 12 | - return str.replace(stackFramesRegexp, '$1*$7\n'); | ||
| 11 | + function replaceStackTrace(str, replacement = '$1*$7\n') { | ||
| 12 | + return str.replace(stackFramesRegexp, replacement); | ||
| 13 | 13 | } | |
| 14 | 14 | ||
| 15 | 15 | function replaceWindowsLineEndings(str) { | |
| 16 | 16 | return str.replace(windowNewlineRegexp, ''); | |
| 17 | 17 | } | |
| 18 | 18 | ||
| 19 | + function replaceWindowsPaths(str) { | ||
| 20 | + return str.replaceAll(path.win32.sep, path.posix.sep); | ||
| 21 | + } | ||
| 22 | + | ||
| 19 | 23 | function transform(...args) { | |
| 20 | 24 | return (str) => args.reduce((acc, fn) => fn(acc), str); | |
| 21 | 25 | } | |
@@ -35,19 +39,32 @@ async function assertSnapshot(actual, filename = process.argv[1]) { | |||
| 35 | 39 | } | |
| 36 | 40 | } | |
| 37 | 41 | ||
| 42 | + /** | ||
| 43 | + * Spawn a process and assert its output against a snapshot. | ||
| 44 | + * if you want to automatically update the snapshot, run tests with NODE_REGENERATE_SNAPSHOTS=1 | ||
| 45 | + * transform is a function that takes the output and returns a string that will be compared against the snapshot | ||
| 46 | + * this is useful for normalizing output such as stack traces | ||
| 47 | + * there are some predefined transforms in this file such as replaceStackTrace and replaceWindowsLineEndings | ||
| 48 | + * both of which can be used as an example for writing your own | ||
| 49 | + * compose multiple transforms by passing them as arguments to the transform function: | ||
| 50 | + * assertSnapshot.transform(assertSnapshot.replaceStackTrace, assertSnapshot.replaceWindowsLineEndings) | ||
| 51 | + * | ||
| 52 | + * @param {string} filename | ||
| 53 | + * @param {function(string): string} [transform] | ||
| 54 | + * @returns {Promise<void>} | ||
| 55 | + */ | ||
| 38 | 56 | async function spawnAndAssert(filename, transform = (x) => x) { | |
| 39 | - // TODO: Add an option to this function to alternatively or additionally compare stderr. | ||
| 40 | - // For now, tests that want to check stderr or both stdout and stderr can use spawnPromisified. | ||
| 41 | 57 | const flags = common.parseTestFlags(filename); | |
| 42 | - const { stdout } = await common.spawnPromisified(process.execPath, [...flags, filename]); | ||
| 43 | - await assertSnapshot(transform(stdout), filename); | ||
| 58 | + const { stdout, stderr } = await common.spawnPromisified(process.execPath, [...flags, filename]); | ||
| 59 | + await assertSnapshot(transform(`${stdout}${stderr}`), filename); | ||
| 44 | 60 | } | |
| 45 | 61 | ||
| 46 | 62 | module.exports = { | |
| 47 | 63 | assertSnapshot, | |
| 48 | 64 | getSnapshotPath, | |
| 49 | 65 | replaceStackTrace, | |
| 50 | 66 | replaceWindowsLineEndings, | |
| 67 | + replaceWindowsPaths, | ||
| 51 | 68 | spawnAndAssert, | |
| 52 | 69 | transform, | |
| 53 | 70 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -69,7 +69,7 @@ function parseTestFlags(filename = process.argv[1]) { | |||
| 69 | 69 | fs.closeSync(fd); | |
| 70 | 70 | const source = buffer.toString('utf8', 0, bytesRead); | |
| 71 | 71 | ||
| 72 | - const flagStart = source.indexOf('// Flags: --') + 10; | ||
| 72 | + const flagStart = source.search(/\/\/ Flags:\s+--/) + 10; | ||
| 73 | 73 | ||
| 74 | 74 | if (flagStart === 9) { | |
| 75 | 75 | return []; | |
@@ -82,7 +82,8 @@ function parseTestFlags(filename = process.argv[1]) { | |||
| 82 | 82 | return source | |
| 83 | 83 | .substring(flagStart, flagEnd) | |
| 84 | 84 | .replace(/_/g, '-') | |
| 85 | - .split(' '); | ||
| 85 | + .split(/\s+/) | ||
| 86 | + .filter(Boolean); | ||
| 86 | 87 | } | |
| 87 | 88 | ||
| 88 | 89 | // Check for flags. Skip this for workers (both, the `cluster` module and | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,7 +20,7 @@ | |||
| 20 | 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| 21 | 21 | ||
| 22 | 22 | 'use strict'; | |
| 23 | - require('../common'); | ||
| 23 | + require('../../common'); | ||
| 24 | 24 | ||
| 25 | 25 | console.log([ | |
| 26 | 26 | '_______________________________________________50', | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,5 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | ||
| 3 | - require('../common'); | ||
| 3 | + require('../../common'); | ||
| 4 | 4 | ||
| 5 | 5 | console.trace('foo'); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,5 @@ | |||
| 1 | 1 | Trace: foo | |
| 2 | - at Object.<anonymous> (*console.js:*:*) | ||
| 2 | + at * | ||
| 3 | 3 | at * | |
| 4 | 4 | at * | |
| 5 | 5 | at * | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,7 +7,7 @@ Object.defineProperty(global, 'console', { | |||
| 7 | 7 | value: {}, | |
| 8 | 8 | }); | |
| 9 | 9 | ||
| 10 | - require('../common'); | ||
| 10 | + require('../../common'); | ||
| 11 | 11 | ||
| 12 | 12 | // This test checks that, if Node cannot put together the `console` object | |
| 13 | 13 | // because it is low on stack space while doing so, it can succeed later | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,6 +20,6 @@ | |||
| 20 | 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| 21 | 21 | ||
| 22 | 22 | 'use strict'; | |
| 23 | - require('../common'); | ||
| 23 | + require('../../common'); | ||
| 24 | 24 | ||
| 25 | 25 | console.log('hello world'); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments