Skip to content

Commit

Permalink
refactor: ➖ remove chalk as dependency (#44)
Browse files Browse the repository at this point in the history
* refactor: ➖ remove `chalk` as dependency

fixes: #43
  • Loading branch information
kopach committed Nov 22, 2023
1 parent 72fc01b commit 35014bc
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
24 changes: 18 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const util = require('util')
const chalk = require('chalk')

const chalk = {
red: (str) => `\u001B[31m${str}\u001B[39m`,
gray: (str) => `\u001B[90m${str}\u001B[39m`,
white: (str) => `\u001B[37m${str}\u001B[39m`,
bold: (str) => `\u001B[1m${str}\u001B[22m`,
}

const defaultErrorMessage = (methodName, bold) =>
`Expected test not to call ${bold(`console.${methodName}()`)}.\n\n` +
Expand Down Expand Up @@ -40,15 +46,18 @@ const init = ({
throw new Error(`${message}\n\n${messages.join('\n\n')}`)
}
}
const groups = [];
const groups = []

const patchConsoleMethod = (methodName) => {
const unexpectedConsoleCallStacks = []

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

if (silenceMessage && silenceMessage(message, methodName, { group: groups[groups.length - 1], groups })) {
if (
silenceMessage &&
silenceMessage(message, methodName, { group: groups[groups.length - 1], groups })
) {
return
}

Expand All @@ -57,7 +66,10 @@ const init = ({
// Don't throw yet though b'c it might be accidentally caught and suppressed.
const { stack } = new Error()
if (stack) {
unexpectedConsoleCallStacks.push([stack.substr(stack.indexOf('\n') + 1), [...groups, message].join('\n')])
unexpectedConsoleCallStacks.push([
stack.substr(stack.indexOf('\n') + 1),
[...groups, message].join('\n'),
])
}
}

Expand Down Expand Up @@ -97,10 +109,10 @@ const init = ({

return false
}
let shouldSkipTest;
let shouldSkipTest

beforeEach(() => {
shouldSkipTest = canSkipTest();
shouldSkipTest = canSkipTest()
if (shouldSkipTest) return

console[methodName] = newMethod // eslint-disable-line no-console
Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
"test": "jest tests/index.test.js",
"type-check": "tsc --noEmit index.d.ts"
},
"dependencies": {
"chalk": "^4.1.0"
},
"devDependencies": {
"jest": "^27.5.1",
"np": "^7.2.0",
Expand Down
4 changes: 2 additions & 2 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('jest-fail-on-console', () => {
it('errors when console.error() is called', async () => {
const { stderr } = await runFixture('error')

expect(stderr).toEqual(expect.stringContaining('Expected test not to call console.error().'))
expect(stderr).toEqual(expect.stringMatching(/Expected test not to call .*console.error().*/))
})

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

expect(stderr).toEqual(expect.stringContaining('Expected test not to call console.assert().'))
expect(stderr).toEqual(expect.stringMatching(/Expected test not to call .*console.assert().*/))
})

it('does not error when console.assert() is called with a passing assertion', async () => {
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1635,9 +1635,9 @@ fs.realpath@^1.0.0:
integrity sha1-FQStJSMVjKpA20onh8sBQRmU6k8=

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

function-bind@^1.1.1:
version "1.1.1"
Expand Down Expand Up @@ -2613,7 +2613,7 @@ jest-worker@^27.5.1:

jest@^27.5.1:
version "27.5.1"
resolved "https://registry.yarnpkg.com/jest/-/jest-27.5.1.tgz#dadf33ba70a779be7a6fc33015843b51494f63fc"
resolved "https://registry.npmjs.org/jest/-/jest-27.5.1.tgz"
integrity sha512-Yn0mADZB89zTtjkPJEXwrac3LHudkQMR+Paqa8uxJHCBr9agxztUifWCyiYrjhMPBoUVBjyny0I7XH6ozDr7QQ==
dependencies:
"@jest/core" "^27.5.1"
Expand Down Expand Up @@ -4048,7 +4048,7 @@ typedarray-to-buffer@^3.1.5:

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

unique-string@^2.0.0:
Expand Down

0 comments on commit 35014bc

Please sign in to comment.