Skip to content
This repository was archived by the owner on Apr 25, 2022. It is now read-only.

Commit 0e42e8f

Browse files
committed
hook & unittest & module config
1 parent f53b2ae commit 0e42e8f

File tree

9 files changed

+2267
-35
lines changed

9 files changed

+2267
-35
lines changed

jest.config.js

+188
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
// For a detailed explanation regarding each configuration property, visit:
2+
// https://jestjs.io/docs/en/configuration.html
3+
4+
module.exports = {
5+
// All imported modules in your tests should be mocked automatically
6+
// automock: false,
7+
8+
// Stop running tests after `n` failures
9+
// bail: 0,
10+
11+
// Respect "browser" field in package.json when resolving modules
12+
// browser: false,
13+
14+
// The directory where Jest should store its cached dependency information
15+
// cacheDirectory: "C:\\Users\\codef\\AppData\\Local\\Temp\\jest",
16+
17+
// Automatically clear mock calls and instances between every test
18+
clearMocks: true,
19+
20+
// Indicates whether the coverage information should be collected while executing the test
21+
// collectCoverage: false,
22+
23+
// An array of glob patterns indicating a set of files for which coverage information should be collected
24+
// collectCoverageFrom: undefined,
25+
26+
// The directory where Jest should output its coverage files
27+
coverageDirectory: "coverage",
28+
29+
// An array of regexp pattern strings used to skip coverage collection
30+
// coveragePathIgnorePatterns: [
31+
// "\\\\node_modules\\\\"
32+
// ],
33+
34+
// A list of reporter names that Jest uses when writing coverage reports
35+
// coverageReporters: [
36+
// "json",
37+
// "text",
38+
// "lcov",
39+
// "clover"
40+
// ],
41+
42+
// An object that configures minimum threshold enforcement for coverage results
43+
// coverageThreshold: undefined,
44+
45+
// A path to a custom dependency extractor
46+
// dependencyExtractor: undefined,
47+
48+
// Make calling deprecated APIs throw helpful error messages
49+
// errorOnDeprecated: false,
50+
51+
// Force coverage collection from ignored files using an array of glob patterns
52+
// forceCoverageMatch: [],
53+
54+
// A path to a module which exports an async function that is triggered once before all test suites
55+
// globalSetup: undefined,
56+
57+
// A path to a module which exports an async function that is triggered once after all test suites
58+
// globalTeardown: undefined,
59+
60+
// A set of global variables that need to be available in all test environments
61+
// globals: {},
62+
63+
// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
64+
// maxWorkers: "50%",
65+
66+
// An array of directory names to be searched recursively up from the requiring module's location
67+
// moduleDirectories: [
68+
// "node_modules"
69+
// ],
70+
71+
// An array of file extensions your modules use
72+
moduleFileExtensions: [
73+
"js",
74+
"ts",
75+
],
76+
77+
// A map from regular expressions to module names that allow to stub out resources with a single module
78+
moduleNameMapper: {
79+
"^@App/(.*)$": "<rootDir>/src/$1",
80+
},
81+
82+
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
83+
// modulePathIgnorePatterns: [],
84+
85+
// Activates notifications for test results
86+
// notify: false,
87+
88+
// An enum that specifies notification mode. Requires { notify: true }
89+
// notifyMode: "failure-change",
90+
91+
// A preset that is used as a base for Jest's configuration
92+
// preset: undefined,
93+
94+
// Run tests from one or more projects
95+
// projects: undefined,
96+
97+
// Use this configuration option to add custom reporters to Jest
98+
// reporters: undefined,
99+
100+
// Automatically reset mock state between every test
101+
// resetMocks: false,
102+
103+
// Reset the module registry before running each individual test
104+
// resetModules: false,
105+
106+
// A path to a custom resolver
107+
// resolver: undefined,
108+
109+
// Automatically restore mock state between every test
110+
// restoreMocks: false,
111+
112+
// The root directory that Jest should scan for tests and modules within
113+
// rootDir: undefined,
114+
115+
// A list of paths to directories that Jest should use to search for files in
116+
// roots: [
117+
// "<rootDir>"
118+
// ],
119+
120+
// Allows you to use a custom runner instead of Jest's default test runner
121+
// runner: "jest-runner",
122+
123+
// The paths to modules that run some code to configure or set up the testing environment before each test
124+
// setupFiles: [],
125+
126+
// A list of paths to modules that run some code to configure or set up the testing framework before each test
127+
// setupFilesAfterEnv: [],
128+
129+
// A list of paths to snapshot serializer modules Jest should use for snapshot testing
130+
// snapshotSerializers: [],
131+
132+
// The test environment that will be used for testing
133+
testEnvironment: "node",
134+
135+
// Options that will be passed to the testEnvironment
136+
// testEnvironmentOptions: {},
137+
138+
// Adds a location field to test results
139+
// testLocationInResults: false,
140+
141+
// The glob patterns Jest uses to detect test files
142+
// testMatch: [
143+
// "**/__tests__/**/*.[jt]s?(x)",
144+
// "**/?(*.)+(spec|test).[tj]s?(x)"
145+
// ],
146+
147+
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
148+
// testPathIgnorePatterns: [
149+
// "\\\\node_modules\\\\"
150+
// ],
151+
152+
// The regexp pattern or array of patterns that Jest uses to detect test files
153+
// testRegex: [],
154+
155+
// This option allows the use of a custom results processor
156+
// testResultsProcessor: undefined,
157+
158+
// This option allows use of a custom test runner
159+
// testRunner: "jasmine2",
160+
161+
// This option sets the URL for the jsdom environment. It is reflected in properties such as location.href
162+
// testURL: "http://localhost",
163+
164+
// Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout"
165+
// timers: "real",
166+
167+
// A map from regular expressions to paths to transformers
168+
transform: {
169+
'^.+\\.ts?$': 'ts-jest'
170+
},
171+
172+
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
173+
// transformIgnorePatterns: [
174+
// "\\\\node_modules\\\\"
175+
// ],
176+
177+
// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
178+
// unmockedModulePathPatterns: undefined,
179+
180+
// Indicates whether each individual test should be reported during the run
181+
// verbose: undefined,
182+
183+
// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
184+
// watchPathIgnorePatterns: [],
185+
186+
// Whether to use watchman for file crawling
187+
// watchman: true,
188+
};

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "> 一个快速学习超星慕课的chrome扩展工具(੧ᐛ੭挂科模式,启动)\r > 初次开发chrome扩展,有兴趣的朋友可以一起来哦",
55
"main": "mooc.js",
66
"scripts": {
7-
"test": "echo \"Hello,World\nDeveloper Blog:blog.icodef.com\" && exit 1",
7+
"test": "jest",
88
"build": "node_modules/.bin/webpack --mode production --config webpack.config.js",
99
"tampermonkey": "node_modules/.bin/webpack --config webpack.tampermonkey.js",
1010
"dev": "node_modules/.bin/webpack --mode development --config webpack.dev.js",
@@ -25,12 +25,15 @@
2525
"homepage": "https://github.com/CodFrm/cxmooc-tools#readme",
2626
"devDependencies": {
2727
"@types/chrome": "^0.0.91",
28+
"@types/jest": "^25.1.2",
2829
"@vuepress/plugin-back-to-top": "^1.0.0-alpha.47",
2930
"@vuepress/plugin-google-analytics": "^1.0.0-alpha.0",
3031
"crx": "^4.0.1",
3132
"css-loader": "^2.1.1",
3233
"html-webpack-plugin": "^3.2.0",
34+
"jest": "^25.1.0",
3335
"style-loader": "^0.23.1",
36+
"ts-jest": "^25.2.0",
3437
"ts-loader": "^6.0.4",
3538
"typescript": "^3.6.2",
3639
"vuepress": "^1.0.0-alpha.47",

src/internal/utils/hook.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
export interface Context {
3+
(next: Function): void
4+
}
5+
6+
export class Hook {
7+
public func: Function;
8+
public stack: Array<Context>;
9+
public context: any;
10+
11+
public constructor(func: Function, context?: any) {
12+
this.context = context || window;
13+
this.func = func;
14+
}
15+
16+
public Middleware(call: Context) {
17+
let old = this.context[this.func.name];
18+
this.context[this.func.name] = function () {
19+
var args = [old];
20+
for (var _i = 0; _i < arguments.length; _i++) {
21+
args[_i + 1] = arguments[_i];
22+
}
23+
return call.apply(this, args);
24+
}
25+
}
26+
27+
}

src/mooc/chaoxing/course.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { Mooc } from "../factory";
2+
import "../../internal/utils/hook"
23

3-
export class Course implements Mooc{
4+
export class Course implements Mooc {
45
Start(): void {
6+
57
}
6-
8+
79
}

src/start.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { NewChromeServerMessage } from "./internal/utils/message";
2-
import { HttpUtils, Injected } from "./internal/utils/utils";
3-
import { Application, Content } from "./internal/application";
4-
import { SystemConfig } from "./internal/utils/config";
1+
import { NewChromeServerMessage } from "@App/internal/utils/message";
2+
import { HttpUtils, Injected } from "@App/internal/utils/utils";
3+
import { Application, Content } from "@App/internal/application";
4+
import { SystemConfig } from "@App/internal/utils/config";
55

66
new Application(Content);
77
class start {

tests/hook.test.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { Hook } from "@App/internal/utils/hook";
2+
3+
function hookContext() {
4+
return {
5+
add: function (a: number, b: number) {
6+
return a + b;
7+
}
8+
};
9+
}
10+
11+
describe("hook", () => {
12+
it("测试hook一次", () => {
13+
let context = hookContext();
14+
15+
let hook = new Hook(context.add, context);
16+
17+
hook.Middleware(function (next: Function, ...args: any) {
18+
return next.apply(this, args) + 1;
19+
});
20+
21+
expect(context.add(1, 1)).toBe(3);
22+
});
23+
it("测试hook两次", () => {
24+
let context = hookContext();
25+
let hook = new Hook(context.add, context);
26+
hook.Middleware(function (next: Function, ...args: any) {
27+
return next.apply(this, args) + 1;
28+
});
29+
hook.Middleware(function (next: Function, ...args: any) {
30+
return next.apply(this, args) + 1;
31+
});
32+
expect(context.add(1, 1)).toBe(4);
33+
});
34+
it("拦截测试", () => {
35+
let context = hookContext();
36+
let hook = new Hook(context.add, context);
37+
hook.Middleware(function (next: Function, ...args: any) {
38+
return 0;
39+
});
40+
expect(context.add(1, 1)).toBe(0);
41+
});
42+
});

tsconfig.json

+13-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,20 @@
44
"noImplicitAny": true,
55
"module": "es6",
66
"target": "es5",
7-
"allowJs": true
7+
"allowJs": true,
8+
"baseUrl": ".",
9+
"paths": {
10+
"@App/*": [
11+
"src/*"
12+
],
13+
"@Test/*": [
14+
"tests/*"
15+
]
16+
}
817
},
918
"include": [
1019
"./src/*.ts",
11-
"./src/**/*.ts"
12-
]
20+
"./src/**/*.ts",
21+
"./tests/*.test.ts",
22+
],
1323
}

webpack.config.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const path = require('path');
12
const htmlWebpackPlugin = require('html-webpack-plugin');
23
const home = __dirname + '/src';
34
module.exports = {
@@ -36,6 +37,9 @@ module.exports = {
3637
]
3738
},
3839
resolve: {
39-
extensions: ['.ts', '.js']
40+
extensions: ['.ts', '.js'],
41+
alias: {
42+
"@App": path.resolve(__dirname, 'src/'),
43+
}
4044
}
4145
};

0 commit comments

Comments
 (0)