-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: supertape: add ability to use console.log in worker mode
- Loading branch information
1 parent
188ab81
commit 023c64d
Showing
6 changed files
with
273 additions
and
1 deletion.
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,47 @@ | ||
'use strict'; | ||
|
||
const {stringify} = require('flatted'); | ||
const SPLITTER = '>[supertape-splitter]<'; | ||
const CONSOLE_LOG = 'console:log'; | ||
const CONSOLE_ERROR = 'console:error'; | ||
|
||
module.exports.CONSOLE_ERROR = CONSOLE_ERROR; | ||
module.exports.CONSOLE_LOG = CONSOLE_LOG; | ||
module.exports.SPLITTER = SPLITTER; | ||
|
||
module.exports.overrideConsoleLog = (parentPort, {console = global.console} = {}) => { | ||
const {log} = console; | ||
|
||
console.log = createConsoleMethod(CONSOLE_LOG, parentPort); | ||
return { | ||
getBackConsoleLog: () => { | ||
console.log = log; | ||
}, | ||
}; | ||
}; | ||
|
||
module.exports.overrideConsoleError = (parentPort, {console = global.console} = {}) => { | ||
const {error} = console; | ||
|
||
console.error = createConsoleMethod(CONSOLE_ERROR, parentPort); | ||
return { | ||
getBackConsoleError: () => { | ||
console.error = error; | ||
}, | ||
}; | ||
}; | ||
|
||
const createConsoleMethod = (type, parentPort) => (...messages) => { | ||
const prepared = []; | ||
|
||
for (const message of messages) | ||
prepared.push(stringify(message)); | ||
|
||
parentPort.postMessage([ | ||
type, { | ||
message: prepared.join(SPLITTER), | ||
}, | ||
]); | ||
}; | ||
|
||
module.exports._createConsoleLog = createConsoleMethod; |
179 changes: 179 additions & 0 deletions
179
packages/supertape/lib/worker/create-console-log.spec.js
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,179 @@ | ||
'use strict'; | ||
|
||
const {test, stub} = require('supertape'); | ||
const { | ||
overrideConsoleLog, | ||
CONSOLE_LOG, | ||
} = require('./create-console-log'); | ||
|
||
const {stringify} = require('flatted'); | ||
|
||
test('supertape: worker: create-console-log', (t) => { | ||
const log = stub(); | ||
const consoleStub = { | ||
log, | ||
}; | ||
|
||
const parentPort = { | ||
postMessage: stub(), | ||
}; | ||
|
||
overrideConsoleLog(parentPort, { | ||
console: consoleStub, | ||
}); | ||
|
||
consoleStub.log('hello'); | ||
|
||
t.notCalled(log); | ||
t.end(); | ||
}); | ||
|
||
test('supertape: worker: create-console-log: get back', (t) => { | ||
const log = stub(); | ||
const consoleStub = { | ||
log, | ||
}; | ||
|
||
const parentPort = { | ||
postMessage: stub(), | ||
}; | ||
|
||
const {getBackConsoleLog} = overrideConsoleLog(parentPort, { | ||
console: consoleStub, | ||
}); | ||
|
||
consoleStub.log('hello'); | ||
|
||
getBackConsoleLog(); | ||
|
||
consoleStub.log('world'); | ||
|
||
t.calledWith(log, ['world']); | ||
t.end(); | ||
}); | ||
|
||
test('supertape: worker: create-console-log: postMessage', (t) => { | ||
const log = stub(); | ||
const consoleStub = { | ||
log, | ||
}; | ||
|
||
const postMessage = stub(); | ||
|
||
const parentPort = { | ||
postMessage, | ||
}; | ||
|
||
overrideConsoleLog(parentPort, { | ||
console: consoleStub, | ||
}); | ||
|
||
consoleStub.log('hello'); | ||
|
||
const arg = [ | ||
CONSOLE_LOG, { | ||
message: '["hello"]', | ||
}, | ||
]; | ||
|
||
t.calledWith(postMessage, [arg]); | ||
t.end(); | ||
}); | ||
|
||
test('supertape: worker: create-console-log: postMessage: array', (t) => { | ||
const log = stub(); | ||
const consoleStub = { | ||
log, | ||
}; | ||
|
||
const postMessage = stub(); | ||
|
||
const parentPort = { | ||
postMessage, | ||
}; | ||
|
||
const {getBackConsoleLog} = overrideConsoleLog(parentPort, { | ||
console: consoleStub, | ||
}); | ||
|
||
consoleStub.log([1, 2]); | ||
|
||
getBackConsoleLog(); | ||
|
||
const arg = [ | ||
CONSOLE_LOG, { | ||
message: '[[1,2]]', | ||
}, | ||
]; | ||
|
||
t.calledWith(postMessage, [arg]); | ||
t.end(); | ||
}); | ||
|
||
test('supertape: worker: create-console-log: postMessage: object', (t) => { | ||
const log = stub(); | ||
const consoleStub = { | ||
log, | ||
}; | ||
|
||
const postMessage = stub(); | ||
|
||
const parentPort = { | ||
postMessage, | ||
}; | ||
|
||
overrideConsoleLog(parentPort, { | ||
console: consoleStub, | ||
}); | ||
|
||
const objectB = {}; | ||
|
||
const objectA = { | ||
objectB, | ||
}; | ||
|
||
objectA.objectB = { | ||
objectA, | ||
}; | ||
|
||
consoleStub.log(objectA); | ||
|
||
const arg = [ | ||
CONSOLE_LOG, { | ||
message: stringify(objectA), | ||
}, | ||
]; | ||
|
||
t.calledWith(postMessage, [arg]); | ||
t.end(); | ||
}); | ||
|
||
test('supertape: worker: create-console-log: postMessage: simple object', (t) => { | ||
const log = stub(); | ||
const consoleStub = { | ||
log, | ||
}; | ||
|
||
const postMessage = stub(); | ||
|
||
const parentPort = { | ||
postMessage, | ||
}; | ||
|
||
overrideConsoleLog(parentPort, { | ||
console: consoleStub, | ||
}); | ||
|
||
consoleStub.log({ | ||
hello: 'world', | ||
}); | ||
|
||
const arg = [ | ||
CONSOLE_LOG, { | ||
message: '[{"hello":"1"},"world"]', | ||
}, | ||
]; | ||
|
||
t.calledWith(postMessage, [arg]); | ||
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