Skip to content

Commit 57dbf5c

Browse files
author
eddie.stanley
committed
[GH-788] Added a test case to prove that #788 has been fixed (this fails prior to f2fe81f)
1 parent f2fe81f commit 57dbf5c

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

tests/test-cases.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2649,3 +2649,52 @@ describe('Custom Tests', function() {
26492649
generateTest(CUSTOM_TESTS[i]);
26502650
}
26512651
});
2652+
2653+
describe("Asynchronous tests", () => {
2654+
it("When parsing synchronously inside a web-worker not owned by PapaParse we should not invoke postMessage", async () => {
2655+
// Arrange
2656+
const papaParseScriptPath = new URL("../papaparse.js", document.baseURI).href;
2657+
2658+
// Define our custom web-worker that loads PapaParse and executes a synchronous parse
2659+
const blob = new Blob([
2660+
`
2661+
importScripts('${papaParseScriptPath}');
2662+
2663+
self.addEventListener("message", function(event) {
2664+
if (event.data === "ExecuteParse") {
2665+
// Perform our synchronous parse, as requested
2666+
const results = Papa.parse('x\\ny\\n');
2667+
postMessage({type: "ParseExecutedSuccessfully", results});
2668+
} else {
2669+
// Otherwise, send whatever we received back. We shouldn't be hitting this (!) If we're reached
2670+
// this it means PapaParse thinks it is running inside a web-worker that it owns
2671+
postMessage(event.data);
2672+
}
2673+
});
2674+
`
2675+
], {type: 'text/javascript'});
2676+
2677+
const blobURL = window.URL.createObjectURL(blob);
2678+
const webWorker = new Worker(blobURL);
2679+
2680+
const receiveMessagePromise = new Promise((resolve, reject) => {
2681+
webWorker.addEventListener("message", event => {
2682+
if (event.data.type === "ParseExecutedSuccessfully") {
2683+
resolve(event.data);
2684+
} else {
2685+
const error = new Error(`Received unexpected message: ${JSON.stringify(event.data, null, 2)}`);
2686+
error.data = event.data;
2687+
reject(error);
2688+
}
2689+
});
2690+
});
2691+
2692+
// Act
2693+
webWorker.postMessage("ExecuteParse");
2694+
const webWorkerMessage = await receiveMessagePromise;
2695+
2696+
// Assert
2697+
assert.equal("ParseExecutedSuccessfully", webWorkerMessage.type);
2698+
assert.equal(3, webWorkerMessage.results.data.length);
2699+
});
2700+
});

0 commit comments

Comments
 (0)