• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

boardgameio / boardgame.io / 15797955584

21 Jun 2025 05:20PM UTC coverage: 97.263% (-2.7%) from 100.0%
15797955584

Pull #1226

github

web-flow
Merge 8ac078c34 into 4f3c90df0
Pull Request #1226: Koa to express

1644 of 1714 branches covered (95.92%)

Branch coverage included in aggregate %.

347 of 349 new or added lines in 3 files covered. (99.43%)

228 existing lines in 10 files now uncovered.

9337 of 9576 relevant lines covered (97.5%)

6155.46 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.92
/src/plugins/plugin-serializable.ts
1
import type { Plugin } from '../types';
4✔
2
import isPlainObject from 'lodash.isplainobject';
4✔
3

4✔
4
/**
4✔
5
 * Check if a value can be serialized (e.g. using `JSON.stringify`).
4✔
6
 * Adapted from: https://stackoverflow.com/a/30712764/3829557
4✔
7
 */
4✔
8
function isSerializable(value: any) {
16,988✔
9
  // Primitives are OK.
16,988✔
10
  if (
16,988✔
11
    value === undefined ||
16,988✔
12
    value === null ||
16,988✔
13
    typeof value === 'boolean' ||
16,988✔
14
    typeof value === 'number' ||
16,988✔
15
    typeof value === 'string'
15,844✔
16
  ) {
16,988✔
17
    return true;
1,684✔
18
  }
1,684✔
19

15,304✔
20
  // A non-primitive value that is neither a POJO or an array cannot be serialized.
15,304✔
21
  if (!isPlainObject(value) && !Array.isArray(value)) {
16,988!
UNCOV
22
    return false;
×
UNCOV
23
  }
×
24

15,304✔
25
  // Recurse entries if the value is an object or array.
15,304✔
26
  for (const key in value) {
16,988✔
27
    if (!isSerializable(value[key])) return false;
1,268!
28
  }
1,268✔
29

15,304✔
30
  return true;
15,304✔
31
}
15,304✔
32

4✔
33
/**
4✔
34
 * Plugin that checks whether state is serializable, in order to avoid
4✔
35
 * network serialization bugs.
4✔
36
 */
4✔
37
const SerializablePlugin: Plugin = {
4✔
38
  name: 'plugin-serializable',
4✔
39

4✔
40
  fnWrap:
4✔
41
    (move) =>
4✔
42
    (context, ...args) => {
7,572✔
43
      const result = move(context, ...args);
15,720✔
44
      // Check state in non-production environments.
15,720✔
45
      if (process.env.NODE_ENV !== 'production' && !isSerializable(result)) {
15,720!
UNCOV
46
        throw new Error(
×
UNCOV
47
          'Move state is not JSON-serialiazable.\n' +
×
UNCOV
48
            'See https://boardgame.io/documentation/#/?id=state for more information.'
×
UNCOV
49
        );
×
UNCOV
50
      }
×
51
      return result;
15,720✔
52
    },
4✔
53
};
4✔
54

4✔
55
export default SerializablePlugin;
4✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc