Skip to content

Commit 3e3b2fb

Browse files
committed
Create mocks for jest
1 parent a66c8bd commit 3e3b2fb

File tree

7 files changed

+23
-37
lines changed

7 files changed

+23
-37
lines changed

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ images
44
CONTRIBUTING.md
55
coverage
66
__tests__
7+
__mocks__
78
.eslintrc.cjs
89
CONTRIBUTING.md

__mocks__/fs.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const fsMock = {
2+
readFileSync: jest.fn(),
3+
createReadStream: jest.fn(),
4+
};
5+
6+
export = fsMock;

__mocks__/http.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const httpMock = {
2+
IncomingMessage: jest.fn(),
3+
ServerResponse: jest.fn(() => ({
4+
writeHead: jest.fn(),
5+
write: jest.fn(),
6+
end: jest.fn(),
7+
})),
8+
};
9+
10+
export = httpMock;

__tests__/context.test.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,8 @@ import fs from "fs";
33
import Context from "../src/context";
44
import { Socket } from "net";
55

6-
jest.mock('http', () => {
7-
return {
8-
IncomingMessage: jest.fn(),
9-
ServerResponse: jest.fn(() => ({
10-
writeHead: jest.fn(),
11-
write: jest.fn(),
12-
end: jest.fn()
13-
})),
14-
};
15-
});
16-
17-
jest.mock('fs', () => ({
18-
readFileSync: jest.fn(),
19-
createReadStream: jest.fn()
20-
}));
6+
jest.mock("http");
7+
jest.mock("fs");
218

229
const { IncomingMessage, ServerResponse } = http;
2310
const mockIncomingMessage = new IncomingMessage(new Socket());

__tests__/middleware-manager.test.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,7 @@ import http from "http";
33
import { Socket } from "net";
44
import { MiddlewareManager } from "../src/middleware";
55

6-
jest.mock('http', () => {
7-
return {
8-
IncomingMessage: jest.fn(),
9-
ServerResponse: jest.fn(() => ({
10-
writeHead: jest.fn(),
11-
write: jest.fn(),
12-
end: jest.fn()
13-
})),
14-
};
15-
});
6+
jest.mock('http');
167

178
const { IncomingMessage, ServerResponse } = http;
189
const mockIncomingMessage = new IncomingMessage(new Socket());
@@ -36,7 +27,7 @@ describe("Middleware Manager", () => {
3627
context.send("1");
3728
next();
3829
})
39-
30+
4031
const context = new Context(mockIncomingMessage, mockServerResponse, '/');
4132
manager.run(context);
4233

__tests__/middleware-utils.test.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,7 @@ import { runMiddleware } from "../src/middleware/utils";
33
import http from "http";
44
import { Socket } from "net";
55

6-
jest.mock('http', () => {
7-
return {
8-
IncomingMessage: jest.fn(),
9-
ServerResponse: jest.fn(() => ({
10-
writeHead: jest.fn(),
11-
write: jest.fn(),
12-
end: jest.fn()
13-
})),
14-
};
15-
});
6+
jest.mock("http");
167

178
const { IncomingMessage, ServerResponse } = http;
189
const mockIncomingMessage = new IncomingMessage(new Socket());

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,6 @@
106106
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
107107
"skipLibCheck": true /* Skip type checking all .d.ts files. */
108108
},
109-
"include": ["src", "__tests__"],
109+
"include": ["src", "__tests__", "__mocks__"],
110110
"exclude": ["node_modules", "dist", "examples"],
111111
}

0 commit comments

Comments
 (0)