Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jest.clearAllMocks is not clearing my test spy's #1992

Closed
njmaeff opened this issue Feb 4, 2019 · 1 comment
Closed

jest.clearAllMocks is not clearing my test spy's #1992

njmaeff opened this issue Feb 4, 2019 · 1 comment

Comments

@njmaeff
Copy link

njmaeff commented Feb 4, 2019

Issue description or question

As I continue writing my tests, the mock counts increase to a very high number. When I run the test using the jest cli, the spy seems to reset because the tests pass.

Here is a sample repository
https://github.com/NikkiJ19/wallaby-jest
branch - issue/mock-clearAll

Okay initially.

image

Once I begin typing, the counts start racking up.

image

Jest cli does not show an error.

image

Wallaby.js configuration file

module.exports = function () {

    return {
        files: [
            'tsconfig.json',
            'package.json',
            '**/__mocks__/**',
            '!/__tests__/**/*.ts',
        ],


        tests: [
            '/__tests__/**/*.ts',
        ],

        filesWithNoCoverageCalculated: [
            '**/__mocks__/**'
        ],

        env: {
            type: 'node',
            runner: 'node'
        },

        testFramework: 'jest',

        debug: true,

        preprocessors: {
            '**/*.js?(x)': file => require('babel-core').transform(
                file.content,
                {sourceMap: true, filename: file.path, presets: ['babel-preset-jest']})
        },
        setup(wallaby) {
            const config = require("./package.json").jest;
            wallaby.testFramework.configure(config);
        }
    };
};

Code editor or IDE name and version

WebStorm 2018.3.4
Build #WS-183.5429.34, built on January 30, 2019
Subscription is active until April 27, 2019
JRE: 1.8.0_152-release-1343-b26 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 10 10.0

OS name and version

OS Name Microsoft Windows 10 Pro
Version 10.0.17134 Build 17134

@ArtemGovorov
Copy link
Member

ArtemGovorov commented Feb 4, 2019

The issue is not Wallaby specific. The same issue can be reproduced if you run Jest CLI with --watch flag (and comment/uncomment jest.clearAllMocks call):

wj

Note that this issue, and #1991 are specific to fs.existsSync mocking. Other objects/modules mocking should work fine in Jest.

My understanding is that mocking fs.existsSync breaks Jest in unexpected ways (even after you clean the mock out).

Another example of things being wrong is visible on your screenshot - Jest CLI finds an obsolete snapshot, even though it doesn't exist. If you run jest -u, you will get an error saying that the snapshot file doesn't exist, but mocking fs.existsSync makes Jest think that it does.

Another example of broken things is the other issue that you have created - #1991.

I would recommend against mocking fs.existsSync, but if you absolutely have to, you may use Wallaby with workers.recycle = true setting, that will make it work as you would run Jest CLI not in watch mode (like on your screenshot):

module.exports = function () {

    return {
        files: [
            'tsconfig.json',
            'package.json',
            '**/__mocks__/**',
            '!/__tests__/**/*.ts',
        ],


        tests: [
            '/__tests__/**/*.ts',
        ],

        filesWithNoCoverageCalculated: [
            '**/__mocks__/**'
        ],

        env: {
            type: 'node',
            runner: 'node'
        },

        testFramework: 'jest',

        debug: true,

+       workers: {recycle: true},

        preprocessors: {
            '**/*.js?(x)': file => require('babel-core').transform(
                file.content,
                {sourceMap: true, filename: file.path, presets: ['babel-preset-jest']})
        },
        setup(wallaby) {
            const config = require("./package.json").jest;
            wallaby.testFramework.configure(config);
        }
    };
};

The same solution applies to another issue #1991. The solution does come with performance cost (as Jest needs to be restarted).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants