Skip to content

Commit f3a87c6

Browse files
committed
chore: use structured cloning in the tests
1 parent cd33f82 commit f3a87c6

File tree

5 files changed

+67
-17
lines changed

5 files changed

+67
-17
lines changed

__tests__/__utils__/WorkerMock.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/* @flow */
22
/* eslint-env node */
33

4+
import clone from 'structured-clone';
5+
46
type Worker = {
57
addEventListener: (name: 'message', cb: (e: any) => mixed) => mixed,
68
removeEventListener: (name: 'message', cb: (e: any) => mixed) => mixed,
@@ -15,7 +17,7 @@ const createScope = onMessage => {
1517
return {
1618
self: {
1719
postMessage(data) {
18-
onMessage(JSON.parse(JSON.stringify(data)));
20+
onMessage(clone(data));
1921
},
2022
addEventListener(name: 'message', listener) {
2123
listeners.push(listener);
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// flow-typed signature: 0b09a31754af9c00b5b5b282961811b4
2+
// flow-typed version: <<STUB>>/structured-clone_v^0.2.2/flow_v0.79.1
3+
4+
/**
5+
* This is an autogenerated libdef stub for:
6+
*
7+
* 'structured-clone'
8+
*
9+
* Fill this stub out by replacing all the `any` types.
10+
*
11+
* Once filled out, we encourage you to share your work with the
12+
* community by sending a pull request to:
13+
* https://github.com/flowtype/flow-typed
14+
*/
15+
16+
declare module 'structured-clone' {
17+
declare module.exports: any;
18+
}
19+
20+
/**
21+
* We include stubs for each file inside this npm package in case you need to
22+
* require those files directly. Feel free to delete any files that aren't
23+
* needed.
24+
*/
25+
declare module 'structured-clone/clone' {
26+
declare module.exports: any;
27+
}
28+
29+
declare module 'structured-clone/serialize' {
30+
declare module.exports: any;
31+
}
32+
33+
declare module 'structured-clone/test/test' {
34+
declare module.exports: any;
35+
}
36+
37+
// Filename aliases
38+
declare module 'structured-clone/clone.js' {
39+
declare module.exports: $Exports<'structured-clone/clone'>;
40+
}
41+
declare module 'structured-clone/index' {
42+
declare module.exports: $Exports<'structured-clone'>;
43+
}
44+
declare module 'structured-clone/index.js' {
45+
declare module.exports: $Exports<'structured-clone'>;
46+
}
47+
declare module 'structured-clone/serialize.js' {
48+
declare module.exports: $Exports<'structured-clone/serialize'>;
49+
}
50+
declare module 'structured-clone/test/test.js' {
51+
declare module.exports: $Exports<'structured-clone/test/test'>;
52+
}

package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,13 @@
3838
"husky": "^0.14.3",
3939
"jest": "^23.5.0",
4040
"parcel": "^1.9.7",
41-
"prettier": "^1.13.5"
41+
"prettier": "^1.13.5",
42+
"structured-clone": "^0.2.2"
4243
},
4344
"jest": {
4445
"testEnvironment": "node",
45-
"testMatch": ["**/__tests__/**/*.test.js"]
46+
"testMatch": [
47+
"**/__tests__/**/*.test.js"
48+
]
4649
}
4750
}

src/index.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,11 @@ export function create(worker: Worker): any {
6565
global
6666
: null;
6767

68-
const { constructor, message, stack } = e.data.error;
68+
const { name, message, stack } = e.data.error;
6969

7070
// If the error was for current action, reject the promise
7171
// Try to preserve the error constructor, e.g. TypeError
72-
const ErrorConstructor =
73-
g && g[constructor] ? g[constructor] : Error;
72+
const ErrorConstructor = g && g[name] ? g[name] : Error;
7473

7574
const error = new ErrorConstructor(message);
7675

@@ -149,7 +148,7 @@ export function proxy(o: Object, target?: Worker = self) {
149148
// Create an error response
150149
// Since we cannot send the error object, we send necessary info to recreate it
151150
const error = e => ({
152-
constructor: e.constructor.name,
151+
name: e.constructor.name,
153152
message: e.message,
154153
stack: e.stack,
155154
});

yarn.lock

+4-10
Original file line numberDiff line numberDiff line change
@@ -4523,10 +4523,6 @@ nan@^2.0.7, nan@^2.9.2:
45234523
version "2.10.0"
45244524
resolved "https://registry.yarnpkg.com/nan/-/nan-2.10.0.tgz#96d0cd610ebd58d4b4de9cc0c6828cda99c7548f"
45254525

4526-
nanoid@^1.0.7:
4527-
version "1.2.1"
4528-
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-1.2.1.tgz#922bf6c10e35f7b208993768dad643577c907adf"
4529-
45304526
nanomatch@^1.2.9:
45314527
version "1.2.13"
45324528
resolved "https://registry.yarnpkg.com/nanomatch/-/nanomatch-1.2.13.tgz#b87a8aa4fc0de8fe6be88895b38983ff265bd119"
@@ -6295,12 +6291,6 @@ shellwords@^0.1.1:
62956291
version "0.1.1"
62966292
resolved "https://registry.yarnpkg.com/shellwords/-/shellwords-0.1.1.tgz#d6b9181c1a48d397324c84871efbcfc73fc0654b"
62976293

6298-
shortid@^2.2.13:
6299-
version "2.2.13"
6300-
resolved "https://registry.yarnpkg.com/shortid/-/shortid-2.2.13.tgz#b2441e71c664ace458a341d343959f677910ef5b"
6301-
dependencies:
6302-
nanoid "^1.0.7"
6303-
63046294
sigmund@^1.0.1:
63056295
version "1.0.1"
63066296
resolved "https://registry.yarnpkg.com/sigmund/-/sigmund-1.0.1.tgz#3ff21f198cad2175f9f3b781853fd94d0d19b590"
@@ -6613,6 +6603,10 @@ strip-json-comments@~2.0.1:
66136603
version "2.0.1"
66146604
resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-2.0.1.tgz#3c531942e908c2697c0ec344858c286c7ca0a60a"
66156605

6606+
structured-clone@^0.2.2:
6607+
version "0.2.2"
6608+
resolved "https://registry.yarnpkg.com/structured-clone/-/structured-clone-0.2.2.tgz#ac92b6be31958a643db30f1335abc6a1b02dfdc2"
6609+
66166610
stylehacks@^4.0.0:
66176611
version "4.0.0"
66186612
resolved "https://registry.yarnpkg.com/stylehacks/-/stylehacks-4.0.0.tgz#64b323951c4a24e5fc7b2ec06c137bf32d155e8a"

0 commit comments

Comments
 (0)