Skip to content

Commit 35014bc

Browse files
authored
refactor: ➖ remove chalk as dependency (#44)
* refactor: ➖ remove `chalk` as dependency fixes: #43
1 parent 72fc01b commit 35014bc

File tree

4 files changed

+25
-16
lines changed

4 files changed

+25
-16
lines changed

index.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
const util = require('util')
2-
const chalk = require('chalk')
2+
3+
const chalk = {
4+
red: (str) => `\u001B[31m${str}\u001B[39m`,
5+
gray: (str) => `\u001B[90m${str}\u001B[39m`,
6+
white: (str) => `\u001B[37m${str}\u001B[39m`,
7+
bold: (str) => `\u001B[1m${str}\u001B[22m`,
8+
}
39

410
const defaultErrorMessage = (methodName, bold) =>
511
`Expected test not to call ${bold(`console.${methodName}()`)}.\n\n` +
@@ -40,15 +46,18 @@ const init = ({
4046
throw new Error(`${message}\n\n${messages.join('\n\n')}`)
4147
}
4248
}
43-
const groups = [];
49+
const groups = []
4450

4551
const patchConsoleMethod = (methodName) => {
4652
const unexpectedConsoleCallStacks = []
4753

4854
const captureMessage = (format, ...args) => {
4955
const message = util.format(format, ...args)
5056

51-
if (silenceMessage && silenceMessage(message, methodName, { group: groups[groups.length - 1], groups })) {
57+
if (
58+
silenceMessage &&
59+
silenceMessage(message, methodName, { group: groups[groups.length - 1], groups })
60+
) {
5261
return
5362
}
5463

@@ -57,7 +66,10 @@ const init = ({
5766
// Don't throw yet though b'c it might be accidentally caught and suppressed.
5867
const { stack } = new Error()
5968
if (stack) {
60-
unexpectedConsoleCallStacks.push([stack.substr(stack.indexOf('\n') + 1), [...groups, message].join('\n')])
69+
unexpectedConsoleCallStacks.push([
70+
stack.substr(stack.indexOf('\n') + 1),
71+
[...groups, message].join('\n'),
72+
])
6173
}
6274
}
6375

@@ -97,10 +109,10 @@ const init = ({
97109

98110
return false
99111
}
100-
let shouldSkipTest;
112+
let shouldSkipTest
101113

102114
beforeEach(() => {
103-
shouldSkipTest = canSkipTest();
115+
shouldSkipTest = canSkipTest()
104116
if (shouldSkipTest) return
105117

106118
console[methodName] = newMethod // eslint-disable-line no-console

package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919
"test": "jest tests/index.test.js",
2020
"type-check": "tsc --noEmit index.d.ts"
2121
},
22-
"dependencies": {
23-
"chalk": "^4.1.0"
24-
},
2522
"devDependencies": {
2623
"jest": "^27.5.1",
2724
"np": "^7.2.0",

tests/index.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('jest-fail-on-console', () => {
2020
it('errors when console.error() is called', async () => {
2121
const { stderr } = await runFixture('error')
2222

23-
expect(stderr).toEqual(expect.stringContaining('Expected test not to call console.error().'))
23+
expect(stderr).toEqual(expect.stringMatching(/Expected test not to call .*console.error().*/))
2424
})
2525

2626
it('does not error when console.error() is called but shouldFailOnError is false', async () => {
@@ -44,7 +44,7 @@ describe('jest-fail-on-console', () => {
4444
it('errors when console.assert() is called with a failing assertion', async () => {
4545
const { stderr } = await runFixture('assert-failure')
4646

47-
expect(stderr).toEqual(expect.stringContaining('Expected test not to call console.assert().'))
47+
expect(stderr).toEqual(expect.stringMatching(/Expected test not to call .*console.assert().*/))
4848
})
4949

5050
it('does not error when console.assert() is called with a passing assertion', async () => {

yarn.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,9 +1635,9 @@ fs.realpath@^1.0.0:
16351635
integrity sha1-FQStJSMVjKpA20onh8sBQRmU6k8=
16361636

16371637
fsevents@^2.3.2:
1638-
version "2.3.2"
1639-
resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-2.3.2.tgz#8a526f78b8fdf4623b709e0b975c52c24c02fd1a"
1640-
integrity sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==
1638+
version "2.3.3"
1639+
resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-2.3.3.tgz#cac6407785d03675a2a5e1a5305c697b347d90d6"
1640+
integrity sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==
16411641

16421642
function-bind@^1.1.1:
16431643
version "1.1.1"
@@ -2613,7 +2613,7 @@ jest-worker@^27.5.1:
26132613

26142614
jest@^27.5.1:
26152615
version "27.5.1"
2616-
resolved "https://registry.yarnpkg.com/jest/-/jest-27.5.1.tgz#dadf33ba70a779be7a6fc33015843b51494f63fc"
2616+
resolved "https://registry.npmjs.org/jest/-/jest-27.5.1.tgz"
26172617
integrity sha512-Yn0mADZB89zTtjkPJEXwrac3LHudkQMR+Paqa8uxJHCBr9agxztUifWCyiYrjhMPBoUVBjyny0I7XH6ozDr7QQ==
26182618
dependencies:
26192619
"@jest/core" "^27.5.1"
@@ -4048,7 +4048,7 @@ typedarray-to-buffer@^3.1.5:
40484048

40494049
typescript@^4.8.2:
40504050
version "4.8.2"
4051-
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.8.2.tgz#e3b33d5ccfb5914e4eeab6699cf208adee3fd790"
4051+
resolved "https://registry.npmjs.org/typescript/-/typescript-4.8.2.tgz"
40524052
integrity sha512-C0I1UsrrDHo2fYI5oaCGbSejwX4ch+9Y5jTQELvovfmFkK3HHSZJB8MSJcWLmCUBzQBchCrZ9rMRV6GuNrvGtw==
40534053

40544054
unique-string@^2.0.0:

0 commit comments

Comments
 (0)