bpedersen / jest-mock-console Goto Github PK
View Code? Open in Web Editor NEWJest utility to mock the console
License: MIT License
Jest utility to mock the console
License: MIT License
Hello! Love this package. So useful. Thank you for maintaining it!
I'm confused by the 2.0.0 release that was just published to NPM 2 hours ago (which GitHub's Dependabot is alerting me to), yet this repository's package.json says the latest is 1.3.0.
Was that a mistake in the published version?
What changed that warranted a major? The latest commit ~19 hours ago just looks like bug fixes, not even new features, so why not just a 1.2.4 patch?
Just wanting to understand the versioning and the difference between the repo and NPM.
With this option set, I get
● Test suite failed to run
TypeError: describe.skip is not a function
In all test where I describe.skip
.
I just upgraded the packages of my app, and now tests no longer run:
➜ npx jest test/my.unit.test.ts
FAIL test/my.unit.test.ts
● Test suite failed to run
ReferenceError: describe is not defined
at Object.<anonymous> (node_modules/jest-mock-console/dist/index.js:16:1)
at Object.<anonymous> (node_modules/jest-mock-console/index.js:1:103)
This worked with the pre-2.x version of this package.
My config is as follows:
{
"jest": {
"collectCoverage": true,
"preset": "ts-jest",
"setupFiles": [
"jest-date-mock",
"jest-mock-console"
],
"setupFilesAfterEnv": [
"jest-mock-console/dist/setupTestFramework.js"
],
"testMatch": [
"**/test/*.test.ts",
"**/test/test.ts"
],
"testRunner": "jasmine2",
"globals": {
"__DEV__": true,
"ts-jest": {
"tsconfig": {
"noImplicitAny": false,
"noUnusedLocals": false
}
}
}
}
}
I am using Jest 28.1.1.
Looking at the commit log of this repo, I am unsure what actually changed. This repo seems to still be at < 2.0? (Seeing #35 it seems you haven't yet pushed the code?)
Update setupTestFrameworkScriptFile
to setupFilesAfterEnv
When adding the following to jest.config.js
{
setupTestFrameworkScriptFile: 'jest-mock-console/dist/setupTestFramework.js',
}
Jest reports the following:
● Deprecation Warning:
Option "setupTestFrameworkScriptFile" was replaced by configuration "setupFilesAfterEnv", which supports multiple paths.
Please update your configuration.
Configuration Documentation:
https://jestjs.io/docs/configuration.html
"devDependencies": {
"@types/jest": "^24.0.22",
"jest": "^24.9.0",
"jest-mock-console": "^1.0.0",
"ts-jest": "^24.1.0",
}
The following seems to work:
{
setupFilesAfterEnv: [
'jest-mock-console/dist/setupTestFramework.js',
],
}
ReferenceError: jest is not defined
118 | };
119 |
> 120 | const restoreConsole = mockConsole("error");
| ^
121 | ...
at mockConsole (../../node_modules/jest-mock-console/dist/index.js:26:31)
I'm using these potentially relevant dependencies:
"jest": "^27.4.3",
"jest-mock-console": "^1.2.3",
"ts-jest": "^27.0.7",
Looking at dist/index.js
is does appear that you're not importing jest from @jest/globals
which was a breaking change in Jest 26: https://jestjs.io/blog/2020/05/05/jest-26#a-new-way-to-consume-jest---jestglobals
I'm also running with the injectGlobals: false,
setting in jest.config.js
, setting it to true
fixes this error
Please update for Jest 28.
Hi
I was about to release almost the same package but when I realized it already exists I stopped myself. Just one consideration: I developed my package it with the opposite goal of your, I didn't need to hide the annoying logging but I'd like to have access to all the stuff logged.
Jest is infamous for logging because it updates the bash output and it's hard to analyze what the testing code is logging and what you're logging in your test files (and when a test fails it could be really useful). Using a mock you can at least do something like
expect(console.log.mock.calls).toEqual([])
and immediately it outputs the full list of console log calls/params. I know that it's a workaround but it works.
So: could I update the readme and make a pull request explaining this particular use of your library? Or you prefer that I deploy on NPM a package dedicated to logging instead of hiding logging?
Thank you
Best
Stefano
● Test suite failed to run
ReferenceError: jasmine is not defined
at Object.<anonymous> (node_modules/jest-mock-console/dist/setupTestFramework.js:9:24)
When using it with [email protected]
; problem goes away downgrading back to [email protected]
.
It appears that dist as distributed to npm and held here is an older version that doesn't accept arguments.
Meanwhile src has the new feature.
Found this little gem of a library today, fits nicely in my testing workflow. It would be great if it could support a Typescript development environment however. Would be happy to submit a pull request if this is something you are interested in.
const mockConsole = jest.requireActual( "jest-mock-console");
describe('api calls', () => {
beforeAll(() => {
expect(jest.isMockFunction(mockConsole)).toBeFalsy();
Except that line is failing the test.
I'm using automock: true,
in my jest config, which is clearly part of the issue. But I should be able to use requireActual
to get around that. But it doesn't appear to work.
Also tried:
const {mockConsole} = jest.requireActual( "jest-mock-console");
But then the test fails:
TypeError: mockConsole is not a function
> 11 | const restoreConsole = mockConsole();
Also tried
const mockConsole = jest.requireActual( "jest-mock-console");
But then the mock-check line expect(jest.isMockFunction(mockConsole)).toBeFalsy();
fails.
Get
● Test suite failed to run
Invalid: beforeEach() may not be used in a describe block containing no tests.
Calls to mockConsole() are not returning anything:
const mockConsole = require('jest-mock-console')
beforeEach(async () => {
const consoleRestoreFn = mockConsole()
})
In this example, consoleRestoreFn
ends up as undefined
.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.