diff --git a/CHANGELOG.md b/CHANGELOG.md index 4fb53e9eb963..5133b092ef0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ - `[jest-changed-files]`: Migrate to TypeScript ([#7827](https://github.com/facebook/jest/pull/7827)) - `[jest-matcher-utils]`: Migrate to TypeScript ([#7835](https://github.com/facebook/jest/pull/7835)) - `[jest-docblock]`: Migrate to TypeScript ([#7836](https://github.com/facebook/jest/pull/7836)) +- `[jest-serializer]`: Migrate to TypeScript ([#7841](https://github.com/facebook/jest/pull/7841)) ### Performance diff --git a/packages/jest-serializer/package.json b/packages/jest-serializer/package.json index 0355c4e2c235..73386346ee8d 100644 --- a/packages/jest-serializer/package.json +++ b/packages/jest-serializer/package.json @@ -11,5 +11,6 @@ }, "license": "MIT", "main": "build/index.js", + "types": "build/index.d.ts", "gitHead": "634e5a54f46b2a62d1dc81a170562e6f4e55ad60" } diff --git a/packages/jest-serializer/src/__tests__/index.test.js b/packages/jest-serializer/src/__tests__/index.test.ts similarity index 98% rename from packages/jest-serializer/src/__tests__/index.test.js rename to packages/jest-serializer/src/__tests__/index.test.ts index 5546ca0addc7..f23cf3651525 100644 --- a/packages/jest-serializer/src/__tests__/index.test.js +++ b/packages/jest-serializer/src/__tests__/index.test.ts @@ -34,6 +34,7 @@ const objs = [ {key1: 'foo', key2: 'bar', key3: {array: [null, {}]}}, {minusInf: -Infinity, nan: NaN, plusInf: +Infinity}, {date: new Date(1234567890), re: /foo/gi}, + // @ts-ignore - testing NaN {map: new Map([[NaN, 4], [undefined, 'm']]), set: new Set([undefined, NaN])}, {buf: Buffer.from([0, 255, 127])}, ]; diff --git a/packages/jest-serializer/src/global.d.ts b/packages/jest-serializer/src/global.d.ts new file mode 100644 index 000000000000..809c49911f31 --- /dev/null +++ b/packages/jest-serializer/src/global.d.ts @@ -0,0 +1,11 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +declare module 'v8' { + function serialize(value: unknown): Buffer; + function deserialize(value: Buffer): unknown; +} diff --git a/packages/jest-serializer/src/index.js b/packages/jest-serializer/src/index.ts similarity index 83% rename from packages/jest-serializer/src/index.js rename to packages/jest-serializer/src/index.ts index 357e8c2f50be..c9b3525ea613 100644 --- a/packages/jest-serializer/src/index.js +++ b/packages/jest-serializer/src/index.ts @@ -3,16 +3,12 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. - * - * @flow */ -'use strict'; - import fs from 'fs'; import v8 from 'v8'; -import type {Path} from 'types/Config'; +type Path = string; // JSON and V8 serializers are both stable when it comes to compatibility. The // current JSON specification is well defined in RFC 8259, and V8 ensures that @@ -23,7 +19,7 @@ const JS_TYPE = '__$t__'; const JS_VALUE = '__$v__'; const JS_VF = '__$f__'; -function replacer(key: string, value: any): any { +function replacer(_key: string, value: any): any { // NaN cannot be in a switch statement, because NaN !== NaN. if (Number.isNaN(value)) { return {[JS_TYPE]: 'n'}; @@ -32,10 +28,8 @@ function replacer(key: string, value: any): any { switch (value) { case undefined: return {[JS_TYPE]: 'u'}; - case +Infinity: return {[JS_TYPE]: '+'}; - case -Infinity: return {[JS_TYPE]: '-'}; } @@ -43,16 +37,12 @@ function replacer(key: string, value: any): any { switch (value && value.constructor) { case Date: return {[JS_TYPE]: 'd', [JS_VALUE]: value.getTime()}; - case RegExp: return {[JS_TYPE]: 'r', [JS_VALUE]: value.source, [JS_VF]: value.flags}; - case Set: return {[JS_TYPE]: 's', [JS_VALUE]: Array.from(value)}; - case Map: return {[JS_TYPE]: 'm', [JS_VALUE]: Array.from(value)}; - case Buffer: return {[JS_TYPE]: 'b', [JS_VALUE]: value.toString('latin1')}; } @@ -60,7 +50,7 @@ function replacer(key: string, value: any): any { return value; } -function reviver(key: string, value: any): any { +function reviver(_key: string, value: any): any { if (!value || (typeof value !== 'object' && !value.hasOwnProperty(JS_TYPE))) { return value; } @@ -68,28 +58,20 @@ function reviver(key: string, value: any): any { switch (value[JS_TYPE]) { case 'u': return undefined; - case 'n': return NaN; - case '+': return +Infinity; - case '-': return -Infinity; - case 'd': return new Date(value[JS_VALUE]); - case 'r': return new RegExp(value[JS_VALUE], value[JS_VF]); - case 's': return new Set(value[JS_VALUE]); - case 'm': return new Map(value[JS_VALUE]); - case 'b': return Buffer.from(value[JS_VALUE], 'latin1'); } @@ -97,7 +79,7 @@ function reviver(key: string, value: any): any { return value; } -function jsonStringify(content) { +function jsonStringify(content: unknown) { // Not pretty, but the ES JSON spec says that "toJSON" will be called before // getting into your replacer, so we have to remove them beforehand. See // https://www.ecma-international.org/ecma-262/#sec-serializejsonproperty @@ -109,37 +91,33 @@ function jsonStringify(content) { /* eslint-disable no-extend-native */ try { - // $FlowFixMe: intentional removal of "toJSON" property. + // @ts-ignore intentional removal of "toJSON" property. Date.prototype.toJSON = undefined; - // $FlowFixMe: intentional removal of "toJSON" property. + // @ts-ignore intentional removal of "toJSON" property. Buffer.prototype.toJSON = undefined; return JSON.stringify(content, replacer); } finally { - // $FlowFixMe: intentional assignment of "toJSON" property. Date.prototype.toJSON = dateToJSON; - // $FlowFixMe: intentional assignment of "toJSON" property. Buffer.prototype.toJSON = bufferToJSON; } /* eslint-enable no-extend-native */ } -function jsonParse(content) { +function jsonParse(content: string) { return JSON.parse(content, reviver); } // In memory functions. export function deserialize(buffer: Buffer): any { - // $FlowFixMe - Node 8+ only return v8.deserialize ? v8.deserialize(buffer) : jsonParse(buffer.toString('utf8')); } -export function serialize(content: any): Buffer { - // $FlowFixMe - Node 8+ only +export function serialize(content: unknown): Buffer { return v8.serialize ? v8.serialize(content) : Buffer.from(jsonStringify(content)); @@ -148,14 +126,12 @@ export function serialize(content: any): Buffer { // Synchronous filesystem functions. export function readFileSync(filePath: Path): any { - // $FlowFixMe - Node 8+ only return v8.deserialize ? v8.deserialize(fs.readFileSync(filePath)) : jsonParse(fs.readFileSync(filePath, 'utf8')); } export function writeFileSync(filePath: Path, content: any) { - // $FlowFixMe - Node 8+ only return v8.serialize ? fs.writeFileSync(filePath, v8.serialize(content)) : fs.writeFileSync(filePath, jsonStringify(content), 'utf8'); diff --git a/packages/jest-serializer/tsconfig.json b/packages/jest-serializer/tsconfig.json new file mode 100644 index 000000000000..7bb06bce6d20 --- /dev/null +++ b/packages/jest-serializer/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "rootDir": "src", + "outDir": "build" + } +}