|
| 1 | +/** |
| 2 | + * Copyright (c) 2018-present, Facebook, Inc. All rights reserved. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +'use strict'; |
| 9 | + |
| 10 | +import prettyFormat from 'pretty-format'; |
| 11 | + |
| 12 | +import fs from 'fs'; |
| 13 | +import os from 'os'; |
| 14 | +import path from 'path'; |
| 15 | +import v8 from 'v8'; |
| 16 | + |
| 17 | +import serializer from '..'; |
| 18 | + |
| 19 | +const v8s = [ |
| 20 | + { |
| 21 | + deserialize: v8.deserialize, |
| 22 | + serialize: v8.serialize, |
| 23 | + }, |
| 24 | + { |
| 25 | + deserialize: undefined, |
| 26 | + serialize: undefined, |
| 27 | + }, |
| 28 | +]; |
| 29 | + |
| 30 | +const objs = [ |
| 31 | + 3, |
| 32 | + null, |
| 33 | + [0, true, '2', [3.14, {}, null]], |
| 34 | + {key1: 'foo', key2: 'bar', key3: {array: [null, {}]}}, |
| 35 | + {minusInf: -Infinity, nan: NaN, plusInf: +Infinity}, |
| 36 | + {date: new Date(1234567890), re: /foo/gi}, |
| 37 | + {map: new Map([[NaN, 4], [undefined, 'm']]), set: new Set([undefined, NaN])}, |
| 38 | + {buf: Buffer.from([0, 255, 127])}, |
| 39 | +]; |
| 40 | + |
| 41 | +const file = path.join(os.tmpdir(), '__jest-serialize-test__'); |
| 42 | + |
| 43 | +afterEach(() => { |
| 44 | + try { |
| 45 | + fs.unlinkSync(file); |
| 46 | + } catch (err) { |
| 47 | + // Do nothing if file does not exist. |
| 48 | + } |
| 49 | +}); |
| 50 | + |
| 51 | +// We execute the same suite of tests over multiple objects ("objs") and over |
| 52 | +// multiple mocks of the V8 object ("v8s") so that we verify that all possible |
| 53 | +// encodings and cases work. |
| 54 | +v8s.forEach((mockV8, i) => { |
| 55 | + describe('Using V8 implementation ' + i, () => { |
| 56 | + beforeEach(() => { |
| 57 | + v8.serialize = mockV8.serialize; |
| 58 | + v8.deserialize = mockV8.deserialize; |
| 59 | + }); |
| 60 | + |
| 61 | + it('throws the error with an invalid serialization', () => { |
| 62 | + // No chance this is a valid serialization, neither in JSON nor V8. |
| 63 | + const invalidBuffer = Buffer.from([0, 85, 170, 255]); |
| 64 | + |
| 65 | + fs.writeFileSync(file, invalidBuffer); |
| 66 | + |
| 67 | + expect(() => serializer.deserialize(invalidBuffer)).toThrow(); |
| 68 | + expect(() => serializer.readFileSync(file)).toThrow(); |
| 69 | + }); |
| 70 | + |
| 71 | + objs.forEach((obj, i) => { |
| 72 | + describe('Object ' + i, () => { |
| 73 | + it('serializes/deserializes in memory', () => { |
| 74 | + const buf = serializer.serialize(obj); |
| 75 | + |
| 76 | + expect(buf).toBeInstanceOf(Buffer); |
| 77 | + |
| 78 | + expect(prettyFormat(serializer.deserialize(buf))).toEqual( |
| 79 | + prettyFormat(obj), |
| 80 | + ); |
| 81 | + }); |
| 82 | + |
| 83 | + it('serializes/deserializes in disk', () => { |
| 84 | + serializer.writeFileSync(file, obj); |
| 85 | + |
| 86 | + expect(prettyFormat(serializer.readFileSync(file))).toEqual( |
| 87 | + prettyFormat(obj), |
| 88 | + ); |
| 89 | + }); |
| 90 | + }); |
| 91 | + }); |
| 92 | + }); |
| 93 | +}); |
0 commit comments