Skip to content

Commit 8d4d332

Browse files
tomer-amir-vonageRomakita
authored andcommitted
feat(test): Added TS_TEST flag to indicate not to switch file extensions
1 parent 2eb27bd commit 8d4d332

File tree

4 files changed

+91
-3
lines changed

4 files changed

+91
-3
lines changed

docs/.vuepress/config.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ module.exports = {
8787
{link: "/tutorials/templating.html", text: "Templating"},
8888
{link: "/tutorials/throw-http-exceptions.html", text: "Throw HTTP exceptions"},
8989
{link: "/tutorials/not-found-page.html", text: "Customize 404"},
90-
{link: "/tutorials/aws.html", text: "AWS project"}
90+
{link: "/tutorials/aws.html", text: "AWS project"},
91+
{link: "/tutorials/jest.html", text: "Jest"}
9192
]
9293
},
9394
{text: "4.x", link: "http://v4.tsed.io"}
@@ -156,7 +157,8 @@ module.exports = {
156157
"templating",
157158
"throw-http-exceptions",
158159
"not-found-page",
159-
"aws"
160+
"aws",
161+
"jest"
160162
]
161163
}]
162164
},
@@ -177,6 +179,7 @@ module.exports = {
177179
"/tutorials/throw-http-exceptions",
178180
"/tutorials/not-found-page",
179181
"/tutorials/aws",
182+
"/tutorials/jest",
180183
"/tutorials/seq",
181184
"/docs/controllers",
182185
"/docs/providers",

docs/tutorials/jest.md

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
meta:
3+
- name: description
4+
content: Use Jest with Ts.ED framework. Jest is a testing framework.
5+
- name: keywords
6+
content: ts.ed express typescript mongoose node.js javascript decorators
7+
---
8+
# Jest <Badge text="beta" type="warn"/> <Badge text="Contributors are welcome" />
9+
10+
<Banner src="https://camo.githubusercontent.com/b5639de5cfa97c51598b60b13a1061498afe2acb/68747470733a2f2f64337676366c703535716a6171632e636c6f756466726f6e742e6e65742f6974656d732f3244324b343533313278304d31713243306133502f6a6573742d6c6f676f2e737667" href="https://jestjs.io/" height="128" />
11+
12+
## Installation
13+
14+
To begin, install Ts.ED testing module:
15+
16+
```bash
17+
npm i -D @tsed/testing
18+
```
19+
20+
Then install `jest`:
21+
22+
```bash
23+
npm i -D jest ts-jest @types/jest
24+
```
25+
26+
## Configuration
27+
28+
Now you need to configure `jest` to work with Typescript.
29+
To do that crate a new file in your project root called `jest.config.js` and put the following configuration in it:
30+
31+
```js
32+
module.exports = {
33+
preset: "ts-jest",
34+
globals: {
35+
"ts-jest": {
36+
tsConfig: "tsconfig.json"
37+
}
38+
},
39+
verbose: true,
40+
collectCoverage: true,
41+
moduleFileExtensions: ["ts", "js", "json"],
42+
testMatch: ["**/tests/**/*.test.ts"],
43+
testEnvironment: "node",
44+
testPathIgnorePatterns: [
45+
"<rootDir>/node_modules/",
46+
"<rootDir>/out/"
47+
],
48+
modulePathIgnorePatterns: [
49+
"<rootDir>/node_modules/",
50+
"<rootDir>/out/"
51+
],
52+
coverageDirectory: "tests/result",
53+
collectCoverageFrom: [
54+
"src/controllers/**/*.ts",
55+
"src/middlewares/**/*.ts",
56+
"src/services/**/*.ts",
57+
"!**/*.d.ts"
58+
]
59+
};
60+
```
61+
62+
### **Important!!!**
63+
64+
The DI will not work unless you set the `TS_TEST` environment variable to `true` like this:
65+
66+
```json
67+
{
68+
"scripts": {
69+
"test": "TS_TEST=true jest",
70+
"test:w": "TS_TEST=true jest --watch"
71+
}
72+
}
73+
```

packages/common/src/server/utils/cleanGlobPatterns.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export function cleanGlobPatterns(files: string | string[], excludes: string[]):
66
return []
77
.concat(files as any)
88
.map((file: string) => {
9-
if (!require.extensions[".ts"]) {
9+
if (!require.extensions[".ts"] && !process.env["TS_TEST"]) {
1010
file = file.replace(/\.ts$/i, ".js");
1111
}
1212

packages/common/test/server/utils/cleanGlobPatterns.spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ describe("cleanGlobPatterns()", () => {
2525
it("should return file.ts.js and manipulate only the file extension", () => {
2626
expect(cleanGlobPatterns("file.ts.ts", ["!**.spec.ts"])[0]).to.contains("file.ts.js");
2727
});
28+
29+
describe("when using ts-jest", () => {
30+
it("should return file.ts", () => {
31+
process.env["TS_TEST"] = "true";
32+
expect(cleanGlobPatterns("file.ts", ["!**.spec.ts"])[0]).to.contains("file.ts");
33+
delete process.env["TS_TEST"];
34+
});
35+
36+
it("should return file.js", () => {
37+
expect(cleanGlobPatterns("file.ts", ["!**.spec.ts"])[0]).to.contains("file.js");
38+
});
39+
});
2840
});
2941
describe("when have typescript compiler", () => {
3042
let compiler: any;

0 commit comments

Comments
 (0)