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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

.mock doesn't exist #9009

Closed
moonformeli opened this issue Oct 5, 2019 · 2 comments
Closed

.mock doesn't exist #9009

moonformeli opened this issue Oct 5, 2019 · 2 comments

Comments

@moonformeli
Copy link

馃挰 Questions and Help

Hi, I'm new to Jest. I was following the code from es6-class-mocks.
And there's something that doesn't work in my code.

// package.json
"devDependencies": {
  "@types/jest": "^24.0.18",
  "jest": "^24.9.0",
  "ts-jest": "^24.1.0",
  "typescript": "^3.6.3"
},
// jest.config.js
module.exports = {
  roots: ["<rootDir>/src"],
  transform: {
    "^.+\\.tsx?$": "ts-jest"
  }
};

I'm using jest with TypeScript. And these below are the related codes about my question.

// Sport.ts
export default class Sport {
  name: string;

  constructor() {
    this.name = "Sport";
  }

  printName() {
    return this.name;
  }
}
// Sport.spec.ts
import Sport from '../Sport';
jest.mock('../Sport');

beforeEach(() => {
  // This gives me an error
  Sport.mockClear();
});

So, Sport.mockClear() gives me an error, Property 'mockClear' does not exist on type 'typeof Sport'.ts. And Sport doesn't even have mock property.

So I found this issue ts-jest issue, and here's my new code.

// Sport.spec.ts
import Sport from '../Sport';
import { mocked } from 'ts-jest/utils';
jest.mock('../Sport');

beforeEach(() => {
  // This works
  mocked(Sport).mockClear();
});

it('Constructor is called', () => {
  new Sport();
  expect(Sport).toHaveBeenCalledTimes(1);
});

it('Mock is clear', () => {
  // Succeess
  expect(Sport).not.toHaveBeenCalled();
});

Question

So, I wonder why Sport.mockClear is error and is it the only way to use ts-jest/utils?

@thymikee
Copy link
Collaborator

thymikee commented Oct 5, 2019

This is a TypeScript error. One thing you could do is to cast the type to jest.Mock. Please ask on TS forums about it though.

@thymikee thymikee closed this as completed Oct 5, 2019
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants