From 9b4b9050a920822d118398aa3aae1b7a27783496 Mon Sep 17 00:00:00 2001 From: mts88 Date: Thu, 10 Oct 2024 22:35:51 +0200 Subject: [PATCH 1/7] feat(spyconstructor): add spyConstructor function Add new spyConstructor function to extends jest --- .../spyConstructor/__mocks__/index.ts | 1 + .../__mocks__/stub-constructor.ts | 3 +++ lib/functions/spyConstructor/index.ts | 2 ++ .../spyConstructor/spy-constructor.spec.ts | 25 +++++++++++++++++++ .../spyConstructor/spy-constructor.ts | 11 ++++++++ lib/index.ts | 2 ++ 6 files changed, 44 insertions(+) create mode 100644 lib/functions/spyConstructor/__mocks__/index.ts create mode 100644 lib/functions/spyConstructor/__mocks__/stub-constructor.ts create mode 100644 lib/functions/spyConstructor/index.ts create mode 100644 lib/functions/spyConstructor/spy-constructor.spec.ts create mode 100644 lib/functions/spyConstructor/spy-constructor.ts diff --git a/lib/functions/spyConstructor/__mocks__/index.ts b/lib/functions/spyConstructor/__mocks__/index.ts new file mode 100644 index 0000000..9eaa891 --- /dev/null +++ b/lib/functions/spyConstructor/__mocks__/index.ts @@ -0,0 +1 @@ +export * from './stub-constructor'; diff --git a/lib/functions/spyConstructor/__mocks__/stub-constructor.ts b/lib/functions/spyConstructor/__mocks__/stub-constructor.ts new file mode 100644 index 0000000..cac1e67 --- /dev/null +++ b/lib/functions/spyConstructor/__mocks__/stub-constructor.ts @@ -0,0 +1,3 @@ +export class StubConstructorExample { + public foo = 'bar'; +} diff --git a/lib/functions/spyConstructor/index.ts b/lib/functions/spyConstructor/index.ts new file mode 100644 index 0000000..b6e836a --- /dev/null +++ b/lib/functions/spyConstructor/index.ts @@ -0,0 +1,2 @@ +export * from './spy-constructor'; +export * from './__mocks__'; diff --git a/lib/functions/spyConstructor/spy-constructor.spec.ts b/lib/functions/spyConstructor/spy-constructor.spec.ts new file mode 100644 index 0000000..b06dbe1 --- /dev/null +++ b/lib/functions/spyConstructor/spy-constructor.spec.ts @@ -0,0 +1,25 @@ +import { spyConstructor } from './spy-constructor'; + +describe('spyConstructor', () => { + it('should returns a mock of the constructor', () => { + const spiedObject = spyConstructor( + './__mocks__/stub-constructor', + 'StubConstructorExample', + ); + + expect(spiedObject._isMockFunction).toBeTrue(); + }); + + it('should mocks the constructor with the desired value', () => { + const mockedValue = jest.fn(); + + const spiedObject = spyConstructor( + './__mocks__/stub-constructor', + 'StubConstructorExample', + ).mockReturnValue(mockedValue); + + const resultFromMock = spiedObject(); + + expect(resultFromMock).toBe(mockedValue); + }); +}); diff --git a/lib/functions/spyConstructor/spy-constructor.ts b/lib/functions/spyConstructor/spy-constructor.ts new file mode 100644 index 0000000..6a25ec9 --- /dev/null +++ b/lib/functions/spyConstructor/spy-constructor.ts @@ -0,0 +1,11 @@ +export function spyConstructor( + module: string, + constructorName: Key, +): ReturnType { + jest.doMock(module); + + const moduleImport = jest.requireActual(module); + + const spiedConstructor = jest.spyOn(moduleImport, constructorName as any); + return spiedConstructor; +} diff --git a/lib/index.ts b/lib/index.ts index 5109393..0ac10d2 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -1,2 +1,4 @@ +export * from 'jest'; +export * from './functions/spyConstructor'; export * from './mocks/create-mock-with-uuid'; export * from './utils'; From 28470c740dab3f2d24730383c67c850e5cd6a5ce Mon Sep 17 00:00:00 2001 From: mts88 Date: Thu, 10 Oct 2024 22:48:29 +0200 Subject: [PATCH 2/7] chore: remove mocks from final build Exclude __mocks__ folder from the final build --- .ctirc | 4 +++- jest.config.js | 1 + lib/functions/spyConstructor/__mocks__/index.ts | 1 - lib/functions/spyConstructor/index.ts | 1 - lib/index.ts | 1 - sonar-project.properties | 4 ++-- tsconfig.build.json | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) delete mode 100644 lib/functions/spyConstructor/__mocks__/index.ts diff --git a/.ctirc b/.ctirc index 4f66353..1ac340a 100644 --- a/.ctirc +++ b/.ctirc @@ -19,7 +19,9 @@ "include": [ "**/*.ts" ], - "exclude": [], + "exclude": [ + "**/__mocks__/*" + ], "skipEmptyDir": true, "startFrom": "./lib", "output": "./dist", diff --git a/jest.config.js b/jest.config.js index 8d3b3c7..eac22e6 100644 --- a/jest.config.js +++ b/jest.config.js @@ -26,6 +26,7 @@ module.exports = { '.*.mock.ts$', '.*.dto.ts$', '.*\\.(spec|test)\\.(t|j)s$', + '__mocks__', ], moduleFileExtensions: ['js', 'json', 'ts'], clearMocks: true, diff --git a/lib/functions/spyConstructor/__mocks__/index.ts b/lib/functions/spyConstructor/__mocks__/index.ts deleted file mode 100644 index 9eaa891..0000000 --- a/lib/functions/spyConstructor/__mocks__/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './stub-constructor'; diff --git a/lib/functions/spyConstructor/index.ts b/lib/functions/spyConstructor/index.ts index b6e836a..5e794b2 100644 --- a/lib/functions/spyConstructor/index.ts +++ b/lib/functions/spyConstructor/index.ts @@ -1,2 +1 @@ export * from './spy-constructor'; -export * from './__mocks__'; diff --git a/lib/index.ts b/lib/index.ts index 0ac10d2..f3de061 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -1,4 +1,3 @@ -export * from 'jest'; export * from './functions/spyConstructor'; export * from './mocks/create-mock-with-uuid'; export * from './utils'; diff --git a/sonar-project.properties b/sonar-project.properties index 7049355..6b77cc8 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -1,10 +1,10 @@ sonar.projectKey=friendly-testers_ts-testing-buddy sonar.organization=friendly-testers sonar.testExecutionReportPaths=./test-report.xml -sonar.exclusions=**/node_modules/**,**/dist/**,**/coverage/**,**/lib/**/*.model.ts,,**/lib/**/*.dto.ts,**/lib/**/*.module.ts,**/lib/configs/**,**/lib/datasources/migrations/**,**/lib/main.ts,**/lib/server.ts,**/lib/app.ts,**/lib/index.ts,**/lib/bootstrap.ts +sonar.exclusions=**/node_modules/**,**/dist/**,**/coverage/**,**/lib/**/*.model.ts,**/lib/**/__mocks__/**,**/lib/**/*.dto.ts,**/lib/**/*.module.ts,**/lib/configs/**,**/lib/datasources/migrations/**,**/lib/main.ts,**/lib/server.ts,**/lib/app.ts,**/lib/index.ts,**/lib/bootstrap.ts sonar.sources=lib sonar.test.inclusions=**/*.spec.ts,**/*.test.ts,**/*.spec.tsx,**/*.test.tsx -sonar.test.exclusions=**/node_modules/**,**/dist/**,**/coverage/**,**/lib/**/*.model.ts,**/lib/**/*.dto.ts,**/lib/**/*.module.ts,**/lib/configs/**,**/lib/datasources/migrations/**,**/lib/main.ts,**/lib/server.ts,**/lib/app.ts,**/lib/index.ts,**/lib/bootstrap.ts +sonar.test.exclusions=**/node_modules/**,**/dist/**,**/coverage/**,**/lib/**/*.model.ts,**/lib/**/__mocks__/**,**/lib/**/*.dto.ts,**/lib/**/*.module.ts,**/lib/configs/**,**/lib/datasources/migrations/**,**/lib/main.ts,**/lib/server.ts,**/lib/app.ts,**/lib/index.ts,**/lib/bootstrap.ts sonar.typescript.lcov.reportPaths=coverage/lcov.info sonar.eslint.reportPaths=./eslint-report.json sonar.typescript.tsconfigPath=./tsconfig.json diff --git a/tsconfig.build.json b/tsconfig.build.json index aed3485..0dfdc8b 100644 --- a/tsconfig.build.json +++ b/tsconfig.build.json @@ -1,4 +1,4 @@ { "extends": "./tsconfig.json", - "exclude": ["node_modules", "test", "dist", "**/*spec.ts"] + "exclude": ["node_modules", "test", "dist", "**/*spec.ts", "**/__mocks__/*"] } From 666c52d848834c0a11eca29c8e8a5fa04403e4f1 Mon Sep 17 00:00:00 2001 From: mts88 Date: Thu, 10 Oct 2024 23:14:41 +0200 Subject: [PATCH 3/7] ci(pull_request.yml): add report from linting Generate the report from linting --- .github/actions/eslint-report/action.yml | 9 +++++++++ .github/workflows/pull_request.yml | 3 +++ 2 files changed, 12 insertions(+) create mode 100644 .github/actions/eslint-report/action.yml diff --git a/.github/actions/eslint-report/action.yml b/.github/actions/eslint-report/action.yml new file mode 100644 index 0000000..fa83296 --- /dev/null +++ b/.github/actions/eslint-report/action.yml @@ -0,0 +1,9 @@ +name: Eslint Report +description: Create the report from Eslint analysis + +runs: + using: 'composite' + steps: + - name: Generate eslint report + shell: bash + run: yarn lint:report || echo "::warning::yarn lint:report command is missing" diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index 6d28d06..ad87851 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -60,6 +60,9 @@ jobs: - name: Restore cached node_modules uses: ./.github/actions/cache-dependencies + - name: Generate lint report + uses: ./.github/actions/eslint-report + - name: Run unit tests run: yarn run test:coverage From e481de600bf2089802385bef5073cbb17d88841f Mon Sep 17 00:00:00 2001 From: mts88 Date: Thu, 10 Oct 2024 23:22:31 +0200 Subject: [PATCH 4/7] build(jest.config.js): remove index files from coverage Exclude index files from coverage --- jest.config.js | 1 + sonar-project.properties | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/jest.config.js b/jest.config.js index eac22e6..dbbcec2 100644 --- a/jest.config.js +++ b/jest.config.js @@ -19,6 +19,7 @@ module.exports = { '/coverage', '.eslintrc.js', 'jest.config.js', + '/lib/**/index.ts$', '.*.provider.ts$', '.*.module.ts$', '.*.model.ts$', diff --git a/sonar-project.properties b/sonar-project.properties index 6b77cc8..a8f76cc 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -1,10 +1,10 @@ sonar.projectKey=friendly-testers_ts-testing-buddy sonar.organization=friendly-testers sonar.testExecutionReportPaths=./test-report.xml -sonar.exclusions=**/node_modules/**,**/dist/**,**/coverage/**,**/lib/**/*.model.ts,**/lib/**/__mocks__/**,**/lib/**/*.dto.ts,**/lib/**/*.module.ts,**/lib/configs/**,**/lib/datasources/migrations/**,**/lib/main.ts,**/lib/server.ts,**/lib/app.ts,**/lib/index.ts,**/lib/bootstrap.ts +sonar.exclusions=**/node_modules/**,**/dist/**,**/coverage/**,**/lib/**/*.model.ts,**/lib/**/__mocks__/**,**/lib/**/*.dto.ts,**/lib/**/*.module.ts,**/lib/configs/**,**/lib/datasources/migrations/**,**/lib/main.ts,**/lib/server.ts,**/lib/app.ts,**/lib/index.ts,**/lib/bootstrap.ts,**/lib/**/index.ts sonar.sources=lib sonar.test.inclusions=**/*.spec.ts,**/*.test.ts,**/*.spec.tsx,**/*.test.tsx -sonar.test.exclusions=**/node_modules/**,**/dist/**,**/coverage/**,**/lib/**/*.model.ts,**/lib/**/__mocks__/**,**/lib/**/*.dto.ts,**/lib/**/*.module.ts,**/lib/configs/**,**/lib/datasources/migrations/**,**/lib/main.ts,**/lib/server.ts,**/lib/app.ts,**/lib/index.ts,**/lib/bootstrap.ts +sonar.test.exclusions=**/node_modules/**,**/dist/**,**/coverage/**,**/lib/**/*.model.ts,**/lib/**/__mocks__/**,**/lib/**/*.dto.ts,**/lib/**/*.module.ts,**/lib/configs/**,**/lib/datasources/migrations/**,**/lib/main.ts,**/lib/server.ts,**/lib/app.ts,**/lib/index.ts,**/lib/bootstrap.ts,**/lib/**/index.ts sonar.typescript.lcov.reportPaths=coverage/lcov.info sonar.eslint.reportPaths=./eslint-report.json sonar.typescript.tsconfigPath=./tsconfig.json From f3757ee6c1d27c560501f71427e2ed462f81244f Mon Sep 17 00:00:00 2001 From: mts88 Date: Thu, 10 Oct 2024 23:25:59 +0200 Subject: [PATCH 5/7] build: fix coverage --- jest.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jest.config.js b/jest.config.js index dbbcec2..46a7789 100644 --- a/jest.config.js +++ b/jest.config.js @@ -19,7 +19,7 @@ module.exports = { '/coverage', '.eslintrc.js', 'jest.config.js', - '/lib/**/index.ts$', + '**/*/index.ts$', '.*.provider.ts$', '.*.module.ts$', '.*.model.ts$', From a0b2da3cf97a5fefc466dc304fde1c80694c0584 Mon Sep 17 00:00:00 2001 From: mts88 Date: Thu, 10 Oct 2024 23:32:19 +0200 Subject: [PATCH 6/7] chore: fix index path --- jest.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jest.config.js b/jest.config.js index 46a7789..a5388ae 100644 --- a/jest.config.js +++ b/jest.config.js @@ -19,7 +19,7 @@ module.exports = { '/coverage', '.eslintrc.js', 'jest.config.js', - '**/*/index.ts$', + '/*/*/index.ts', '.*.provider.ts$', '.*.module.ts$', '.*.model.ts$', From 3675ff973a29bcac8d773d3bf623a9608e678975 Mon Sep 17 00:00:00 2001 From: mts88 Date: Thu, 10 Oct 2024 23:43:58 +0200 Subject: [PATCH 7/7] chore: add jest as peerDependencies Require jest as a peerDependencies --- package.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/package.json b/package.json index 02d5803..ca4658f 100644 --- a/package.json +++ b/package.json @@ -70,6 +70,9 @@ "reflect-metadata": "^0.2.2", "uuid": "^10.0.0" }, + "peerDependencies": { + "jest": "^29.7.0" + }, "config": { "commitizen": { "path": "./node_modules/cz-conventional-changelog"