Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .ctirc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
"include": [
"**/*.ts"
],
"exclude": [],
"exclude": [
"**/__mocks__/*"
],
"skipEmptyDir": true,
"startFrom": "./lib",
"output": "./dist",
Expand Down
9 changes: 9 additions & 0 deletions .github/actions/eslint-report/action.yml
Original file line number Diff line number Diff line change
@@ -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"
3 changes: 3 additions & 0 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ module.exports = {
'/coverage',
'.eslintrc.js',
'jest.config.js',
'/*/*/index.ts',
'.*.provider.ts$',
'.*.module.ts$',
'.*.model.ts$',
'.*.interface.ts$',
'.*.mock.ts$',
'.*.dto.ts$',
'.*\\.(spec|test)\\.(t|j)s$',
'__mocks__',
],
moduleFileExtensions: ['js', 'json', 'ts'],
clearMocks: true,
Expand Down
3 changes: 3 additions & 0 deletions lib/functions/spyConstructor/__mocks__/stub-constructor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class StubConstructorExample {
public foo = 'bar';
}
1 change: 1 addition & 0 deletions lib/functions/spyConstructor/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './spy-constructor';
25 changes: 25 additions & 0 deletions lib/functions/spyConstructor/spy-constructor.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { spyConstructor } from './spy-constructor';

describe('spyConstructor', () => {
it('should returns a mock of the constructor', () => {
const spiedObject = spyConstructor<typeof import('./__mocks__/stub-constructor')>(
'./__mocks__/stub-constructor',
'StubConstructorExample',
);

expect(spiedObject._isMockFunction).toBeTrue();
});

it('should mocks the constructor with the desired value', () => {
const mockedValue = jest.fn();

const spiedObject = spyConstructor<typeof import('./__mocks__/stub-constructor')>(
'./__mocks__/stub-constructor',
'StubConstructorExample',
).mockReturnValue(mockedValue);

const resultFromMock = spiedObject();

expect(resultFromMock).toBe(mockedValue);
});
});
11 changes: 11 additions & 0 deletions lib/functions/spyConstructor/spy-constructor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function spyConstructor<T extends object, Key extends keyof T = keyof T>(
module: string,
constructorName: Key,
): ReturnType<typeof jest.spyOn> {
jest.doMock(module);

const moduleImport = jest.requireActual<T>(module);

const spiedConstructor = jest.spyOn(moduleImport, constructorName as any);
return spiedConstructor;
}
1 change: 1 addition & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './functions/spyConstructor';
export * from './mocks/create-mock-with-uuid';
export * from './utils';
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions sonar-project.properties
Original file line number Diff line number Diff line change
@@ -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,**/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/**/*.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
2 changes: 1 addition & 1 deletion tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"extends": "./tsconfig.json",
"exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
"exclude": ["node_modules", "test", "dist", "**/*spec.ts", "**/__mocks__/*"]
}