Skip to content

Commit

Permalink
test(logs): fix the failing tests due to ansi styles
Browse files Browse the repository at this point in the history
I was not able to disable or mock ansi-styles so instead I found a way to make the tests pass
it's not perfect but it's still nice because the logs will keep their trustful colour when running through the tests
  • Loading branch information
C0ZEN committed May 25, 2021
1 parent 9821c26 commit 3488a07
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 29 deletions.
3 changes: 1 addition & 2 deletions jest.config.js
Expand Up @@ -7,6 +7,5 @@ module.exports = {
transform: {
'^.+\\.ts$': 'ts-jest'
},
verbose: true,
setupFilesAfterEnv: [`./jest/test.ts`]
verbose: true
};
9 changes: 0 additions & 9 deletions jest/test.ts

This file was deleted.

50 changes: 38 additions & 12 deletions src/classes/loggers/issue-logger.spec.ts
Expand Up @@ -26,12 +26,17 @@ describe('IssueLogger', (): void => {
});

it('should log a warning with the given message and with the issue number as prefix', (): void => {
expect.assertions(2);
expect.assertions(3);

issueLogger.warning(message);

expect(coreWarningSpy).toHaveBeenCalledTimes(1);
expect(coreWarningSpy).toHaveBeenCalledWith('[#8] dummy-message');
expect(coreWarningSpy).toHaveBeenCalledWith(
expect.stringContaining('[#8]')
);
expect(coreWarningSpy).toHaveBeenCalledWith(
expect.stringContaining('dummy-message')
);
});
});

Expand All @@ -52,12 +57,15 @@ describe('IssueLogger', (): void => {
});

it('should log an information with the given message and with the issue number as prefix', (): void => {
expect.assertions(2);
expect.assertions(3);

issueLogger.info(message);

expect(coreInfoSpy).toHaveBeenCalledTimes(1);
expect(coreInfoSpy).toHaveBeenCalledWith('[#8] dummy-message');
expect(coreInfoSpy).toHaveBeenCalledWith(expect.stringContaining('[#8]'));
expect(coreInfoSpy).toHaveBeenCalledWith(
expect.stringContaining('dummy-message')
);
});
});

Expand All @@ -78,17 +86,22 @@ describe('IssueLogger', (): void => {
});

it('should log an error with the given message and with the issue number as prefix', (): void => {
expect.assertions(2);
expect.assertions(3);

issueLogger.error(message);

expect(coreErrorSpy).toHaveBeenCalledTimes(1);
expect(coreErrorSpy).toHaveBeenCalledWith('[#8] dummy-message');
expect(coreErrorSpy).toHaveBeenCalledWith(
expect.stringContaining('[#8]')
);
expect(coreErrorSpy).toHaveBeenCalledWith(
expect.stringContaining('dummy-message')
);
});
});

it('should prefix the message with the issue number', (): void => {
expect.assertions(2);
expect.assertions(3);
message = 'dummy-message';
issue = new Issue(
DefaultProcessorOptions,
Expand All @@ -102,7 +115,12 @@ describe('IssueLogger', (): void => {
issueLogger.warning(message);

expect(coreWarningSpy).toHaveBeenCalledTimes(1);
expect(coreWarningSpy).toHaveBeenCalledWith('[#123] dummy-message');
expect(coreWarningSpy).toHaveBeenCalledWith(
expect.stringContaining('[#123]')
);
expect(coreWarningSpy).toHaveBeenCalledWith(
expect.stringContaining('dummy-message')
);
});

it.each`
Expand All @@ -114,7 +132,7 @@ describe('IssueLogger', (): void => {
`(
'should replace the special tokens "$$type" with the corresponding type',
({pull_request, replacement}): void => {
expect.assertions(2);
expect.assertions(3);
message = 'The $$type will stale! $$type will soon be closed!';
issue = new Issue(
DefaultProcessorOptions,
Expand All @@ -130,7 +148,12 @@ describe('IssueLogger', (): void => {

expect(coreWarningSpy).toHaveBeenCalledTimes(1);
expect(coreWarningSpy).toHaveBeenCalledWith(
`[#8] The ${replacement} will stale! ${replacement} will soon be closed!`
expect.stringContaining(`[#8]`)
);
expect(coreWarningSpy).toHaveBeenCalledWith(
expect.stringContaining(
`The ${replacement} will stale! ${replacement} will soon be closed!`
)
);
}
);
Expand All @@ -144,7 +167,7 @@ describe('IssueLogger', (): void => {
`(
'should replace the special token "$$type" with the corresponding type with first letter as uppercase',
({pull_request, replacement}): void => {
expect.assertions(2);
expect.assertions(3);
message = '$$type will stale';
issue = new Issue(
DefaultProcessorOptions,
Expand All @@ -160,7 +183,10 @@ describe('IssueLogger', (): void => {

expect(coreWarningSpy).toHaveBeenCalledTimes(1);
expect(coreWarningSpy).toHaveBeenCalledWith(
`[#8] ${replacement} will stale`
expect.stringContaining(`[#8]`)
);
expect(coreWarningSpy).toHaveBeenCalledWith(
expect.stringContaining(`${replacement} will stale`)
);
}
);
Expand Down
12 changes: 9 additions & 3 deletions src/classes/loggers/logger.spec.ts
Expand Up @@ -25,7 +25,9 @@ describe('Logger', (): void => {
logger.warning(message);

expect(coreWarningSpy).toHaveBeenCalledTimes(1);
expect(coreWarningSpy).toHaveBeenCalledWith('dummy-message');
expect(coreWarningSpy).toHaveBeenCalledWith(
expect.stringContaining('dummy-message')
);
});
});

Expand All @@ -46,7 +48,9 @@ describe('Logger', (): void => {
logger.info(message);

expect(coreInfoSpy).toHaveBeenCalledTimes(1);
expect(coreInfoSpy).toHaveBeenCalledWith('dummy-message');
expect(coreInfoSpy).toHaveBeenCalledWith(
expect.stringContaining('dummy-message')
);
});
});

Expand All @@ -67,7 +71,9 @@ describe('Logger', (): void => {
logger.error(message);

expect(coreErrorSpy).toHaveBeenCalledTimes(1);
expect(coreErrorSpy).toHaveBeenCalledWith('dummy-message');
expect(coreErrorSpy).toHaveBeenCalledWith(
expect.stringContaining('dummy-message')
);
});
});
});
2 changes: 1 addition & 1 deletion tsconfig.app.json
@@ -1,5 +1,5 @@
{
"extends": "./tsconfig.json",
"exclude": ["node_modules", "**/*.spec.ts", "jest"],
"exclude": ["node_modules", "**/*.spec.ts"],
"include": ["src"]
}
2 changes: 1 addition & 1 deletion tsconfig.json
Expand Up @@ -8,5 +8,5 @@
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
//"sourceMap": true
},
"include": ["src", "__tests__", "jest"]
"include": ["src", "__tests__"]
}
2 changes: 1 addition & 1 deletion tsconfig.spec.json
@@ -1,5 +1,5 @@
{
"extends": "./tsconfig.json",
"include": ["src", "__tests__", "jest"],
"include": ["src", "__tests__"],
"exclude": ["node_modules"]
}

0 comments on commit 3488a07

Please sign in to comment.