Skip to content

Commit

Permalink
use global.d.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
thymikee committed Feb 8, 2019
1 parent 5dccf17 commit 6a2b5ca
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 20 deletions.
4 changes: 0 additions & 4 deletions packages/jest-serializer/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ import serializer from '..';

const v8s = [
{
// @ts-ignore - Node 8+ only
deserialize: v8.deserialize,
// @ts-ignore - Node 8+ only
serialize: v8.serialize,
},
{
Expand Down Expand Up @@ -57,9 +55,7 @@ afterEach(() => {
v8s.forEach((mockV8, i) => {
describe('Using V8 implementation ' + i, () => {
beforeEach(() => {
// @ts-ignore - Node 8+ only
v8.serialize = mockV8.serialize;
// @ts-ignore - Node 8+ only
v8.deserialize = mockV8.deserialize;
});

Expand Down
11 changes: 11 additions & 0 deletions packages/jest-serializer/src/global.d.ts
Original file line number Diff line number Diff line change
@@ -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;
}
20 changes: 4 additions & 16 deletions packages/jest-serializer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,40 +114,28 @@ function jsonParse(content: string) {
// In memory functions.

export function deserialize(buffer: Buffer): any {
// @ts-ignore - Node 8+ only
return v8.deserialize
? //
// @ts-ignore - Node 8+ only
v8.deserialize(buffer)
? v8.deserialize(buffer)
: jsonParse(buffer.toString('utf8'));
}

export function serialize(content: unknown): Buffer {
// @ts-ignore - Node 8+ only
return v8.serialize
? //
// @ts-ignore - Node 8+ only
v8.serialize(content)
? v8.serialize(content)
: Buffer.from(jsonStringify(content));
}

// Synchronous filesystem functions.

export function readFileSync(filePath: Path): any {
// @ts-ignore - Node 8+ only
return v8.deserialize
? //
// @ts-ignore - Node 8+ only
v8.deserialize(fs.readFileSync(filePath))
? v8.deserialize(fs.readFileSync(filePath))
: jsonParse(fs.readFileSync(filePath, 'utf8'));
}

export function writeFileSync(filePath: Path, content: any) {
// @ts-ignore - Node 8+ only
return v8.serialize
? //
// @ts-ignore - Node 8+ only
fs.writeFileSync(filePath, v8.serialize(content))
? fs.writeFileSync(filePath, v8.serialize(content))
: fs.writeFileSync(filePath, jsonStringify(content), 'utf8');
}

Expand Down
1 change: 1 addition & 0 deletions packages/jest-serializer/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"extends": "../../tsconfig.json",
"include": ["./src/global.d.ts"],
"compilerOptions": {
"rootDir": "src",
"outDir": "build"
Expand Down

0 comments on commit 6a2b5ca

Please sign in to comment.