diff --git a/docs/.vuepress/config.js b/docs/.vuepress/config.js index 89a449428ce..4dcd2b0dc6e 100644 --- a/docs/.vuepress/config.js +++ b/docs/.vuepress/config.js @@ -87,7 +87,8 @@ module.exports = { {link: "/tutorials/templating.html", text: "Templating"}, {link: "/tutorials/throw-http-exceptions.html", text: "Throw HTTP exceptions"}, {link: "/tutorials/not-found-page.html", text: "Customize 404"}, - {link: "/tutorials/aws.html", text: "AWS project"} + {link: "/tutorials/aws.html", text: "AWS project"}, + {link: "/tutorials/jest.html", text: "Jest"} ] }, {text: "4.x", link: "http://v4.tsed.io"} @@ -156,7 +157,8 @@ module.exports = { "templating", "throw-http-exceptions", "not-found-page", - "aws" + "aws", + "jest" ] }] }, @@ -177,6 +179,7 @@ module.exports = { "/tutorials/throw-http-exceptions", "/tutorials/not-found-page", "/tutorials/aws", + "/tutorials/jest", "/tutorials/seq", "/docs/controllers", "/docs/providers", diff --git a/docs/tutorials/jest.md b/docs/tutorials/jest.md new file mode 100644 index 00000000000..d34a0788334 --- /dev/null +++ b/docs/tutorials/jest.md @@ -0,0 +1,73 @@ +--- +meta: + - name: description + content: Use Jest with Ts.ED framework. Jest is a testing framework. + - name: keywords + content: ts.ed express typescript mongoose node.js javascript decorators +--- +# Jest + + + +## Installation + +To begin, install Ts.ED testing module: + +```bash +npm i -D @tsed/testing +``` + +Then install `jest`: + +```bash +npm i -D jest ts-jest @types/jest +``` + +## Configuration + +Now you need to configure `jest` to work with Typescript. +To do that crate a new file in your project root called `jest.config.js` and put the following configuration in it: + +```js +module.exports = { + preset: "ts-jest", + globals: { + "ts-jest": { + tsConfig: "tsconfig.json" + } + }, + verbose: true, + collectCoverage: true, + moduleFileExtensions: ["ts", "js", "json"], + testMatch: ["**/tests/**/*.test.ts"], + testEnvironment: "node", + testPathIgnorePatterns: [ + "/node_modules/", + "/out/" + ], + modulePathIgnorePatterns: [ + "/node_modules/", + "/out/" + ], + coverageDirectory: "tests/result", + collectCoverageFrom: [ + "src/controllers/**/*.ts", + "src/middlewares/**/*.ts", + "src/services/**/*.ts", + "!**/*.d.ts" + ] +}; +``` + +### **Important!!!** + +The DI will not work unless you set the `TS_TEST` environment variable to `true` like this: + +```json +{ + "scripts": { + "test": "TS_TEST=true jest", + "test:w": "TS_TEST=true jest --watch" + } +} +``` diff --git a/packages/common/src/server/utils/cleanGlobPatterns.ts b/packages/common/src/server/utils/cleanGlobPatterns.ts index 800678bf00f..bc573de0f67 100644 --- a/packages/common/src/server/utils/cleanGlobPatterns.ts +++ b/packages/common/src/server/utils/cleanGlobPatterns.ts @@ -6,7 +6,7 @@ export function cleanGlobPatterns(files: string | string[], excludes: string[]): return [] .concat(files as any) .map((file: string) => { - if (!require.extensions[".ts"]) { + if (!require.extensions[".ts"] && !process.env["TS_TEST"]) { file = file.replace(/\.ts$/i, ".js"); } diff --git a/packages/common/test/server/utils/cleanGlobPatterns.spec.ts b/packages/common/test/server/utils/cleanGlobPatterns.spec.ts index 91bb8f3c830..b9d029bac34 100644 --- a/packages/common/test/server/utils/cleanGlobPatterns.spec.ts +++ b/packages/common/test/server/utils/cleanGlobPatterns.spec.ts @@ -25,6 +25,18 @@ describe("cleanGlobPatterns()", () => { it("should return file.ts.js and manipulate only the file extension", () => { expect(cleanGlobPatterns("file.ts.ts", ["!**.spec.ts"])[0]).to.contains("file.ts.js"); }); + + describe("when using ts-jest", () => { + it("should return file.ts", () => { + process.env["TS_TEST"] = "true"; + expect(cleanGlobPatterns("file.ts", ["!**.spec.ts"])[0]).to.contains("file.ts"); + delete process.env["TS_TEST"]; + }); + + it("should return file.js", () => { + expect(cleanGlobPatterns("file.ts", ["!**.spec.ts"])[0]).to.contains("file.js"); + }); + }); }); describe("when have typescript compiler", () => { let compiler: any;